这是一个小故事.
曾几何时,一个小项目想要使用node-mongodb-native.但是,它非常害羞,它想要使用包装器对象隐藏它.
var mongodb = require( 'mongodb' ),
Server = mongodb.Server,
Db = mongodb.Db,
database;
var MongoModule = {};
MongoModule.setup = function() {
// Create a mongodb client object
var client = new Db( this.config.databaseName,
new Server(
this.config.serverConfig.address,
this.config.serverConfig.port,
this.config.serverConfig.options
),
this.config.options
);
// Open the connection!
client.open( function( err, db ) {
if ( err ) throw err;
database = db;
console.log( 'Database driver loaded.' );
});
};
Run Code Online (Sandbox Code Playgroud)
该setup方法是一种启动小项目的方法.它在应用程序运行时被调用.
为了尝试自己,小项目为collection方法添加了一个包装方法node-mongodb-native.
MongoModule.collection …Run Code Online (Sandbox Code Playgroud) 我正在尝试从nodejs中使用MongoDB 2.4实验文本搜索功能.唯一的问题是,就我所知,native nativejs mongo驱动程序似乎不支持集合级runCommand.
Mongo shell语法如下所示:
db.collection.runCommand( "text", { search : "Textvalue" } );
Run Code Online (Sandbox Code Playgroud)
它出现了一个db.command/db.executeDbCommand函数,但我不知道如何选择一个集合并使用它运行text命令(如果可能的话),因为它需要在集合级别而不是数据库级别.
任何帮助,将不胜感激
我似乎无法在他们的文档或互联网上找到MongoDB错误对象的任何示例.
示例MongoDB错误对象是什么样的?我想根据错误来"处理"错误和/或为自己的目的重新格式化错误.
此代码转储为异常
self.staticVars.Model
.find({shortAddress: {$text : { $search: data.text }}, _town: data._town},{limit: 10})
.populate('_street _district')
.sort({house: 1})
.exec(callback);
Run Code Online (Sandbox Code Playgroud)
例外
Can't use $text with String
Run Code Online (Sandbox Code Playgroud)
模型
shortAddress: {
type: String
},
Run Code Online (Sandbox Code Playgroud)
指数
collection.ensureIndex({fullAddress: 'text', shortAddress: 'text'}, { default_language: "russian" },function(){});
Run Code Online (Sandbox Code Playgroud) 我正在使用Node.js v0.12.0和MongoDB驱动程序v1.4.34.那么,使用toString和toHexString方法将ObjectID转换为String之间有什么区别吗?
我尝试在Windows命令(Window 8.1)上从Mongo客户端连接MongoDB.当我require()在javascript中使用时,我有如下错误.有没有人有同样的问题?我错过了任何require相关的npm安装吗?MongoDB shell怎么能找不到require功能?
C:\tutorial\nodeMongoAngular-master\lesson2>mongo
MongoDB shell version: 3.0.1
connecting to: test
var MongoClient = require('mongodb').MongoClient;
2015-04-30T14:33:25.812-0400 E QUERY ReferenceError: require is not defined
at (shell):1:19
Run Code Online (Sandbox Code Playgroud) node-mongo-native collection.find()函数的文档说它创建了一个光标对象,它懒惰地返回匹配的文档.此外:
游标的基本操作是
nextObject从数据库中提取下一个匹配文档的方法.方便的方法each和toArray调用,nextObject直到光标用尽.
不幸的是,文档没有提供如何判断光标实际耗尽的指示.您可以使用"toArray"方法并使用标准数组接口(例如"长度"方法),但此解决方案不适合流式传输大量数据.MongoDB API Wiki引用cursor.hasNext()了mongo shell,但是这个方法似乎在node.js驱动程序中不可用.
在node.js中从MongoDB流式传输数据时,如何确定光标何时耗尽?
我想制作一个 HTML 表单来查询 MongoDB。如何编写忽略空白字段的逻辑?例如,我有两个搜索参数。如果姓氏字段为空,我将如何编写查询以忽略该字段?
router.post('/auth/search', auth, function(req,res){
var db = req.db;
console.log(req.body);
db.users.find({ 'firstname': req.body.firstname,
'lastname' :req.body.lastname // if this field is blank in the form how can I ignore it?
}, function(err, docs){
if (err) return err;
console.log(docs);
res.send(docs);
});
});
Run Code Online (Sandbox Code Playgroud)
感谢任何帮助。谢谢!
我目前正在使用MongoDB游标的toArray()函数将数据库结果转换为数组:
run = true;
count = 0;
var start = process.hrtime();
db.collection.find({}, {limit: 2000}).toArray(function(err, docs){
var diff = process.hrtime(start);
run = false;
socket.emit('result', {
result: docs,
time: diff[0] * 1000 + diff[1] / 1000000,
ticks: count
});
if(err) console.log(err);
});
Run Code Online (Sandbox Code Playgroud)
此操作在我的计算机上大约需要7毫秒.如果我删除.toArray()函数,那么操作大约需要0.15ms.当然这不起作用,因为我需要转发数据,但是我想知道函数是做什么的,因为它花了这么长时间?数据库中的每个文档只包含4个数字.
最后,我希望在一个更小的处理器上运行它,比如Raspberry Pi,这里从数据库中获取500个文件并将其转换为数组的操作大约需要230ms.这对我来说似乎很重要.或者我只是期待太多?
有没有其他方法可以在不使用toArray()的情况下从数据库中获取数据?
我注意到的另一件事是整个Node应用程序在获取数据库结果时显着减慢.我创建了一个简单的间隔函数,它应该每1 ms增加一次计数值:
setInterval(function(){
if(run) count++;
}, 1);
Run Code Online (Sandbox Code Playgroud)
然后我希望计数值几乎与时间相同,但是在我的计算机上16 ms的时间内,计数值为3或4.在Raspberry Pi上,计数值从未递增.什么占用了这么多CPU?显示器告诉我,当我被要求重复运行数据库查询时,我的计算机使用了27%的CPU,并且Raspberry Pi使用了92%的CPU和11%的RAM.
我知道那是很多问题.任何帮助或解释都非常感谢.我还是Node和MongoDB的新手.
通过typeorm和Nestjs使用mongodb - 创建 CRUD REST API
当尝试通过findone()和 ' id '获取数据时。低于错误
TS2345:类型参数 '{ id: string; }' 不可分配给“FindOneOptions”类型的参数。
对象文字只能指定已知属性,并且“FindOneOptions”类型中不存在“id”。
代码:
const result = await this.sellerRepository.findOne({ id });
Run Code Online (Sandbox Code Playgroud)
实体
@Entity('seller')
export class Seller {
@ObjectIdColumn()
id: ObjectID;
@Column({
type: 'string',
nullable: false,
name: 'product_name',
})
productName: string;
@Column({
name: 'short_desc',
})
}
async findOne(id: string): Promise<Seller> {
const result = await this.sellerRepository.findOne({ id });
return result;
}
Run Code Online (Sandbox Code Playgroud)