-1

i have 4 collections like

division,

category,

productgroup,

product

these all are my data's which is stored in collection my division collecion data's

{

    "divisionid": "1",
    "divisioncode": "0",
    "divisionname": "ELECTRONICS/HOME APPLIANCE",
    "divisionpoint": "2"
},
{

    "divisionid": "2",
    "divisioncode": "1",
    "divisionname": "FOODS",
    "divisionpoint": "8"
}

category collection data's

{

    "categoryid": "1",
    "divisionid": "1",
    "categorycode": "34",
    "categoryname": "AUDIO SYSTEM",
    "categorypoint": "Null"
},
{

    "categoryid": "2",
    "divisionid": "1",
    "categorycode": "348",
    "categoryname": "DVD/VCD",
    "categorypoint": "8"
}

productgroup collection data's

{

    "productgroupid": "1",
    "divisionid": "1",
    "categoryid": "1",
    "productgroupname": "ADAPTOR",
    "productgroupcode": "6765",
    "productgrouppoint": "7"
},
{

    "productgroupid": "2",
    "divisionid": "1",
    "categoryid": "2",
    "productgroupname": "WALKMAN",
    "productgroupcode": "7659",
    "productgrouppoint": "Null"
}

product collection data's

{

    "productid": "1",
    "divisionid": "1",
    "categoryid": "1",
    "productgroupid":"1",
    "productname":"UNIVERSAL AC DC ADAPTER-PCS",
    "productcode": "1000054",
    "productpoint": "1"
},
{

    "productid": "2",
    "divisionid": "1",
    "categoryid": "2",
    "productgroupid":"2",
    "productname":"WALKMAN WM#M470-PCS",
    "productcode": "1000089",
    "productpoint": "2"
}

i want to combine these 4 collection into one collection.

my expectation result:

productsummary
{

    "product": {
        "point": "1",
        "name": "UNIVERSAL AC DC ADAPTER-PCS",
        "code": "10000054",
        "id"  :"1"
    },
    "group": {
        "point": "7",
        "name": "ADAPTOR",
        "id"  :"1"
    },
    "category": {
        "point": "0",
        "name": "AUDIO SYSTEM",
        "id"  :"1"
    },
    "division": {
        "point": "2",
        "name": "ELECTRONICS/HOME APPLIANCE",
        "id"  :"1"
    }
},
{

    "product": {
        "point": "2",
        "name": "WALKMAN WM#M470-PCS",
        "code": "1000089",
        "id"  :"2"
    },
    "group": {
        "point": "7",
        "name": "WALKMAN",
        "id"  :"Null"
    },
    "category": {
        "point": "8",
        "name": "DVD/VCD",
        "id"  :"2"
    },
    "division": {
        "point": "2",
        "name": "ELECTRONICS/HOME APPLIANCE",
        "id"  :"1"
    }
}

when i do below ansered code i m getting error

enter image description here

its me
  • 524
  • 6
  • 28
  • 62
  • You can do this via aggregation and lookup for references see http://stackoverflow.com/questions/35813854/how-to-join-multiple-collections-with-lookup-mongodb and http://www.clusterdb.com/mongodb/joins-and-other-aggregation-enhancements-in-mongodb-3-2 try and comment here if you face any problems. – Vinod Louis Dec 06 '16 at 06:21
  • m new to this technology pls can u help me out @VinodLouis – its me Dec 06 '16 at 06:42

1 Answers1

2

You can use multiple $lookup to get data from multiple collection. say you run query from product collection. query like

db.products.aggregate([ 
    {$lookup:{from:"productgroups", localField:"productgroupid", foreignField:"productgroupid", as:"group"}},
    {$lookup:{from:"category", localField:"categoryid", foreignField:"categoryid", as:"category"}},
    {$lookup:{from:"divisions", localField:"divisionid", foreignField:"divisionid", as:"division"}},
    {$project: {productpoint:1,productname:1,productcode:1,productid:1,group: { $arrayElemAt: [ "$group", 0 ]},
                category: { $arrayElemAt: [ "$category", 0 ]}, division: { $arrayElemAt: [ "$division", 0 ]} }} 
  ]); 

then your output will show like

{
    "_id" : ObjectId("58466154668bde730a460e1c"),
    "productid" : "1",
    "productname" : "UNIVERSAL AC DC ADAPTER-PCS",
    "productcode" : "1000054",
    "productpoint" : "1",
    "group" : {
        "_id" : ObjectId("5846612b668bde730a460e1a"),
        "productgroupid" : "1",
        "divisionid" : "1",
        "categoryid" : "1",
        "productgroupname" : "ADAPTOR",
        "productgroupcode" : "6765",
        "productgrouppoint" : "7"
    },
    "category" : {
        "_id" : ObjectId("584660f8668bde730a460e18"),
        "categoryid" : "1",
        "divisionid" : "1",
        "categorycode" : "34",
        "categoryname" : "AUDIO SYSTEM",
        "categorypoint" : "Null"
    },
    "division" : {
        "_id" : ObjectId("584660a7668bde730a460e16"),
        "divisionid" : "1",
        "divisioncode" : "0",
        "divisionname" : "ELECTRONICS/HOME APPLIANCE",
        "divisionpoint" : "2"
    }
}
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
  • i have 60k products .above code is not working what can i do sir @Shaishab Roy – its me Apr 20 '17 at 06:23
  • If huge amount of data then you can use `skip` and `limit` before `lookup` otherwise it may will take some time to complete and one important thing is you should create `index` of `lookup` fields then it will run faster. – Shaishab Roy Apr 20 '17 at 06:53
  • how can i do skip and limit can you help me out? – its me Apr 20 '17 at 10:38
  • `{$skip:skipValue},{$limit: limitValue}` then `{$lookup :...}` – Shaishab Roy Apr 20 '17 at 11:35
  • http://stackoverflow.com/questions/43542270/how-can-i-combine-multiple-collection-into-one-collection-using-with-lookup-mon – its me Apr 21 '17 at 12:24