I have this collection named "points"
[
{ 'guid': 'aaa' },
{ 'guid': 'bbb' },
{ 'guid': 'ccc' },
]
and I have another named "stream"
[
{ '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'},
]
I need to do an aggregate query where I want to search email
that contains letter "o". The results that I expect is this
[
{ 'guid': 'aaa', items: 2 },
{ 'guid': 'bbb', item: 1 },
{ 'guid': 'ccc', item: 0 },
]
I have already done this
db.getCollection('points').aggregate([
{
$lookup: {
from: "stream",
localField: "guid",
foreignField: "guid",
as: "items"
}
},
{
$project: {
_id: 0,
guid: 1,
items: {
$size: "$items"
}
}
}
]);
If I were in mysql the query will be this
SELECT DISTINCT
points.guid,
(
SELECT COUNT(*)
FROM stream
WHERE guid = stream.guid and stream.email like '%o%'
) AS items
FROM points