【问题标题】:Find one document from whole collection, with one specific value nested inside multiple embedded sub documents从整个集合中查找一个文档,其中一个特定值嵌套在多个嵌入的子文档中
【发布时间】:2020-03-31 01:48:32
【问题描述】:

我需要从查询中找到一个与唯一条件匹配的文档。

以下是示例数据 - 请注意,我们必须从 "verificationCode" : "4146" 的文档中找到一封电子邮件 如果您需要我更详细地解释我的问题,请告诉我。

   {
        "_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)
     }

我正在尝试,但对于此解决方案的任何接近都没有运气。

更新: 我现在可以找到带有“verificationCode”4146 的子集合,但我想获取整个文档而不仅仅是子文档。

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');

以上代码仅返回过滤后的数据。我需要获取文档顶部的 _id 或电子邮件。

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    您可以使用 mongodb 聚合来获得所需的准确输出

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

    【讨论】:

    • 它为我工作。使用聚合而不是查找。如果您只需要匹配文档的 _id 或电子邮件,则可以使用以下解决方案。
    【解决方案2】:

    我认为我找到了更优雅的解决方案。

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

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 2023-01-19
      • 2023-03-07
      • 1970-01-01
      • 2016-11-26
      • 2019-09-19
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多