Friday, 26 May 2017

How to do second lookup based on first lookup output in mongodb?

My query

db.transections.aggregate([     

    {$lookup:{from:"activitylogs", localField:"transectionid", foreignField:"ref_id", as:"user"}}
    {$lookup:{from:"users", localField:"", foreignField:"", as:"userdetails"}}

  ]);

for first lookup i got output like this

{
    "_id" : ObjectId("592566529a8a92e6286b91d8"),
    "transectionid" : "00000P3014000707338",
    "transectionamount" : -11.75,
    "user" : [ 
        {
            "_id" : ObjectId("592565da9a8a92e6286b91d7"),
            "usernmuber" : "969848",
            "created" : "24/05/2017",
            "ref_id" : "00000P3014000707338",

        }
    ]
}


{
    "_id" : ObjectId("59159f8ca2195694e367cd98"),
    "transectionid" : "00000P1005000429886",
    "transectionamount" : -56.6,
    "transectionrewardablevalue" : -56.6,
    "user" : [ 
        {
            "_id" : ObjectId("9da525659a8a92e6286bd791"),
            "usernmuber" : "843203",
            "created" : "25/05/2017",
            "ref_id" : "00000P1005000429886",

        }
    ]

}

i want to add one more lookup to get user name and dob from users collection how can i do this ?

based on first lookup result (usernmuber) i need to do lookup with users (mobilenumber)

users collection

{
    "_id" : ObjectId("592530649a8a92e6286b91d3"),
    "name" : "abcd",
    "mobilenumber" : "843203",
    "email" : "abcd@gmail.com",
    "dob":"12/02/1990",

}

{
    "_id" : ObjectId("530659249a8a92e6286b391d"),
    "name" : "cdfg",
    "mobilenumber" : "969848",
    "email" : "cdfg@gmail.com",
    "dob":"14/09/1989",

}



via koo 005

No comments:

Post a Comment