[英]Not able to search data based on Id field of mongo document from nodejs
我正在嘗試根據其ID如下獲取文檔詳細信息。 但是,除了空數組之外,我沒有任何數據。 代碼的進一步調試指向下面代碼中的JSON.parse(query)語句解析問題。 無法弄清為什么它無法正確解析對象。
var table = dbase.collection(name);
var qfld = JSON.parse(fields);
var qobj = JSON.parse(query);
console.log('query : ' + query + ' and fileds ' + fields + ' qid ' + qobj._id + ' device id ' + qobj.deviceid);
table.find(qobj,qfld).toArray(function(err,result){
if (err) reject(err);
else
resolve(result);
});
我正在嘗試將查詢對象發送到上述功能,如下所示,其中ccaid的格式為“ 5486ab .... ce6”。
var ccid = new require('mongodb').ObjectID(ccaid);
query = '{\"_id\": '+ ccid + '};
這應該工作
var table = dbase.collection(name);
var qfld = JSON.parse(fields);
var ccid = new require('mongodb').ObjectID(ccaid);
var qobj = {_id: ccaid };
console.log('query : ' + JSON.stringify(qobj) + ' and fileds ' + fields + ' qid ' + qobj._id + ' device id ' + qobj.deviceid);
table.find(qobj,qfld).toArray(function(err,result){
if (err)
reject(err);
else
resolve(result);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.