MongoDB $elemMatch $in
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19920829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB $elemMatch $in
提问by Digits
So, I have a database with a load of arrays in documents in it.
I want to find entire documents where my queries are an exact match for one or more array elements using $in
.
所以,我有一个数据库,其中的文档中有大量数组。我想找到整个文档,其中我的查询与使用$in
.
So, document structure:
所以,文档结构:
{
"_id": "76561198045636214",
"timecreated": 1311148549,
"unusual": [
{
"id": 1960169991,
"original_id": 698672623,
"defindex": 313,
"_particleEffect": 19
},
{
"id": 965349033,
"original_id": 931933064,
"defindex": 363,
"_particleEffect": 6
}
]
}
I have a lot of documents like this, I want to find where a document has an array containing both a defindex 313 and a _particleEffect 19 in one array entry which means I'd have to use $elemMatch
.
我有很多这样的文档,我想找到文档在一个数组条目中包含 defindex 313 和 _particleEffect 19 的数组的位置,这意味着我必须使用$elemMatch
.
I also want to be able to search for many different array combinations at once so for example an array with a defindex of 363 and a _particleEffect of either 19 or 6 which means I have to use $in
.
我还希望能够同时搜索许多不同的数组组合,例如一个 defindex 为 363 且 _particleEffect 为 19 或 6 的数组,这意味着我必须使用$in
.
However, when I try to put $elemMatch and $in
in a query, elemMatch will have nothing to do with it since it won't work on an array. I haven't been able to do it.
但是,当我尝试将 $elemMatch 放入$in
查询中时,elemMatch 将与它无关,因为它不适用于数组。我一直没能做到。
My attempts so far:
我到目前为止的尝试:
{unusual:{$all:[{"defindex":{"$in":[361,378]}},{"_particleEffect":{"$in":[30,0]}}]}}
(my latest attempt, simply does not work.)
(我最近的尝试,根本行不通。)
{"$and":[{"unusual.defindex":{"$in":[361,378]}},{"unusual._particleEffect":{"$in":[[30,36]]}}]}
and many more where I tried loads of combinations with $elemmatch
and $and
.
以及更多我尝试与$elemmatch
和组合的地方$and
。
(finds items in the unusual array but ignores array delimitation IE it will return a document where multiple items will be used to satisfy the condition (so at least one item with a defindex that matches and one item that has the effect.))
(在不寻常的数组中查找项目但忽略数组分隔 IE 它将返回一个文档,其中将使用多个项目来满足条件(因此至少有一个具有匹配 defindex 的项目和一个具有效果的项目。))
I've spend a day and a half on this and have come really far, even finding a question which was almost the same as mine but was missing any mention of an $in
part. -> MongoDB: Match multiple array elements
我已经花了一天半的时间来解决这个问题,并且已经走了很远,甚至找到了一个与我几乎相同但没有提到任何$in
部分的问题。-> MongoDB:匹配多个数组元素
tl;dr:is there a way to effectively do
$in
+$elemMatch
?
tl;dr:有没有办法有效地做
$in
+$elemMatch
?
Thanks for reading and being able to read my somewhat badly formatted post, thanks.
感谢您阅读并能够阅读我格式有点糟糕的帖子,谢谢。
回答by Asya Kamsky
You can use different syntax than the one you're trying that achieves the same result but doesn't run into the limitation in SERVER-3544.
您可以使用与您正在尝试的语法不同的语法,以获得相同的结果,但不会遇到 SERVER-3544 中的限制。
Use this syntax:
使用以下语法:
db.collection.find({ "unusual": {"$elemMatch":{"defindex":363,"_particleEffect":{"$in":[6,19]} }} })
This will match any document which has an array element with both 313 and either 6 or 19.
这将匹配具有 313 和 6 或 19 的数组元素的任何文档。
It also works with {$in:[]}
for both defindex and _particleEffect, as long as you intend to match any combination of the two lists.
{$in:[]}
只要您打算匹配两个列表的任意组合,它也适用于 defindex 和 _particleEffect。
db.collection.find({ "unusual": {"$elemMatch":{"defindex":{"$in":[313,363]},"_particleEffect":{"$in":[6,19]} }} })
回答by Digits
https://jira.mongodb.org/browse/SERVER-3544
https://jira.mongodb.org/browse/SERVER-3544
Welp, did a LOT of digging and it looks like that answers my question. You cannot do $in $elemmatch at this time.
Welp,做了很多挖掘,看起来这回答了我的问题。你现在不能做 $in $elemmatch 。
回答by Learner
Just try this (Tested)
试试这个(测试)
{
"unusual": {
$all:[{
$elemMatch:{"defindex":313},
$elemMatch:{"_particleEffect":6}
}]
}
}