我有这个名为“点”的集合
[
{ 'guid': 'aaa' },
{ 'guid': 'bbb' },
{ 'guid': 'ccc' },
]
我还有另一个名为“流”
[
{ 'title': 'pippo', 'guid': 'aaa', 'email': 'pippo@disney'},
{ 'title': 'pluto', 'guid': 'aaa', 'email': 'pluto@disney'},
{ 'title': 'goofy', 'guid': 'bbb', 'email': 'goofy@disney'},
{ 'title': 'duck', 'guid': 'aaa', 'email': 'duck@disney'},
{ 'title': 'minnie', 'guid': 'ccc', 'email': 'minnie@disney'},
]
我需要在要搜索email
的包含字母“o”的地方进行聚合查询。我期望的结果是这样的
[
{ 'guid': 'aaa', items: 2 },
{ 'guid': 'bbb', item: 1 },
{ 'guid': 'ccc', item: 0 },
]
我已经这样做了
db.getCollection('points').aggregate([
{
$lookup: {
from: "stream",
localField: "guid",
foreignField: "guid",
as: "items"
}
},
{
$project: {
_id: 0,
guid: 1,
items: {
$size: "$items"
}
}
}
]);
如果我在 mysql 中,查询将是这个
SELECT DISTINCT
points.guid,
(
SELECT COUNT(*)
FROM stream
WHERE guid = stream.guid and stream.email like '%o%'
) AS items
FROM points