我有以下带有文件计数的集合 师--30,
类别 - 248
productgroup - 1402
产品 - 60000
例如我给出了以下数据 分割
{
"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个收藏品,如
师,
类,
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个系列合并为一个系列。 我应该得到这样的总结 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"
}
}
我运行此代码
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 ]} }}
]);
并且数据在productpointallocations中以这样的方式存储
{
"productpoint": "2",
"productname": "WALKMAN WM#M470-PCS",
"productcode": "1000089",
"productid" :"2"
}
有没有其他方法可以做到这一点,并为此
提供一些其他解决方案nodejs mongodb中还有其他选项吗?
答案 0 :(得分:1)
您应该使用$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}
//初始,然后下次应跳过:1000,限制:1000,依此类推。您可以从请求或使用循环或实施时获得它。