![](/img/trans.png)
[英]How do I log a mongodb database query to the console (not the mongo shell)?
[英]How do I query in Mongo?
我頭撞牆,試圖成功查詢Mongo。
這段代碼:
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);
}
]);
產生以下輸出:
result is:[object Object]
{}
為什么? 應該在集合中找到一個文檔。
作為后續問題,我如何了解
[object Object]
是什么?
基本上,您應該等待查詢完成,然后調用回調並期待任何結果:
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.