Mongodb 使用第一个集合id从另一个集合获取总计数
我有两个收藏 第一个id具有以下结构的吊舱:Mongodb 使用第一个集合id从另一个集合获取总计数,mongodb,mongoose,mongodb-query,aggregation-framework,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我有两个收藏 第一个id具有以下结构的吊舱: { "_id": "5ae46c0aff1bde1634ad8b3f", "admin": "595bdd5aa6ea2811d48a7802", "name": "Fashion Trend", "datetime": 1524919038771, "approval": false, "actions": "Comments", "description": "Fashion is abou
{
"_id": "5ae46c0aff1bde1634ad8b3f",
"admin": "595bdd5aa6ea2811d48a7802",
"name": "Fashion Trend",
"datetime": 1524919038771,
"approval": false,
"actions": "Comments",
"description": "Fashion is about dressing according to what's fashionable. Style is more about being yourself.",
"profilepic": "https://cdn.com/upload/Community_photos/595bdd5aa6ea2811d48a7802_1524919188491.jpg",
"coverpic": "https://cdn.com/upload/Community_photos/595bdd5aa6ea2811d48a7802_1524919171374.jpg",
"__v": 0
}
第二个是pod_成员,结构如下:
{
"_id": "5b127683187a4f19901e34b7",
"__v": 0,
"pod_id": "5ae470d04e15d607f043a20e",
"instaid": "5ad9a44cabd2180d64073462",
"username": "tagtor",
"user_id": "595bdd5aa6ea2811d48a7802",
"member_name": "Sagar Dalal",
"req_date": 1527936643123,
"datetime": 1527938142745,
"comments": ["Love the page. I would change your aggregation a little bit so you don't need to use $group
stage. Use $size
to get the total_members instead of $sum
. Replace stages after $lookup
with below stages.
{"$addFields":{
"total_members":{
"$size":{
"$filter":{
"input":"$meminfo",
"as":"mi",
"cond":{"$gt":["$$mi.datetime",null]}
}
}
}
}},
{"$project":{"meminfo":0}},
{"$addFields":{"Status":false}}
{
“_id”:“5b127683187a4f19901e34b7”,
“_v”:0,
“pod_id”:“5ae470d04e15d607f043a20e”,
“instaid”:“5ad9a44cabd2180d64073462”,
“用户名”:“标记器”,
“用户id”:“595bdd5aa6ea2811d48a7802”,
“成员姓名”:“Sagar Dalal”,
“请求日期”:1527936643123,
“日期时间”:1527938142745,
“评论”:[“喜欢这个页面。我会稍微更改你的聚合,这样你就不需要使用$group
阶段。使用$size
来获取总成员,而不是$sum
。将$lookup
之后的阶段替换为以下阶段
基本上,我只需要所有的pod文档都有“total_members”"字段和没有成员的pod将拥有总成员数:0
不确定,但看起来这就是你得到的。当没有匹配项时,总成员数为0。我得到x个对象数的数组,这是pod成员中存在的文档总数,我不想要它。如果我在po中有4个文档,我只需要4个对象的数组d集合中有total_成员:x
字段。更新了答案以删除查找数组文档。这应在3.4版本中有效。已更新。请查看。