How can i combine multiple collection into one collection using with $lookup mongodb or nodejs mongodb?

前端 未结 1 1215
南旧
南旧 2020-12-22 14:10

i have below collections with document count division - 30,

category - 248

productgroup - 1402

product - 60000

for example i given below dat

相关标签:
1条回答
  • 2020-12-22 14:17

    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.

    0 讨论(0)
提交回复
热议问题