[英]How do I query in Mongo?
I'm beating my head against a wall trying to query Mongo successfully. 我头撞墙,试图成功查询Mongo。
This code: 这段代码:
async.waterfall([
function(callback){
cursor = db.collection(collection).findOne(query)
callback(null);
},
function(callback){
console.log("Result is:" + cursor);
console.log(JSON.stringify(cursor));
callback(null);
}
]);
Produces the following output: 产生以下输出:
result is:[object Object]
{}
Why? 为什么? There is a document that it should be finding in the collection.
应该在集合中找到一个文档。
As a followup question, how do I get to see what 作为后续问题,我如何了解
[object Object]
is? 是什么?
Basically you should wait for your query to complete and then call the callback and expect any result: 基本上,您应该等待查询完成,然后调用回调并期待任何结果:
async.waterfall([
function(callback){
db.collection(collection).findOne(query, function(err, result) {
callback(err, result); // if there is no err, it will be null
});
// the above can be simplified to just
// db.collection(collection).findOne(query, callback);
// since findOne callback and current function callback have the same arguments
},
function(result, callback) {
// use comma here to automatically doing JSON.stringiry(result)
console.log("Result is:", result);
callback();
}
], function(err) {
// here is your final callback where you know that async.waterfall
// is finished (with or without error)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.