Monday, 24 April 2017

Sending in a MongoDB Array to get results from a different collection in MongoDB

I have a MongoDB Collection that contains timecards. I have a 2nd collection that contains locations. Normally I would do this if I was looking for anyone that was oncall at the time (you have to have a signout for every signin):

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

And this would give me this result:

 [
   {
    "_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
      }
    ]
  }
]

Now I could have multiple users in the array all with their own location data. What I want is only the Last location they were at (based on received_time inside Test array). So my best guess is that I would need to first just get a list of user_ids and then call the second collection and pass in the array to get the results but I am not sure how to do this correctly or if that is even the best way to do this. Thanks again for your help.



via Justin Yanta

No comments:

Post a Comment