【发布时间】:2015-10-16 10:41:15
【问题描述】:
使用 Mongoskin,我想检查一个文档是否存在于 MongoDB 集合中,其中每个文档都有一个唯一的键和一个数组作为其值。此代码完美运行:
db.collection('buyerRec').find({ "abcd" : { $exists : true }}, { _id:0 }).toArray(function(err, doc) {
...
});
找到了“abcd”文档。然而,在实际系统的设计中,文档的查询键是事先不知道的,所以我需要使用一个变量来代替“abcd”。我找不到有效的组合。在两种失败的情况下,它总是返回一个空数组-
失败的例子-1:
console.log("idCode: " + typeof idCode + " " + idCode); // idCode: string abcd
db.collection('buyerRec').find({ idCode : { $exists : true }}, { _id:0 }).toArray(function(err, doc) {
...
});
失败的例子 2:
console.log("idCode: " + typeof idCode + " " + idCode); // idCode: string abcd
var query = "\"" + idCode + "\"";
console.log("query: " + typeof query + " " + query); // query: string "abcd"
db.collection('buyerRec').find({ query : { $exists : true }}, { _id:0 }).toArray(function(err, doc) {
...
});
对我来说,这两个失败的示例中的一个应该重复了第一个示例的预期操作。有人可以指导我如何重新编码吗?谢谢。
【问题讨论】:
标签: javascript mongodb mongoskin