I have 2 collections in mongodb 4.4.0. I have a video collection that contains all the videos and some related fields, and a collection called users where I store all the users and a list of videos they have completed, including the notes they have placed on the individual video. Now I have to get a list of all the videos, combined with the fields of the videos contained in the user collection, only obviously in cases where there is correspondence
Videos
{
"_id": ObjectId("aaaaaaaaaaaaaaaaaaaaaa01"),
"title": "Video title 1",
"duration" : 120,
"author" : "John Doe"
},
{
"_id": ObjectId("aaaaaaaaaaaaaaaaaaaaaa02"),
"title": "Video title 2",
"duration" : 180,
"author" : "Maria Hernandez"
},
{
"_id": ObjectId("aaaaaaaaaaaaaaaaaaaaaa03"),
"title": "Video title 3",
"duration" : 75,
"author" : "Henry Ford"
}
Users (only one for demo purpose)
{
"_id": ObjectId("bbbbbbbbbbbbbbbbbbbbbb01"),
"fullname": "James Smith",
"videos":
[
{
"video_id": ObjectId("aaaaaaaaaaaaaaaaaaaaaa01"),
"views": 12,
"completed": true,
"notes": "very fun video",
"tags": ["fun", "best"]
},
{
"video_id": ObjectId("aaaaaaaaaaaaaaaaaaaaaa02"),
"views": 64,
"completed": false,
"notes": "very difficult to understand",
"tags": ["hard", "training", "sport"]
}
]
}
Desired result merged objects, assuming I'm filtering for user bbbbbbbbbbbbbbbbbbbbbb01:
{
"_id": ObjectId("bbbbbbbbbbbbbbbbbbbbbb01"),
"fullname": "James Smith",
"videos":
[
{
"video_id": ObjectId("aaaaaaaaaaaaaaaaaaaaaa01"),
"title": "Video title 1",
"duration" : 120,
"author" : "John Doe"
"views": 12,
"completed": true,
"notes": "very fun video",
"tags": ["fun", "best"]
},
{
"video_id": ObjectId("aaaaaaaaaaaaaaaaaaaaaa02"),
"title": "Video title 2",
"duration" : 180,
"author" : "Maria Hernandez"
"views": 64,
"completed": false,
"notes": "very difficult to understand",
"tags": ["hard", "training", "sport"]
},
{
"video_id": ObjectId("aaaaaaaaaaaaaaaaaaaaaa03"),
"title": "Video title 3",
"duration" : 75,
"author" : "Henry Ford"
}
]
}
I searched a lot on google and SO for example: MongoDB - $lookup in complex nested array or Merge two array objects together in Mongodb from $Lookup
I'm trying to use $ lookup and $ mergeObjects but I just can't get the result I want.