sql >> Databasteknik >  >> NoSQL >> MongoDB

Göra joins i mongodb med tre samlingar?

Okej, du verkar ha en massa problem. Jag ska försöka sammanfatta dem.

Problem 1:den samlade koden du har angett kompileras inte.

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"}}},

Fixa:

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"}}}])

Anta nu att du använder samma data som du har angett.

Problem 2 :{$match : {"bmarks.active": 1}} matchar inte någon post.

Fixa :{$match : {"bmarks.active": 0}}

Problem 3:Inget uppslagsfält definierat {$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 :Inget avkopplingssteg för movie_ids för tidigare uppslagning

Fixa :{$unwind : "$movie_ids"}

Problem 5 :Inget fält för postad tid {$sort: {time_posted: -1}}

Fix :inkludera fält före sortering

Så för att få ihop allt behöver du samla ihop det för att se ut som något nedan för att extrahera kommentarerna för varje film.

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"}}}])

Exempelutdata

{
    "_id": ObjectId("5834ecf7432d92675bde9d83"),
    "comments": [{
        "_id": ObjectId("583d96d7e35f6e9c53c9e894"),
        "movie_id": "dallas00",
        "comment": "what a great movie."
    }, {
        "_id": ObjectId("583d96d7e35f6e9c53c9e895"),
        "movie_id": "dallas00",
        "comment": "awesome movie."
    }]
}

Uppdatering:

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



  1. Filtrera med regex MongoDB C#-drivrutin

  2. hur får man kombinationer av objekt i ett arrayfält i MongoDB?

  3. redis:återställ räknaren varje dag

  4. "IdentityContext" kunde inte hittas (saknar du ett användningsdirektiv eller en monteringsreferens)