为什么这个查询工作在mongo shell而不是mongo节点驱动程序?
我在shell中成功地运行了这个查询:
db.hourlydatas.find({'timeseries':ObjectId('1234')})
试图把它翻译成mongo驱动程序:
MongoClient.connect(config.db, function(err, db) { // Use the admin database for the operation var collection = db.collection('hourlydatas'); collection.find({'timeseries':'1234'}).toArray(function(err, docs) { // assert.equal(err, null); console.log("Found the following records"); console.log(docs); // callback(docs); }); });
这不会返回任何文件,我假定,因为我没有将string转换为对象ID。 这是可能的驱动程序?
尝试这个
var ObjectId = require('mongodb').ObjectID; var collection = db.collection('hourlydatas'); collection.find({'timeseries':ObjectId('1234')}).toArray(function(err,docs) {...}
它应该工作,你确定你连接到相同的数据库? 检查两个连接是否testing或生产…我曾经浪费了很多时间在这个只是最后发现我的mongoshell连接prod而节点连接testing。