Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 从整个集合中查找一个文档,其中一个特定值嵌套在多个嵌入的子文档中_Mongodb_Mongoose_Mongodb Query - Fatal编程技术网

Mongodb 从整个集合中查找一个文档,其中一个特定值嵌套在多个嵌入的子文档中

Mongodb 从整个集合中查找一个文档,其中一个特定值嵌套在多个嵌入的子文档中,mongodb,mongoose,mongodb-query,Mongodb,Mongoose,Mongodb Query,我需要从查询中找到一个与唯一条件匹配的文档 下面是示例数据-请注意,我们必须从“验证代码”的文档中找到一封电子邮件:“4146” 如果你需要我更详细地解释我的问题,请告诉我 { "_id": ObjectId("58dc5b9f3107602dbaba1280"), "email": "test1@gmail.com", "password": "password", "userName": "test1",

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

下面是示例数据-请注意,我们必须从“验证代码”的文档中找到一封电子邮件:“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聚合来获得

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');
Account..aggregate([
{$match:{'buildings.gateways.devices.verificationCode':"4146"}},
{$unwind:"$buildings"},
{$unwind:"$buildings.gateways"},
{$unwind:"$buildings.gateways.devices"},
{$match:{'buildings.gateways.devices.verificationCode':"4146"}}
])

我觉得我找到了更优雅的解决方案

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

这对我有用。使用find的聚合insead。如果您只需要匹配文档的_id或电子邮件,您可以使用以下解决方案。