Okay, sieht aus, als hättest du eine Menge Probleme. Ich werde versuchen, sie zusammenzufassen.
Problem 1:Der von Ihnen bereitgestellte Gesamtcode lässt sich nicht kompilieren.
db.user_movies.aggregate(
{$match : {mobile_no : mobile_no}},
{$unwind: "$movies" },
{$lookup: {from: "movies",localField: "movies",foreignField: "movie_id",as: "bmarks"}},
{$unwind : "$bmarks"},
{$match : {"bmarks.active": 1}},
{$group : { _id : "$_id", movies : {$push : "$bmarks"}, movie_ids: {$push : "$bmarks.movie_id"}}},
{$lookup: {from: "movie_comments", localField: "",foreignField: "movie_id",as: "comments"}},
{$unwind : "$comments"},
{$sort: {time_posted: -1}},
{$group: {_id: '$_id', comments : {$push : "$comments"}}},
Behebung:
db.user_movies.aggregate([
{$match : {mobile_no : mobile_no}},
{$unwind: "$movies" },
{$lookup: {from: "movies",localField: "movies",foreignField: "movie_id",as: "bmarks"}},
{$unwind : "$bmarks"},
{$match : {"bmarks.active": 1}},
{$group : { _id : "$_id", movies : {$push : "$bmarks"}, movie_ids: {$push : "$bmarks.movie_id"}}},
{$lookup: {from: "movie_comments", localField: "",foreignField: "movie_id",as: "comments"}},
{$unwind : "$comments"},
{$sort: {time_posted: -1}},
{$group: {_id: '$_id', comments : {$push : "$comments"}}}])
Gehen Sie nun davon aus, dass Sie dieselben Daten verwenden, die Sie angegeben haben.
Problem 2:{$match : {"bmarks.active": 1}}
stimmt mit keinem Eintrag überein.
Behebung:{$match : {"bmarks.active": 0}}
Problem 3:Kein Suchfeld definiert {$lookup: {from: "movie_comments", localField: "",foreignField: "movie_id",as: "comments"}}
Fix:{$lookup: {from: "movie_comments", localField: "movie_ids",foreignField: "movie_id",as: "comments"}}
Problem 4:Keine Abwicklungsphase für movie_ids für vorherige Suche
Fix :{$unwind : "$movie_ids"}
Problem 5:Kein Zeitbuchungsfeld {$sort: {time_posted: -1}}
Fix:Feld vor dem Sortieren einbeziehen
Also, um alles zusammenzufügen, brauchen Sie ein Aggregat, das wie unten aussieht, um die Kommentare für jeden Film zu extrahieren.
db.user_movies.aggregate([
{$match : {mobile_no : mobile_no}},
{$unwind: "$movies"},
{$lookup: {from: "movies",localField: "movies",foreignField: "movie_id",as: "bmarks"}},
{$unwind : "$bmarks"},
{$match : {"bmarks.active": 0}},
{$group : { _id : "$_id", movies : {$push : "$bmarks"}, movie_ids: {$push : "$bmarks.movie_id"}}},
{$unwind : "$movie_ids"},
{$lookup: {from: "movie_comments", localField: "movie_ids",foreignField: "movie_id",as: "comments"}},
{$unwind : "$comments"},
{$group: {_id: '$_id', comments : {$push : "$comments"}}}])
Beispielausgabe
{
"_id": ObjectId("5834ecf7432d92675bde9d83"),
"comments": [{
"_id": ObjectId("583d96d7e35f6e9c53c9e894"),
"movie_id": "dallas00",
"comment": "what a great movie."
}, {
"_id": ObjectId("583d96d7e35f6e9c53c9e895"),
"movie_id": "dallas00",
"comment": "awesome movie."
}]
}
Aktualisieren:
db.user_movies.aggregate([
{$match : {mobile_no : mobile_no}},
{$unwind: {path: "$movies", includeArrayIndex: "moviePosition"}},
{$sort : {moviePosition:1}},
{$lookup: {from: "movies",localField: "movies",foreignField: "movie_id",as: "bmarks"}},
{$unwind :"$bmarks"},
{$group : {_id : "$_id", movies : {$push : {movie_name:"$bmarks.movie_name", movie_id:"$bmarks.movie_id"} }}},
{$unwind : "$movies"},
{$lookup: {from: "movie_comments", localField: "movies.movie_id",foreignField: "movie_id",as: "comments"}},
{$unwind : "$comments"},
{$group: {_id: "$movies.movie_id", movie_name: {$first:"$movies.movie_name"}, comments : {$push : {comment:"$comments.comment", time_posted:"$comments.time_posted"}}}},
{$sort : {time_posted:-1}},
{$project:{_id:0, movie_id:"$_id", movie_name:1, comments: "$comments.comment"}}
]).pretty();