MongoDB中存储的对象结构如下:
obj = {_id: "55c898787c2ab821e23e4661", ingredients: [{name: "ingredient1", value: "70.2"}, {name: "ingredient2", value: "34"}, {name: "ingredient3", value: "15.2"}, ...]}
我想做的是检索所有文档,其中特定成分的值大于任意数字。
更具体地说,假设我们要检索所有包含名称为“ingredient1”的成分且其值大于 50 的文档。
尝试以下操作但无法检索到所需的结果:
var collection = db.get('docs');
var queryTest = collection.find({$where: 'this.ingredients.name == "ingredient1" && parseFloat(this.ingredients.value) > 50'}, function(e, docs) {
console.log(docs);
});
有谁知道以特定数组元素名称和值为条件的正确查询是什么?
谢谢!
最佳答案
你真的不需要$where
的JavaScript评估在这里,只需使用基本查询运算符 $elemMatch
查询数组。虽然这里的“值”元素实际上是字符串,但这并不是真正的重点(正如我在本文末尾所解释的那样)。要点是第一次就把事情做好:
collection.find(
{
"ingredients": {
"$elemMatch": {
"name": "ingredient1",
"value": { "$gt": 50 }
}
}
},
{ "ingredients.$": 1 }
)
第二部分中的$
是postional operator ,它仅投影查询条件中匹配的数组元素。
这也比 JavaScript 评估快得多,因为评估代码不需要编译并使用 native 编码运算符,并且“索引”可以用于“名称”甚至“value” 数组元素来帮助过滤匹配。
如果您期望数组中有多个匹配项,则 .aggregate()
命令是最好的选择。对于现代 MongoDB 版本,这非常简单:
collection.aggregate([
{ "$match": {
"ingredients": {
"$elemMatch": {
"name": "ingredient1",
"value": { "$gt": 50 }
}
}
}},
{ "$redact": {
"$cond": {
"if": {
"$and": [
{ "$eq": [ { "$ifNull": [ "$name", "ingredient1" ] }, "ingredient1" ] },
{ "$gt": [ { "$ifNull": [ "$value", 60 ] }, 50 ] }
]
},
"then": "$$DESCEND",
"else": "$$PRUNE"
}
}}
])
在即将推出的版本中,引入 $filter
运算符的版本会更加简单:
collection.aggregate([
{ "$match": {
"ingredients": {
"$elemMatch": {
"name": "ingredient1",
"value": { "$gt": 50 }
}
}
}},
{ "$project": {
"ingredients": {
"$filter": {
"input": "$ingredients",
"as": "ingredient",
"cond": {
"$and": [
{ "$eq": [ "$$ingredient.name", "ingredient1" ] },
{ "$gt": [ "$$ingredient.value", 50 ] }
]
}
}
}
}}
])
在这两种情况下,您都可以有效地“过滤”初始文档匹配后与条件不匹配的数组元素。
<小时/>此外,由于您的“值”现在实际上是“字符串”,因此您确实应该将其更改为数字。这是一个基本过程:
var bulk = collection.initializeOrderedBulkOp(),
count = 0;
collection.find().forEach(function(doc) {
doc.ingredients.forEach(function(ingredient,idx) {
var update = { "$set": {} };
update["$set"]["ingredients." + idx + ".value"] = parseFloat(ingredients.value);
bulk.find({ "_id": doc._id }).updateOne(update);
count++;
if ( count % 1000 != 0 ) {
bulk.execute();
bulk = collection.initializeOrderedBulkOp();
}
})
]);
if ( count % 1000 != 0 )
bulk.execute();
这将修复数据,以便此处的查询表单正常工作。
这比使用 JavaScript $where
处理要好得多,JavaScript $where
需要评估集合中的每个文档,而无需索引来过滤。正确的形式是:
collection.find(function() {
return this.ingredients.some(function(ingredient) {
return (
( ingredient.name === "ingredient1" ) &&
( parseFloat(ingredient.value) > 50 )
);
});
})
这也不能像其他形式那样“投影”结果中的匹配值。
关于javascript - 如何通过条件嵌套对象数组来检索文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32155411/