数据库有两个集合如下:
event: {_id:abc,....},{_id:efg,...},{_id:xxx,....}
items:{_id:xxx,eid:abc,view:233,...},{_id:xxx,eid:abc,view:234,...},{_id:xxx,eid:efg,view:324,...},{_id:xxx,eid:efg,view:44,...},{_id:eid:abc,view:90,...}
要写一个读出event 的列表并统计items跟event中eid关联的items的数量,并计算关联的view的和;
要求输出结果如下:
{_id:abc,itemscount:213,viewsum:123123},{_id:efg,itemscount:234,viewsum:2342},{_id:xxx,itemscount:2342,viewsum:23423}
这个怎么写求大神给个例子
动不动就粘链接,也是厉害,都不看别人啥问题。。
.lookup({ from:'items', localField:'_id', foreignField:'eid', as:'xxx' }).replaceRoot({ newRoot:$.mergeObjects([{itemscount:$.size('$xxx')},'$$ROOT']) }).project({xxx:0})
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-server-api/database/aggregation/stages/lookup.html