MongoDB的$ elemMatch在$ [英] mongodb $elemMatch $in

查看:112
本文介绍了MongoDB的$ elemMatch在$的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有在它的文档阵列负载的数据库。
我想找到我在哪里查询是使用在$一个或多个数组元素完全匹配整个文档。

所以,文档结构:

  {
_id:76561198045636214,
timecreated:1311148549,不寻常:
{
    ID:1960169991,
    original_id:698672623,
    defindex:313,
    _particleEffect:19
},
{
    ID:965349033,
    original_id:931933064,
    defindex:363,
    _particleEffect:6
}
]
}

我有很多像这样的文件,我想寻找到一个文档具有同时包含defindex 313和_particleEffect 19在一个阵列中的条目,这意味着我不得不使用$ elemMatch数组。

我也希望能够同时搜索许多不同的阵列组合,所以例如具有363 defindex和任19或6 _particleEffect一个数组,这意味着我在使用$

然而,当我试图把$ elemMatch和$在查询时,elemMatch将一无所有,用它做,因为它不会在阵列上运行。我一直没能做到这一点。

我尝试至今:

  {异常:{$所有:[{defindex:{$的:[361378]}},{_ particleEffect:{$的:[30 ,0]}}]}}

(我的最新尝试,根本不起作用。)

<$p$p><$c$c>{\"$and\":[{\"unusual.defindex\":{\"$in\":[361,378]}},{\"unusual._particleEffect\":{\"$in\":[[30,36]]}}]}

和更多的,我试着用$ elemmatch和$和组合的负载。

(发现异常数组中的项,但忽略阵列划界的IE它将返回,其中多个项目将被用来满足条件(因此与匹配一个defindex并具有效果的一个项目的至少一个项目的文件)。 )

我已经花了一天半的时间就这个问题和都来真的很远,甚至发现这几乎和我一样的问题,但缺少的一部分$的任何提及。 - > 的MongoDB:匹配多个数组元素

TL;博士:有没有办法在+ $ elemMatch,切实做好$

感谢您阅读,并能读我有些格式错误后,表示感谢。


解决方案

您可以使用不同的语法比你想的实现同样的结果,但不会碰上SERVER-3544的限制之一。

使用此语法:

  db.collection.find({不寻常:{$ elemMatch:{defindex:363,_ particleEffect:在${:[6,19 ]}}}})

这将匹配与两个313和6或19数组元素的任何文件。

它还可以与 {$于:[]} 为defindex和_particleEffect,只要你打算将两个列表的任意组合搭配。

  db.collection.find({不寻常:{$ elemMatch:{defindex:{$的:[313363]},_ particleEffect:{ $在:[6,19]}}}})

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.

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.

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.

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.

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.

(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.))

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

tl;dr: is there a way to effectively do $in + $elemMatch?

Thanks for reading and being able to read my somewhat badly formatted post, thanks.

解决方案

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.

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.

It also works with {$in:[]} for both defindex and _particleEffect, as long as you intend to match any combination of the two lists.

db.collection.find({ "unusual": {"$elemMatch":{"defindex":{"$in":[313,363]},"_particleEffect":{"$in":[6,19]}  }} })

这篇关于MongoDB的$ elemMatch在$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆