Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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
Node.js 发送MongoDB数组以从MongoDB中的其他集合获取结果_Node.js_Mongodb - Fatal编程技术网

Node.js 发送MongoDB数组以从MongoDB中的其他集合获取结果

Node.js 发送MongoDB数组以从MongoDB中的其他集合获取结果,node.js,mongodb,Node.js,Mongodb,我有一个MongoDB集合,其中包含时间卡。我有一个包含位置的第二个集合。通常我会这样做,如果我正在寻找任何人在当时是oncall(你必须有一个签出为每一个签名): 这会给我这个结果: [ { "_id": "123-88", "count_types": 5, "lastTime": "2017-04-20T01:30:18.713Z", "Test": [ { "_id": "58fa4021ffa99100116585e0

我有一个MongoDB集合,其中包含时间卡。我有一个包含位置的第二个集合。通常我会这样做,如果我正在寻找任何人在当时是oncall(你必须有一个签出为每一个签名):

这会给我这个结果:

 [
   {
    "_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
      }
    ]
  }
]
现在我可以让阵列中的多个用户都拥有自己的位置数据。我想要的只是它们最后所在的位置(基于测试阵列中接收到的时间)。因此,我的最佳猜测是,我需要首先获得一个用户ID列表,然后调用第二个集合并传入数组以获得结果,但我不确定如何正确执行此操作,或者这是否是执行此操作的最佳方法。再次感谢您的帮助。

使用操作符获取测试数组中唯一的元素,该元素的
接收时间
键与聚合的
上次时间
字段匹配。如果您的MongoDB服务器版本为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服务器不支持,请改用管道:

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

对不起,我不完全明白你要做什么。你能澄清一下吗?当然。我想要的是测试中的内容只有一条记录(最新的一条)。因此,在上面的示例输出中,我只看到位置Gym,而没有看到科学实验室(因为Gym是用户最后一次访问的位置)。我们正在使用MongoDB 3.2,所以我正在尝试$project。一个问题是$lastTime与$$user.received\u时间不匹配。$$user.received_时间可能更晚,因为如果他们移动了位置并选中了,但仍在调用,则他们在user.received_时间中的时间会更晚,但时间卡$lastime不会更改。
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);}
});