MongoDB使用嵌套文档查找查询

时间:2017-09-06 11:10:51

标签: node.js mongodb mongoose mongoose-schema

我的MongoDB中有以下文档结构:

[{
  "_id": "59afce079af65f48de8fb609",
  "some_fields": "...",
  "members": [
    {
      "some_fields": "...",
      "device_id": "7FD45D19-F746-4293-A410-60B485E617F2",
      "_id": "59afce079af65f48de8fb60a"
    },
    {
      "some_fields": "...",
      "device_id": "7AC1635B-3E39-4F43-A4D4-4927FA072F89",
      "_id": "59afd01cff27894e8237eb2d"
    }
  ]
}]

如何使用Mongoose查找documents which members contain object where device_id == "given_id"

1 个答案:

答案 0 :(得分:0)

尝试这个:

使用elemMatch:

db.collection.find({members: {$elemMatch: {device_id:'your ID'}}})