MongoDB查询数组中的双重嵌套文档 [英] MongoDB Query double nested documents in array

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

问题描述

我想知道如何从数组内的嵌入文档中进行查询.我有以下结构:

I was wondering how I can query from an embedded document inside an array. I have following structure:

{ "targetId" : 2, "metaData" : [ {     "key" : "id",     "value" : 1 },     {     "key" :        "name",     "value" : "Parisa" },     {     "key" : "img",     "value" : {     "imgid" : 1,         "imgName" : "img1" } } ] 

我可以搜索简单的键值,例如 key = id 和 value =1,但我无法根据嵌入文档的值进行搜索,例如key="img"

I could search simple key-values like key = id and value =1, but I could not search based on the values with embedded document e.g. key="img"

我尝试了以下查询,但它不起作用:

I tried following query but it does not work:

db.test.find({"metaData":{$elemMatch:{"key":"img", "value":{"imgid":1}}}})

你能帮我吗!

推荐答案

我认为您查询的值"部分有点不对劲.您需要将文档元素放在标准中:

I think the "value" part of your query is a little off. You need to put the document element in the criteria:

b.test.find({"metaData":{$elemMatch:{"key":"img", "value.imgid":1}}})

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

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