从整个集合中查找一个文档,其中一个特定值嵌套在多个嵌入式子文档中 [英] Find one document from whole collection, with one specific value nested inside multiple embedded sub documents
问题描述
我需要找到一个与查询中的唯一条件匹配的文档.
I need to find one document which matches the unique criteria from query.
下面是示例数据-请注意,我们必须从文档中找到一封电子邮件,其中"verificationCode":"4146" 让我知道您是否需要我详细解释我的问题.
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.
更新: 我现在可以找到带有"verificationCode" 4146的子集合,但是我想获取整个文档而不是仅仅子文档.
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');
以上代码仅返回经过过滤的数据.我需要在文档顶部获取_id或电子邮件.
Above code returns only filtered data. I need to get _id or email which is at top of the document.
推荐答案
我发现了更优雅的解决方案.
I found more elegant solution i think.
const account = await Account.find({ "buildings.gateways.devices.verificationCode": code })
const accountId = account.map(item => item._id)
这篇关于从整个集合中查找一个文档,其中一个特定值嵌套在多个嵌入式子文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!