4

I have the below collections in mongodb

db.orders.find()

{ "_id" : ObjectId("5cc69ad493297eade15bacb7"), "item" : "card", "qty" : 15, "user_mob" : 8220097 }
{ "_id" : ObjectId("5cc69adf93297eade15bacb8"), "item" : "card1", "qty" : 11, "user_mob" : 8220097 }

db.items.find()

{ "_id" : ObjectId("5cc69ba493297eade15bacbc"), "item" : "card1", "color" : "blue", "weight" : "50g" }
{ "_id" : ObjectId("5cc69bb793297eade15bacbd"), "item" : "card", "color" : "yellow", "weight" : "60g" }
{ "_id" : ObjectId("5cc69bd193297eade15bacbe"), "item" : "ball", "color" : "multi color", "weight" : "140g" }
{ "_id" : ObjectId("5cc69be793297eade15bacbf"), "item" : "footer", "color" : "Black", "weight" : "340g" }
{ "_id" : ObjectId("5cc69c0c93297eade15bacc0"), "item" : "caps", "color" : "multi color", "weight" : "250g" }

db.users_nippon.find()

{ "_id" : ObjectId("5cadf1d9b0a2d18bdc6de90a"), "name" : "hariharan", "mobile_no" : "8220097", "role_id" : "2", "language_code" : "ml", "encrypted_password" : "password_Passw0rd", "is_salesman" : "true", "is_mobile" : "true" }

And I write code in node js to fetch data from all the above tables. The code is...

db.collection('users_nippon').aggregate([
            {
                $lookup: {
                   from: "orders",
                   localField: "mobile_no",
                   foreignField: "user_mob",
                   as: "orders_data"
                }
            },
            {
                $unwind: "$orders_data"
            },
            {
                $lookup: {
                    from: "items",
                    localField: "item",
                    foreignField: "item",
                    as: "items_data"
                }
            },
            {
                $unwind: "$items_data"
            }
        ]).toArray(function(err, list) {
            if (err) throw err;
            console.log(JSON.stringify(list));
            res.send(JSON.stringify(list));
            });

There is something went wrong. It returns empty ([ ]).

so , please help me to fix this issue... Thanks in advance.

Below code is the sample for The output which i want

[
{
    "_id": "5cc67e439c26e35a70fddfd5",
    "name": "hariharan",
    "mobile_no": "8220097",
    "role_id": "2",
    "language_code": "ml",
    "encrypted_password": "password_Passw0rd",
    "is_salesman": "true",
    "is_mobile": "true",
    "orders_data": {
        "_id": "5cc6ebe9f4c1d9080b93b013",
        "item": "card",
        "qty": 15,
        "user_mob": "8220097",
        "items_data": {
                "_id": "5cc69bb793297eade15bacbd",
                "item": "card",
                "color": "yellow",
                "weight": "60g"
          }
    }

}]
8
  • are you getting null on this console.log(JSON.stringify(list)); ? Commented Apr 29, 2019 at 11:37
  • 1
    no. I getting empty array [ ] Commented Apr 29, 2019 at 11:39
  • Remove JSON.Stringify and two $unwind stages from the above code and check the response.
    – Ashh
    Commented Apr 29, 2019 at 11:45
  • 1
    it returns some data. but , the structure of the return data not in format. [ { "_id": "5cadf1d9b0a2d18bdc6de90a", "name": "hariharan", "mobile_no": "8220097", "role_id": "2", "language_code": "ml", "encrypted_password": "password_Passw0rd", "is_salesman": "true", "is_mobile": "true", "orders_data": [], "items_data": [] } } Commented Apr 29, 2019 at 11:58
  • 1
    Are your collections linked, can you show us ur users_nippon model
    – noitse
    Commented Apr 29, 2019 at 12:11

2 Answers 2

2

There are 2 issues in your query:

  1. The field user_mob (orders schema) is a Number while mobile_no (user_nippon schema) is a String therefore the lookup cannot be made. You might want to use the same type for those fields.

  2. The second lookup is incorrect. the first lookup and unwind returns your item element inside the orders_data element so the localField of this lookup should be changed to: orders_data.item

So after you change both user_mob and mobile_no to have matching types your query would look like this:

db.collection('users_nippon').aggregate([
            {
                $lookup: {
                   from: "orders",
                   localField: "mobile_no",
                   foreignField: "user_mob",
                   as: "orders_data"
                }
            },
            {
                $unwind: "$orders_data"
            },
            {
                $lookup: {
                    from: "items",
                    localField: "orders_data.item",
                    foreignField: "item",
                    as: "items_data"
                }
            },
            {
                $unwind: "$items_data"
            }
        ]).toArray(function(err, list) {
            if (err) throw err;
            console.log(JSON.stringify(list));
            res.send(JSON.stringify(list));
            });

1
  • 1
    Your code is useful.. it give me some output. but i need the output in different Json format.. please check the question for the output format. Thank you Commented Apr 29, 2019 at 12:27
1

You have to change data-type of mobile_no in users_nippon collection from string to NumberLong otherwise $lookup will not work. After correcting it, now you can use below aggregation query to get your desired result in the same format you want.

db.collection('users_nippon').aggregate([

        {
            $lookup: {
              from: "orders",
              localField: "mobile_no",
              foreignField: "user_mob",
              as: "orders_data"
            }
        },
        {
            $unwind: "$orders_data"
        },
        {
            $lookup: {
                from: "items",
                localField: "orders_data.item",
                foreignField: "item",
                as: "orders_data.items_data"
            }
        },
        {
            $unwind: "$orders_data.items_data"
        }
    ]).toArray(function(err, list) {
        if (err) throw err;
        console.log(JSON.stringify(list));
        res.send(JSON.stringify(list));
        });

this is the tested and working solution.

1
  • now it's working bro. Thanks for your answer. You save me. Thanks alot. Commented Apr 30, 2019 at 5:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.