MongoDB:查询嵌套数组 [英] MongoDB: Query nested arrays

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

问题描述

我一直在阅读旧的答案,试图找到解决方案-但是我没有成功.

I have been reading through old answers here to try to find a solution to this - but I haven't been successful.

我在文档中具有一个嵌套数组的字段,如下所示:

I have a field in a document that has a nested array as follows:

[tags] => Array ( 
       [0] => Array ( [_id] => 5271318ea7d63ba9a25045ad 
                      [name] => photo ) 
       [1] => Array ( [_id] => 526ffafba7d63ba9a25045a9 
                      [name] => beach ) 
       [2] => Array ( [_id] => 5271318ea7d63ba9a25045ac 
                      [name] => landscape ) 
)

我正在寻找搜索查询中具有全部个必需标签ID的文档.我有这个查询,但是它返回所有具有任何 ID的文档.

I'm looking for documents that have all of the required tag id's in the search query. I have this query, but it returns all of the documents that have any of the id's.

{"tags":{"$elemMatch":{"_id":{"$in":["5271318ea7d63ba9a25045ad","526ffafba7d63ba9a25045a9"]}}}}

如果我将查询从 $ in 更改为 $ all ,则不会返回任何内容.

If I change the query from $in to $all it does not return anything.

任何帮助将不胜感激.

推荐答案

尝试一下.使用$in$all的查询不能完全相同.

Try this. Queries using $in and $all don't quite work the same.

{"tags": { $all: [
    {"$elemMatch": {"_id": "5271318ea7d63ba9a25045ad"}},
    {"$elemMatch": {"_id": "526ffafba7d63ba9a25045a9"}}]
}}

这里是一个例子

这篇关于MongoDB:查询嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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