简体   繁体   中英

MongoDB nested aggregation (join)

I have one document that I want to run nested aggregation on this, but I don't know how to run this operation.

Content document (removed unnecessary fields):

{
    "_id" : ObjectId("5a6b8b734f1408137f79e2cc"),
    "reviews" : [ 
        {
            "_id" : ObjectId("5a6cf7c41562160494238781"),
            "headline" : "",
            "body" : "",
            "likeUsers" : [ 
                {
                    "_id" : ObjectId("5a6b5f2e1fc2a11c5c83a6e2")
                }
            ],
            "dislikeUsers" : [],
            "isCritic" : false,
            "isSpoilers" : false,
            "isTop" : true,
            "rate" : 3,
            "userId" : ObjectId("5a6b5f2e1fc2a11c5c83a6e2"),
            "date" : Timestamp(1517090823, 1)
        }
    ]
}

user content:

{
    "_id" : ObjectId("5a6b5f2e1fc2a11c5c83a6e2"),
    "username" : "",
    "password" : "",
    "firstname" : "",
    "lastname" : "",
    "email" : "",
    "mobile" : "",
    "tel" : "",
    "nationalCode" : "",
    "gender" : "male",
    "birthdate" : Timestamp(0, 1),
    "promotionCode" : "12131SKSQ",
    "status" : 1,
    "created" : Timestamp(1516986633, 1),
    "updated" : Timestamp(1516986633, 2)
}

I want to get all users information from likeUsers array field.

you can use $lookup aggregation to get the liked user's info as an embedded array

db.content.aggregate(
    [
        {$match : {"_id" : ObjectId("5a6b8b734f1408137f79e2cc")}},
        {
            $lookup : {
                from : "user",
                localField : "reviews.likeUsers._id",
                foreignField : "_id",
                as : "likeUsersInfo"
            }
        }
    ]
)

EDIT-1

to have embedded documents in same hierarchy

db.content.aggregate(
    [
        {$match : {"_id" : ObjectId("5a6b8b734f1408137f79e2cc")}},
        {
            $lookup : {
                from : "user",
                localField : "reviews.likeUsers._id",
                foreignField : "_id",
                as : "likeUsersInfo"
            }
        },
        {$addFields : {"reviews.likeUsers" : "$likeUsersInfo"}},
        {$project : {likeUsersInfo : 0}}
    ]
).pretty()

Thanks very much for your response. is possible that get user information exactly in reviews.likerUsers ? i need return result like this:

{
    "_id" : ObjectId("5a6b8b734f1408137f79e2cc"),
    "reviews" : [ 
        {
            "_id" : ObjectId("5a6cf7c41562160494238781"),
            "headline" : "",
            "body" : "",
            "likeUsers" : [ 
                {
                  "_id" : ObjectId("5a6b5f2e1fc2a11c5c83a6e2"),
                  "username" : "",
                  "firstname" : "",
                  "lastname" : ""
                }
            ],
            "dislikeUsers" : [],
            "isCritic" : false,
            "isSpoilers" : false,
            "isTop" : true,
            "rate" : 3,
            "userId" : ObjectId("5a6b5f2e1fc2a11c5c83a6e2"),
            "date" : Timestamp(1517090823, 1)
        }
    ]
}

finally i can get user information in exactly tags with $addFields and $project command. i added another $project for limited user information fields. operation run success and reviews.likeUsers.[users] fields is limited but contents fields doesn't show.

New query:

db.contents.aggregate(
    // Pipeline
    [
        // Stage 1
        {
            $lookup: // Equality Match
            {
                            from : "users",
                            localField : "reviews.likeUsers._id",
                            foreignField : "_id",
                            as : "likeUsersInfo"
            }
        },

        // Stage 2
        {
            $addFields: {"reviews.likeUsers" : "$likeUsersInfo"}
        },

        // Stage 3
        {
            $project: {likeUsersInfo : 0}
        },

        // Stage 4
        {
            $project: {"reviews.likeUsers.username": 1,"reviews.likeUsers.firstname": 1,"reviews.likeUsers.lastname": 1}
        },

    ]
);

after run above query return below fields and other contents field doesn't show:

{
    "_id" : ObjectId("5a6b8b734f1408137f79e2cc"),
    "reviews" : [ 
        {
            "likeUsers" : [ 
                {
                    "username" : "e.farahani",
                    "firstname" : "Ehsan",
                    "lastname" : "Farahani Asil"
                }
            ]
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM