gpt4 book ai didi

node.js - 发送 MongoDB 数组以从 MongoDB 中的不同集合获取结果

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

我有一个包含考勤卡的 MongoDB 集合。我有第二个包含位置的集合。通常,如果我正在寻找当时值类的人,我会这样做(每次登录都必须注销):

TimeCard.aggregate([
{ $match: {agency_id: req.query.agency_id}},
{ $sort: { user_id: 1, received_time: -1 }},
{ $group: { _id: "$user_id", count_types: { $sum: 1 }, lastTime: { $last: "$received_time" }}},
{ $match: { count_types: { $mod: [2, 1] }}},
{ $lookup: { from: "locations", localField: "_id", foreignField: "user_id", as: "Test" }}
], function(err, docs){
if(err){console.log(err);}
else {res.json(docs);}
});

这会给我这个结果:

 [
{
"_id": "123-88",
"count_types": 5,
"lastTime": "2017-04-20T01:30:18.713Z",
"Test": [
{
"_id": "58fa4021ffa99100116585e0",
"user_id": "123-88",
"agency_id": "44",
"contract_region_id": "contract-region-id-007",
"department_id": "department-id-42",
"shift_id": "shift-id-45",
"unit_id": "unit-id-88",
"received_time": "2017-04-21T17:23:45.672Z",
"location": "Science Lab",
"__v": 0
},
{
"_id": "58fed3efdac1bd00112a914b",
"user_id": "123-88",
"agency_id": "44",
"contract_region_id": "contract-region-id-007",
"department_id": "department-id-42",
"shift_id": "shift-id-45",
"unit_id": "unit-id-88",
"received_time": "2017-04-25T04:43:27.501Z",
"location": "Gym",
"__v": 0
}
]
}
]

现在我可以在数组中拥有多个用户,并且每个用户都有自己的位置数据。我想要的只是他们所在的最后一个位置(基于测试数组中的 receive_time )。所以我最好的猜测是,我需要首先获取 user_ids 列表,然后调用第二个集合并传入数组以获取结果,但我不确定如何正确执行此操作,或者这是否是最好的方法去做这个。再次感谢您的帮助。

最佳答案

使用$filter 运算符获取 Test 数组中 received_time 键与聚合 lastTime 字段匹配的唯一元素。您可以在 $addFields 中应用此功能 如果您的 MongoDB Server 版本是 3.4 或更高版本,则管道步骤:

TimeCard.aggregate([
{ $match: {agency_id: req.query.agency_id}},
{ $sort: { user_id: 1, received_time: -1 }},
{ $group: { _id: "$user_id", count_types: { $sum: 1 }, lastTime: { $last: "$received_time" }}},
{ $match: { count_types: { $mod: [2, 1] }}},
{ $lookup: { from: "locations", localField: "_id", foreignField: "user_id", as: "Test" }},
{
$addFields: {
Test: {
$filter: {
input: "$Test",
as: "user",
cond: { $eq: ["$lastTime", "$$user.received_time"] }
}
}
}
}
], function(err, docs){
if(err){console.log(err);}
else {res.json(docs);}
});
<小时/>

如果您的 MongoDB 服务器不支持 $addFields 然后使用 $project 管道:

TimeCard.aggregate([
{ $match: {agency_id: req.query.agency_id}},
{ $sort: { user_id: 1, received_time: -1 }},
{ $group: { _id: "$user_id", count_types: { $sum: 1 }, lastTime: { $last: "$received_time" }}},
{ $match: { count_types: { $mod: [2, 1] }}},
{ $lookup: { from: "locations", localField: "_id", foreignField: "user_id", as: "Test" }},
{
$project: {
count_types: 1,
lastTime: 1,
Test: {
$filter: {
input: "$Test",
as: "user",
cond: { $eq: ["$lastTime", "$$user.received_time"] }
}
}
}
}
], function(err, docs){
if(err){console.log(err);}
else {res.json(docs);}
});

关于node.js - 发送 MongoDB 数组以从 MongoDB 中的不同集合获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43602307/

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