如何从数组查询子文档

时间:2019-07-02 18:03:03

标签: arrays mongodb

如何查询和仅返回mongodb中具有键值的元素?

db.test.find({'componentStubs': { $all:[ {$elemMatch: {"id":6484} }] } })

成功查询,但返回整个数组。如何返回单个字段

[
  {
    "_id": ObjectId("5d1b8e0411919967a193d6c5"),
    "componentStubs": [
      {
        "id": 6458,
        "name": "DTCP-DDNBEED01A",
        "modelId": 282,
        "modelName": "Elektron EDGE",
        "modelVersion": 18,
        "lastUpdated": 1562029651988
      },
      {
        "id": 6459,
        "name": "DTCP-DDNBEED01B",
        "modelId": 282,
        "modelName": "Elektron EDGE",
        "modelVersion": 18,
        "lastUpdated": 1562029651808
      },
      {
        "id": 6460,
        "name": "DTCP-DDNBEED02A",
        "modelId": 282,
        "modelName": "Elektron EDGE",
        "modelVersion": 18,
        "lastUpdated": 1562029655639
      },
      {
        "id": 6461,
        "name": "DTCP-DDNBEED02B",
        "modelId": 282,
        "modelName": "Elektron EDGE",
        "modelVersion": 18,
        "lastUpdated": 1562029647511
      }          
    ]
  }
]

0 个答案:

没有答案