查询嵌套数组

时间:2019-05-10 00:46:34

标签: node.js mongodb subdocument

我怎么只能找到一个与我的过滤器匹配的文档

machine_name:ABT6,处理天数:33

我只需要退还一份文件。

[ {     “ _id”:“ 5a7ca2227c42ac67682731d5”,     “ machine_name”:“ ABT6”,     “过程”:[         {             “ _id”:“ 5a2ca22b7c42ad67682731d5”,
            “天”:33,             “活动”:“前往电梯1”
        },         {             “ _id”:“ 5a2ca72b0a1aa173aaae07da”,             “天数”:是的,             “活动”:“前往电梯4”         },         {             “ _id”:“ 5a2cabf85a41077a2f87d4e3”,
            “天数”:false,             “活动”:“前往电梯4”         }     ] } ]

1 个答案:

答案 0 :(得分:0)

您需要添加投影:$ (projection)

db.collectionName.find({ machine_name : "ABT6","process.days" : 33},{"process.$" : 1,machine_name: 1}).pretty()

还带有$ elemMatch:

db.collectionName.find({ machine_name : "ABT6","process": { $elemMatch : { days: 33 }}},{"process.$" : 1,machine_name: 1}).pretty()