gpt4 book ai didi

javascript - 我在 mongodb 聚合中使用查找,但它不返回我正在查找的数据

转载 作者:太空宇宙 更新时间:2023-11-03 23:48:06 25 4
gpt4 key购买 nike

这是一个示例建筑集合。

{
_id: 5e4aa096a6b7db2270c65815,
previous_audit: false,
no_of_people: 3,
project_name: 'UNDP',
premise_contact: 'Kanagaraj',
premise_address: 'Delhi',
premise_city: 'Delhi',
premise_state: 'Delhi',
premise_country: 'IN',
latitude: '28.593716',
longitude: '77.222391',
building_name: 'Nemi',
building_type: 'Hospital',
climate_zone: 'Hot & Dry',
no_of_stories: '3',
year_of_occupation: 3,
gross_area: 3333,
gross_area_units: 'Square Meters',
lease_type: 'Annual Rental',
campus_id: 5e4857816560b92a58129e68,
project_name_lower: 'undp',
createdAt: 2020-02-17T14:17:58.892Z,
updatedAt: 2020-02-17T14:17:58.892Z,
__v: 0,
}

在第一个管道阶段,我使用 $match 函数。

Building.aggregate([
{
$match: {
campus_id: {
$in: campus_ids
}
}
},
{
$lookup: {
from: 'manual_meters',
localField: '_id',
foreignField: 'building_name',
as: 'manual_meter'
}
}
])

但是当查找lookup时,它总是在manual_meter中返回空数组。这就是我得到的答复。虽然采集手动电表中包含了建筑物的id,但仍然没有返回手动电表采集。

 {
_id: 5e4857816560b92a58129e72,
previous_audit: false,
project_name: 'UNDP',
premise_contact: 'Kanagaraj',
premise_address: 'Delhi',
premise_city: 'Delhi',
premise_state: 'Delhi',
premise_country: 'IN',
latitude: '28.593716',
longitude: '77.222391',
building_name: 'U.N.House',
building_type: 'Office',
climate_zone: 'Hot & Dry',
no_of_stories: '4',
year_of_occupation: 1970,
gross_area: 6500,
gross_area_units: 'Square Meters',
lease_type: 'Owned',
campus_id: 5e4857816560b92a58129e68,
project_name_lower: 'undp',
createdAt: 2020-02-15T20:41:37.171Z,
updatedAt: 2020-02-15T20:41:37.171Z,
__v: 0,
manual_meter: []
},
{
_id: 5e4aa096a6b7db2270c65815,
previous_audit: false,
no_of_people: 3,
project_name: 'UNDP',
premise_contact: 'Kanagaraj',
premise_address: 'Delhi',
premise_city: 'Delhi',
premise_state: 'Delhi',
premise_country: 'IN',
latitude: '28.593716',
longitude: '77.222391',
building_name: 'Nemi',
building_type: 'Hospital',
climate_zone: 'Hot & Dry',
no_of_stories: '3',
year_of_occupation: 3,
gross_area: 3333,
gross_area_units: 'Square Meters',
lease_type: 'Annual Rental',
campus_id: 5e4857816560b92a58129e68,
project_name_lower: 'undp',
createdAt: 2020-02-17T14:17:58.892Z,
updatedAt: 2020-02-17T14:17:58.892Z,
__v: 0,
manual_meter: []
}
]

下面是一个样本manual_meter集合。

    {"_id":"5e4ad8e1a3833e2bb44b4dcb",
"meter_data : [],
"meter_name":"undp_unh",
"meter_id":"1001",
"meter_type":"energy",
"campus_name":"UNDP",
"building_name":"5e4857816560b92a58129e72",
"floor_name":"common",
"zone_name":"common",
"createdAt":"2020-02-17T18:18:09.192Z",
"updatedAt":"2020-02-17T18:18:09.192Z",
"__v":0}

最佳答案

看起来您正在尝试将 stringObjectId 进行匹配,即使值相同,这也不起作用。您可以运行$toString作为 $lookup 的一部分在您的 _id 字段上:

{
$lookup: {
from: "manual_meters",
let: { building_id: { $toString: "$_id" } },
pipeline: [
{
$match:{
$expr: {
$eq: [ "$$building_id", "$building_name" ]
}
}
}
],
as: "manual_meter"
}
}

关于javascript - 我在 mongodb 聚合中使用查找,但它不返回我正在查找的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60286384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com