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

时间:2017-04-25 05:26:38

标签: node.js mongodb

我有一个包含时间卡的MongoDB Collection。我有第二个包含位置的集合。通常我会这样做,如果我正在寻找当时在线呼叫的任何人(你必须为每次登录注册):

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
      }
    ]
  }
]

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

1 个答案:

答案 0 :(得分:1)

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

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