Hi, what is the best way to store lists (like music plyalists or favorites movies) related to an existing user, I think about 2 options:
- Separate collection to store the lists:
db.listCollection = {
idList: 1,
name: "X",
movies: [idMovie1, idMovie2] ,
idUser: 12
}
db.UserCollection = {
idUser: 1,
username: "jane doe",
pwd: "encrypted",
lists: [idList12, idList2]
}
- In the user document:
db.userCollection = {
idUser: 1,
username: "jane doe",
pwd: "encrypted",
lists: [
{ idList: 12, name: "X", movies: [id1, id2...] } ,
{ idList: 13, name: "XZ", movies: [id1, id3...] }
]
}
I went for the second option but manage the movies inside the lists is a bit confusing, especially when trying to remove a movie from specific list. If the answer is option 2 can you help me to remove movies from the list, I know i need an aggregation pipeline (im new workig with aggregations) ,
here it is mine
db.userCollection.aggregate([
{ $match: { "lists._id": 12 } },
{
$project: {
lists: {
$filter: {
input: '$lists',
as: 'list',
cond: { $eq: ['$$list._id', 12] }
}
},_id:0
}
])