6

I'm trying to make a query by merging from another collection, but there are obstacles when the query is run, the data generated is not what I imagined

i have the data like this

{
        "_id": "5ce8981a46039c14a4ec32d1",
        "name": "Monkey D Luffy",
        "email": "[email protected]",
        "status": "not verified",
        "password": "$2a$10$ayluBIsOOelBTIk.69GjHubgQemr6dJfgBUELNusCOaUGLpS/qKs6",
        "metas": {
            "role": "admin",
            "smartphone": "ios",
            "address": "konoha",
            "hobby": "eat ramen"
        }
    },

and i want pull out metas from nested document :

{
        "_id": "5ce8981a46039c14a4ec32d1",
        "name": "Monkey D Luffy",
        "email": "[email protected]",
        "status": "not verified",
        "password": "$2a$10$ayluBIsOOelBTIk.69GjHubgQemr6dJfgBUELNusCOaUGLpS/qKs6",
        "role": "admin",
        "smartphone": "ios",
        "address": "konoha",
        "hobby": "eat ramen"
    },

if any duplicate from my question pls suggest me, because I didn't find the same question, mostly using arrays.

and here is my query:

db.accounts.aggregate([
    {
        $lookup: {
            from: "account_meta",
            localField: "_id", 
            foreignField: "account_id",
            as: "metas"
        }
    },

    { "$unwind": "$metas" },
    {
        $group: {
            _id: "$_id",
            name: {$first:"$name"},
            status: {$first: "$status"},
            email: {$first: "$email"},
            password: {$first: "$password"},
            data: {
                "$push": {
                    "k" : "$metas.key",
                    "v": "$metas.value"
                }
            }
        }
    },
    {
        $project: {
            "_id": "$_id", 
            "name": "$name", 
            "email": "$email", 
            "status": "$status", 
            "password": "$password",
            "metas" :{
                $arrayToObject: "$data"
            }
        }
    },
    {
        "$replaceRoot": {

            "newRoot":

                {
                    "$mergeObjects": [ {$arrayToObject: "$data"}, "$$ROOT"]
                },

        }
    },
])
8
  • solved xD i just change "$mergeObjects": [ '$metas', "$$ROOT"] and added $project : {metas: 0} after $replaceRoot Commented May 25, 2019 at 4:36
  • Please post answers as answers, not comments. Also, don't edit the question title; just mark the answer as "accepted". Commented May 25, 2019 at 5:10
  • mark answer as accepted? how to do it? Commented May 25, 2019 at 5:49
  • Well, first you need to actually post an answer. Again, please stop editing your question; just post a separate answer. Commented May 25, 2019 at 5:51
  • As for accepting answers, see meta.stackexchange.com/questions/5234/…. You may have to wait two days to be able to accept your own answer. Commented May 25, 2019 at 5:55

1 Answer 1

3

i just edit some code from my $mergeObject:

{
        "$replaceRoot": {

            "newRoot":

                {
                    "$mergeObjects": [ "$metas", "$$ROOT"]
                },

        }
},
{$project: { metas: 0} }
Sign up to request clarification or add additional context in comments.

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.