如何使用$ lookup mongodb或nodejs mongodb将多个集合合并成一个集合? [英] How can i combine multiple collection into one collection using with $lookup mongodb or nodejs mongodb?
问题描述
我有以下文件数量的集合
division - 30,
category - 248
productgroup - 1402
产品 - 60000
例如我给出以下数据
division
{
divisionid:1,
divisioncode:0 ,
divisionname:ELECTRONICS / HOME APPLIANCE,
divisionpoint:2
},
{
2,
divisioncode:1,
divisionname:FOODS,
divisionpoint:8
}
类别收集数据
{
categoryid:1,
divisionid:1,
categorycode:34,
categoryname AUDIO SYSTEM,
类别:空
},
{
categoryid:2,
1,
categorycode:348,
categoryname:DVD / VCD,
categorypoint:8
}
产品组收藏数据
{
productgroupid:1,
divisionid:1,
categoryid:1 ,
productgroupname:ADAPTER,
productgroupcode:6765,
productgrouppoint:7
},
{
productgroupid:2,
divisionid:1,
categoryid:2,
productgroupname:WALKMAN b $ bproductgroupcode: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个集合,如
division,
类别,
productgroup,
这些都是我的数据,它存储在我的分类收集数据的
{
divisionid:1,
divisioncode:0,
divisionname:ELECTRONICS / HOME APPLIANCE
divisionpoint:2
},
{
divisionid:2,
divisioncode:1,
divisionname:食物,
分区:8
}
类别收集数据的
{
categoryid:1,
divisionid:1,
categorycode:34,
categoryname:AUDIO SYSTEM,
categorypoint:Null
},
{
categoryid:2,
divisionid:1,
categorycode:348,
categoryname:DVD / VCD $ bcategoriesypoint:8
}
产品组收集数据
{
productgroupid:1,
:1,
categoryid:1,
productgroupname:ADAPTER,
productgroupcode:6765,
productgrouppoint 7
},
{
productgroupid:2,
divisionid:1,
categoryid:2,
productgroupname:WALKMAN,
productgroupcode:7659,
productgrouppoint:Null
}
产品收藏数据
{
productid:1,
divisionid:1,
categoryid:1,
productgroupid ,
productname:UNIVERSAL AC DC ADAPTER-PCS,
productco de:1000054,
productpoint:1
},
{
productid:2,
divisionid:1,
categoryid:2,
productgroupid:2,
productname:WALKMAN WM#M470-PCS,
productcode:1000089 ,
productpoint:2
}
i想组合这些4集合成一个集合。
i应该得到这样的总结
productsummary
{
productpoint:1,
productname:UNIVERSAL AC DC ADAPTER-PCS,
productcode:10000054,
productid:1
group:{
point:7,
name:ADAPTER,
id:1
,
category:{
point:0,
name:AUDIO SYSTEM,
id:1
$ b $$$$$$$$$$$
}
},
{
productpoint:2,
productname:WALKMAN WM#M470 -PCS,
productcode:1000089,
productid:2
group:{
point:7 ,
name:WALKMAN,
id:Null
},
category:{
point:8 ,
name:DVD / VCD,
id:2
},
division:{
point:2 b $ bname:ELECTRONICS / HOME APPLIANCE,
id:1
}
}
我运行这段代码
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]},除法:{$ arrayElemAt:[$ division,0]}}}
]);
它的工作,但我有60 k的产品,那个时候它不工作,得到这样的错误
,数据在productpointallocations中存储如下
{
productpoint:2,
productname:WALKMAN WM#M470-PCS,
productcode:1000089 ,
productid:2
}
有没有另一种方法来做这个,并为这个
提供一些其他解决方案nodejs mongodb中还有其他选项?
您应该使用 $ skip 和 $ limit 以避免此错误。为什么会收到此错误?您必须阅读@Alex评论链接。
db.products.aggregate([
{$ skip:0} ,{$ limit:1000},
{$ 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]}}}
]);
{$ skip:0},{$ limit:1000} code> //初始化,然后下一次应该跳过:1000,limit:1000等等。您可以从请求或使用循环或实现获取。
i have below collections with document count division - 30,
category - 248
productgroup - 1402
product - 60000
for example i given below data division
{
"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 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. i should get summary like this productsummary
{
"productpoint": "1",
"productname": "UNIVERSAL AC DC ADAPTER-PCS",
"productcode": "10000054",
"productid" :"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"
}
},
{
"productpoint": "2",
"productname": "WALKMAN WM#M470-PCS",
"productcode": "1000089",
"productid" :"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"
}
}
i run this code
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 ]} }}
]);
its working but i had 60 k products that time its not working and getting error like this
and data get stored like this in productpointallocations
{
"productpoint": "2",
"productname": "WALKMAN WM#M470-PCS",
"productcode": "1000089",
"productid" :"2"
}
is there any other way to do this and give some other solutions for this
is there any other option in nodejs mongodb?
You should use $skip and $limit to avoid this error. why getting this error ? you have to read @Alex comment link.
db.products.aggregate([
{$skip:0},{$limit: 1000},
{$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 ]} }}
]);
{$skip:0},{$limit: 1000}
// for initial and then for next time should skip: 1000, limit:1000 and so on. you may get it from request or by using loop or as you implement.
这篇关于如何使用$ lookup mongodb或nodejs mongodb将多个集合合并成一个集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!