Find one document from whole collection, with one specific value nested inside multiple embedded sub documents

怎甘沉沦 提交于 2019-12-11 16:23:59

问题


I need to find one document which matches the unique criteria from query.

Below is example data - Please note we have to find an email from the document where "verificationCode" : "4146" Let me know if you need me to explain my question in more details.

   {
        "_id": ObjectId("58dc5b9f3107602dbaba1280"),
        "email": "test1@gmail.com",
        "password": "password",
        "userName": "test1",
        "companyName": "W company",
        "apiKey": "JSAD&unjSAD7821SADnosad&",
        "apiCallCount": 0,
        "buildings": [
           {
              "_id": ObjectId("528b7c7b594d11167ecdd1a6"),
              "buildingName": "TD Bank Tower",
              "address": "66 Wellington Street West",
              "suite": "3300",
              "floor": "48th floor",
              "timeZone": "America/Toronto",
              "gateways": [
                 {
                    "_id": ObjectId("5d254b51f9a35ec2e3926e9d"),
                    "gatewayName": "TD-xaka-001",
                    "gatewayKey": "gk_d0202d93d0d29293d9",
                    "suite": "Xk1",
                    "devices": [
                       {
                          "_id": ObjectId("5d25f9d2dc4aea7838b0aa9f"),
                          "serialNumber": "100000100213",
                          "area": "Meeting room 2",
                          "connectionStatus": 0,
                          "gatewayKey": "gk_d0202d93d0d29293d9",
                          "applicationNumber": 31,
                          "firmwareVersion": "v12.1.8",
                          "needsAttention": true,
                          "createdAt": new Date(1492305720000),
                          "updatedAt": new Date(1492305720000)
                       }
                    ],
                    "createdAt": new Date(1492305720000),
                    "updatedAt": new Date(1492305720000)
                 }
              ],
              "createdAt": new Date(1492305720000),
              "updatedAt": new Date(1492305720000)
           },
           {
              "_id": ObjectId("5d254bf5f57660050561dba1"),
              "buildingName": "TD North Tower",
              "address": "70 Wellington Street West",
              "suite": "2200",
              "floor": "12th floor",
              "timeZone": "America/Toronto",
              "gateways": [
                 {
                    "_id": ObjectId("5d254bfb6323fbd9e0fafe11"),
                    "gatewayName": "TD-xaka-002",
                    "gatewayKey": "gk_d0202d93d0d29293d1",
                    "suite": "xk2",
                    "devices": [
                       {
                          "_id": ObjectId("5d25f9d2dc4aea7838b0aa9e"),
                          "serialNumber": "100000100212",
                          "area": "Meeting room 1",
                          "connectionStatus": 0,
                          "gatewayKey": "gk_d0202d93d0d29293d1",
                          "applicationNumber": 50,
                          "firmwareVersion": "v12.1.8",
                          "verificationCode": "4146",
                          "createdAt": new Date(1492305720000),
                          "updatedAt": new Date(1492305720000)
                       },
                       {
                          "_id": ObjectId("5d4c5f4b3e0c539469fb20a2"),
                          "serialNumber": "100000100218",
                          "area": "Kitchen",
                          "connectionStatus": 1,
                          "gatewayKey": "gk_d0202d93d0d29293d1",
                          "applicationNumber": 50,
                          "firmwareVersion": "v12.1.8",
                          "needsAttention": true,
                          "createdAt": new Date(1492305720000),
                          "updatedAt": new Date(1492305720000)
                       }
                    ],
                    "createdAt": new Date(1492305720000),
                    "updatedAt": new Date(1492305720000)
                 }
              ],
              "createdAt": new Date(1492305720000),
              "updatedAt": new Date(1492305720000)
           }
        ],
        "createdAt": new Date(1492305720000),
        "updatedAt": new Date(1492305720000)
     }
     {
        "_id": ObjectId("58dc5b9f3107602dbaba1281"),
        "email": "test2@gmail.com",
        "password": "password",
        "userName": "test2",
        "companyName": "test2 company",
        "apiKey": "Hhd87AD&jasd90dlK09aosdO",
        "apiCallCount": 0,
        "buildings": [
           {
              "_id": ObjectId("5d254bb179584ebcbb68b712"),
              "buildingName": "RBC Bank Tower",
              "address": "1 Front Street East",
              "suite": "110",
              "floor": "2nd floor",
              "timeZone": "America/Toronto",
              "gateways": [
                 {
                    "_id": ObjectId("5d254b64ba574040d9632ada"),
                    "gatewayName": "RBC-xaka-001",
                    "gatewayKey": "gk_d0202d93d0d29293a1",
                    "suite": "RBC1",
                    "devices": [
                       {
                          "_id": ObjectId("5d25f9d2dc4aea7838b0aaa1"),
                          "serialNumber": "100000100215",
                          "area": "Office 1",
                          "connectionStatus": 0,
                          "gatewayKey": "gk_d0202d93d0d29293a1",
                          "applicationNumber": 40,
                          "firmwareVersion": "v12.1.8",
                          "createdAt": new Date(1492305720000),
                          "updatedAt": new Date(1492305720000)
                       },
                       {
                          "_id": ObjectId("5d25f9d2dc4aea7838b0aaa2"),
                          "serialNumber": "100000100216",
                          "area": "Office 2",
                          "connectionStatus": 0,
                          "gatewayKey": "gk_d0202d93d0d29293a1",
                          "applicationNumber": 41,
                          "firmwareVersion": "v12.1.8",
                          "createdAt": new Date(1492305720000),
                          "updatedAt": new Date(1492305720000)
                       },
                       {
                          "_id": ObjectId("5d25f9d2dc4aea7838b0aaa3"),
                          "serialNumber": "100000100217",
                          "area": "Office 4",
                          "connectionStatus": 0,
                          "gatewayKey": "gk_d0202d93d0d29293a1",
                          "applicationNumber": 60,
                          "firmwareVersion": "v12.1.8",
                          "createdAt": new Date(1492305720000),
                          "updatedAt": new Date(1492305720000)
                       }
                    ],
                    "createdAt": new Date(1492305720000),
                    "updatedAt": new Date(1492305720000)
                 },
                 {
                    "_id": ObjectId("5d254b7174d7b3a73dff342f"),
                    "gatewayName": "RBC-xaka-002",
                    "gatewayKey": "gk_d0202d93d0d29293a2",
                    "suite": "RBC2",
                    "devices": [
                       {
                          "_id": ObjectId("5d25f9d2dc4aea7838b0aaa0"),
                          "serialNumber": "100000100214",
                          "area": "Office 3",
                          "connectionStatus": 0,
                          "gatewayKey": "gk_d0202d93d0d29293a2",
                          "applicationNumber": 60,
                          "firmwareVersion": "v12.1.8",
                          "createdAt": new Date(1492305720000),
                          "updatedAt": new Date(1492305720000)
                       }
                    ],
                    "createdAt": new Date(1492305720000),
                    "updatedAt": new Date(1492305720000)
                 }
              ],
              "createdAt": new Date(1492305720000),
              "updatedAt": new Date(1492305720000)
           }
        ],
        "createdAt": new Date(1492305720000),
        "updatedAt": new Date(1492305720000)
     }

I am trying but no luck yet with anything close for this solution.

UPDATE: I can find the sub collection with "verificationCode" 4146 now but I want to get whole document instead of just sub document.

const accounts = await Account.find();
const buildings = _.flatMap(accounts, a => a.buildings)
const gateways = _.flatMap(buildings, b => b.gateways);
const devices = _.flatMap(gateways, g => g.devices);
const device = _.filter(devices, d => d.verificationCode === '4146');

Above code returns only filtered data. I need to get _id or email which is at top of the document.


回答1:


you can use mongodb aggregation to get exact output as required by

Account..aggregate([
{$match:{'buildings.gateways.devices.verificationCode':"4146"}},
{$unwind:"$buildings"},
{$unwind:"$buildings.gateways"},
{$unwind:"$buildings.gateways.devices"},
{$match:{'buildings.gateways.devices.verificationCode':"4146"}}
])



回答2:


I found more elegant solution i think.

const account = await Account.find({ "buildings.gateways.devices.verificationCode":   code  })
const accountId = account.map(item => item._id)


来源:https://stackoverflow.com/questions/59206844/find-one-document-from-whole-collection-with-one-specific-value-nested-inside-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!