0

I would like to map back the valuse from a $lookup operation to local array field.

Currently, I am only able to create a standalone array field using $lookup, but not able to map the value back inside the original array.

An example of document:

Input

User: { "_id": "58a4fa0e24180825b05e14e9" "events": [ {"eventId": "100", "joined": ISODate("2019-10-04T04:25:44.636Z")}, {"eventId": "1001", "joined": ISODate("2019-10-04T04:25:44.636Z")}, {"eventId": "1002", "joined": ISODate("2019-10-04T04:25:44.636Z")} } Event: { "eventId": "100", "eventName": "Taco Day" } 

Output I want:

User: { "_id": "58a4fa0e24180825b05e14e9" "events": [ {"eventId": "100", "joined": ISODate("2019-10-04T04:25:44.636Z"), eventName: "Taco Day"}, {"eventId": "1001", "joined": ISODate("2019-10-04T04:25:44.636Z")}, {"eventId": "1002", "joined": ISODate("2019-10-04T04:25:44.636Z")} } 
3
  • 1
    Try mongoplayground.net/p/2D2_TNtfIVB Commented Oct 4, 2019 at 6:32
  • @sushantmehta That works! I see that you used the $filter inside a $map. What if I know that for each eventId in User.events, there must be a coresponding event in Event collection. Is there a way I can optimize the query? Commented Oct 8, 2019 at 3:36
  • There's no $$index variable inside $map. I am not able to eliminate the nested operation... Commented Oct 8, 2019 at 4:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.