Ich habe diese Sammlung namens „Punkte“mongodb Aggregat Filter und zählen auf NodeJS
[
{ 'guid': 'aaa' },
{ 'guid': 'bbb' },
{ 'guid': 'ccc' },
]
und ich habe einen anderen Namen „Stream“
[
{ '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]'},
]
Ich brauche eine Aggregatabfrage zu tun, wo ich will zu suchen email
, die Buchstaben "o" enthält. Die Ergebnisse, die ich erwarte, ist dies
[
{ 'guid': 'aaa', items: 2 },
{ 'guid': 'bbb', item: 1 },
{ 'guid': 'ccc', item: 0 },
]
ich bereits in diesem
db.getCollection('points').aggregate([
{
$lookup: {
from: "stream",
localField: "guid",
foreignField: "guid",
as: "items"
}
},
{
$project: {
_id: 0,
guid: 1,
items: {
$size: "$items"
}
}
}
]);
getan, wenn ich in mysql waren die Abfrage dieser
wird auf KommentarSELECT DISTINCT
points.guid,
(
SELECT COUNT(*)
FROM stream
WHERE guid = stream.guid and stream.email like '%o%'
) AS items
FROM points
gut gemacht, aber wenn ich brauche diesen SELECT COUNT (*) FROM Strom WHERE guid = stream.guid und (stream.email like '% o%' oder stream.email like '% i%') dank –
@MauroSala: Meine Antwort wurde aktualisiert. Aber alle Ihre E-Mails haben "ich" in "Disney" ... – 4J41
Ich muss eine Abfrage mit viel oder wie folgt machen: SELECT COUNT (*) FROM Stream WHERE guid = stream.guid und (stream.email like ' % o% 'oder stream.title wie'% i% '), aber Ihre Antwort ist korrekt –