我正试图了解聚合特性
我很难弄清楚如何过滤结果
没有孩子?
假设我有
things
:
{ _id: abc1, thingColor: "green" }
{ _id: abc2, thingColor: "red" }
{ _id: abc3, thingColor: "amazing" }
我有
birds
:
{ _id: 1, thing_id: "abc1", type: "singing", isBiting: false }
{ _id: 2, thing_id: "abc1", type: "notFlying", isBiting: true }
{ _id: 3, thing_id: "abc3", type: "manEating", isBiting: false }
现在我想得到一份清单,但只列出那些至少有一只鸟通过id与它们关联,并且只有会咬人的鸟。
所以基本上从这个例子中,我想从
仅:
{ _id: abc1, birds_id: "abc1" }
我的查询是这样的-查询
{
$lookup:
{
from: 'birds',
let: { thingIdVar: '$_id'},
pipeline: [
{$match:
{$expr:
{$and: [
{$eq: ['$thing_id', '$$thingIdVar']},
{$eq: ['$isBiting', true]}
]}
}
}
],
as: 'birds'
}
},
这将返回
加上
但是它会把所有的东西都拿回来,即使他们没有
鸟
合计。
如果我有1比1
到
{$unwind: '$birds'}
但每个
thing
鸟
在这一点上,我以编程的方式进行过滤,但这会弄乱其他一些东西(这个例子是一个简化的版本)。
因此,我更希望从mongo获得已过滤的结果。。
有没有办法做到这一点??
谢谢