2
私は、このコレクションが命名した「ポイント」MongoDBの集計フィルタと、私は私が欲しい集計クエリを実行する必要がありnodejs
[
{ 'guid': 'aaa' },
{ 'guid': 'bbb' },
{ 'guid': 'ccc' },
]
を頼りに、私は別の名前の「ストリーム」
[
{ 'title': 'pippo', 'guid': 'aaa', 'email': '[email protected]'},
{ 'title': 'pluto', 'guid': 'aaa', 'email': '[email protected]'},
{ 'title': 'goofy', 'guid': 'bbb', 'email': '[email protected]'},
{ 'title': 'duck', 'guid': 'aaa', 'email': '[email protected]'},
{ 'title': 'minnie', 'guid': 'ccc', 'email': '[email protected]'},
]
を持っています文字「o」を含むemail
を検索します。私はすでにこの
db.getCollection('points').aggregate([
{
$lookup: {
from: "stream",
localField: "guid",
foreignField: "guid",
as: "items"
}
},
{
$project: {
_id: 0,
guid: 1,
items: {
$size: "$items"
}
}
}
]);
を行っている私は期待した結果がこの
[
{ 'guid': 'aaa', items: 2 },
{ 'guid': 'bbb', item: 1 },
{ 'guid': 'ccc', item: 0 },
]
である私は、MySQLにあった場合は、クエリは、この
SELECT DISTINCT
points.guid,
(
SELECT COUNT(*)
FROM stream
WHERE guid = stream.guid and stream.email like '%o%'
) AS items
FROM points
よくやったが、私は、このSELECT COUNT(*) ストリーム GUID = stream.guid FROMと( '%Iの%' のような '%O%' またはstream.emailようstream.email)感謝が必要な場合 –
@MauroSala:私の答えを更新しました。しかし、すべてのあなたの電子メールは、 "ディズニー"で "私"を持っています... – 4J41
私はたくさんのまたはこのようなクエリを行う必要があります:SELECT COUNT(*)からストリームどこguid = stream.guidと(stream.email like ' %o% 'やstream.titleのように'%i% ')、あなたの答えは正しいです –