如何使用mongo Query从嵌套文档中获取数据

时间:2019-01-17 17:18:21

标签: node.js mongodb mongoose aggregation-framework

这是文件。

如何在 branchId 上使用mongoQuery,该值必须为 e63a5343-871c-47ee-8fe6-31d9b7f9fa1b1 ,并且返回的数据仅具有该branchId。

我正在使用

  

db.collection.find({        'checkinDetails.presenceTable':{$ elemMatch:              {'priorityDevice.branchId':“ e63a5343-871c-47ee-8fe6-31d9b7f9fa1b1”}}

{
 "checkinDetails": {
       "userCount": NumberInt("9"),
       "presenceTable": [
        {
        "entity": {
            "entityCategory": "Users",
            "entityType": "User",
            "entityName": "Raghvendra",
            "id": "25d156df-2c7e-4506-a5b0-969bf5d0745b"
        },
        "fromState": "current",
        "priorityDevice": {
            "aclFlag": false,
            "zoneGranularity": NumberInt("300000"),
            "csId": "0577936e-f81c-4386-bae7-0255a28ee374",
            "branchId": "e63a5343-871c-47ee-8fe6-31d9b7f9fa1b1",
            "txPower": NumberInt("-54"),
            "filterRSSI": -63.15999016102085,
            "rssi": NumberInt("-55"),
            "time": "2019-01-17T14:38:11.804Z",
            "zoneId": "e813b2f3-88f8-4111-b2bb-50bb7dfb0ba7",
            "deviceId": "26341b09-9b16-4de4-a9ce-74dd3a858be8"
        }
    },
    {
        "entity": {
            "entityCategory": "Users",
            "entityType": "User",
            "entityName": "Rahul Saini",
            "id": "8f067f5b-6a8f-4237-a760-109303def580"
        },
        "fromState": "current",
        "priorityDevice": {
            "aclFlag": false,
            "zoneGranularity": NumberInt("300000"),
            "csId": "0577936e-f81c-4386-bae7-0255a28ee374",
            "branchId": "e63a5343-871c-47ee-8fe6-31d9b7f9fa1b",
            "txPower": NumberInt("-54"),
            "filterRSSI": -66.44148222664761,
            "rssi": NumberInt("-61"),
            "time": "2019-01-17T14:38:11.803Z",
            "zoneId": "e813b2f3-88f8-4111-b2bb-50bb7dfb0ba7",
            "deviceId": "26341b09-9b16-4de4-a9ce-74dd3a858be8"
        }
    },
    {
        "entity": {
            "entityCategory": "Users",
            "entityType": "User",
            "entityName": "Shubham Saurabh",
            "id": "4f0fe333-3f9f-457f-8046-5476573323fb"
        },
        "fromState": "current",
        "priorityDevice": {
            "aclFlag": false,
            "zoneGranularity": NumberInt("300000"),
            "csId": "0577936e-f81c-4386-bae7-0255a28ee374",
            "branchId": "e63a5343-871c-47ee-8fe6-31d9b7f9fa1b1",
            "txPower": NumberInt("-62"),
            "filterRSSI": -61.489655691672645,
            "rssi": NumberInt("-57"),
            "time": "2019-01-17T14:38:11.799Z",
            "zoneId": "e813b2f3-88f8-4111-b2bb-50bb7dfb0ba7",
            "deviceId": "26341b09-9b16-4de4-a9ce-74dd3a858be8"
        }
    }
]
"type": "presenceTable"
  }

1 个答案:

答案 0 :(得分:0)

关于您的评论。尝试使用此示例以使用MongoDB查询获取数据。
db.collection.find({ checkinDetails: { presenceTable: { priorityDevice : { branchId :"e63a5343-871c-47ee-8fe6-31d9b7f9fa1b1"} } } })
您可以在这里找到更多信息:https://docs.mongodb.com/manual/tutorial/query-embedded-documents/