MongoDB嵌套数组查询 [英] MongoDB nested array query
问题描述
我已将此问题作为对另一个的评论a>问题,并还发布了问题.到目前为止,没有任何回应,因此我要提出一个单独的问题.
I've asked this as a comment on another question, and also posted a question on mongodb-user. No responses so far, so I'm resorting to asking a separate question.
文档指出:
如果该字段包含一个数组,则$ in运算符选择 字段包含至少包含一个的数组的文档 与指定数组中的值匹配的元素(例如, 等)
If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (e.g. , , etc.)
我正在使用:
mongod --version:
db version v2.2.2, pdfile version 4.5
Thu May 30 12:19:12 git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
mongo --version:
MongoDB shell version: 2.0.4
在MongoDB shell中:
In MongoDB shell:
db.nested.insert({'level1': {'level2': [['item00', 'item01'], ['item10', 'item11']]}})
以下是应根据文档运行的查询列表以及它们产生的结果:
Here's a list of queries that should work according to the documentation, and the results they produce:
为什么这行不通?
> db.nested.findOne({'level1.level2.0': 'item00'})
null
为什么我需要全部美元?
Why do I need the $all?
> db.nested.findOne({'level1.level2.0': {'$all': ['item00']}})
{
"_id" : ObjectId("51a7a4c0909dfd8872f52ed7"),
"level1" : {
"level2" : [
[
"item00",
"item01"
],
[
"item10",
"item11"
]
]
}
}
以下至少一项应该起作用,对吧?
At least one of the following should work, right?
> db.nested.findOne({'level1.level2.0': {'$in': ['item00']}})
null
> db.nested.findOne({'level1.level2': {'$in': ['item00']}})
null
有什么想法吗?如果查询语法无法像宣传的那样工作,我们正在考虑放弃MongoDB.
Any ideas? We're considering abandoning MongoDB if the query syntax doesn't work as advertised.
谢谢!
推荐答案
使用嵌套的elemMatch
搜索数组中的嵌套级别.
Use nested elemMatch
to search nested levels within arrays.
详细信息在MongoDB中查询数组的数组
这篇关于MongoDB嵌套数组查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!