0

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.

3
  • are these collections in the same database? Commented Jan 27, 2018 at 23:32
  • Yes, these collections exists in the same db Commented Jan 28, 2018 at 7:08
  • @EhsanFarahaniAsil you can use $lookup aggregate pipeline for joining with other collection Commented Jan 28, 2018 at 7:36

3 Answers 3

1

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()
Sign up to request clarification or add additional context in comments.

6 Comments

yes, the aggregation returns cursor with this format
how to limit users information field? i mean return only these field (firstName,lastName,username)?
see $project documentation to exclude embedded fields
thanks. i used this query ({$project : {likeUsersInfo :0,reviews.likeUsers.password:0}}) but after run it, return error (Error: Line 13: Unexpected token .)
enclose fields with double quotes
|
0

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

1 Comment

you should post this in a new question? you should not add/update answered questions?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.