如何使用node.js,mngodb将多个集合合并到一个集合中?

时间:2016-12-06 06:07:48

标签: javascript node.js mongodb

我有4个收藏品,如

师,

类,

productgroup,

产品

这些都是我的数据存储在集合中 我的部门收集数据

{

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

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

类别集合数据

{

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

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

产品组合数据

{

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

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

产品收集数据

{

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

我想将这4个集合合并为一个集合。

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

当我在下面的ansered代码中执行错误时

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用多个$lookup从多个集合中获取数据。说你从产品集合运行查询。查询如

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

然后您的输出将显示为

{
    "_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"
    }
}