Ich versuche, eine Abfrage mit folgendem Dokument in Mongo db zu bilden:MongoDB: auf demselben Dokument resultiert aus Abfrage in derselben Abfrage mit
myMetaDataDoc document
{
_id: <objectId>,
tour:
{
tourId: "TOURID1",
stops: [{
locationId: "LOC1",
stopId: "STOPID1",
...
],[
locationId: "LOC2",
stopId: "STOPID2",
]}
}
schedule:{
stopSchedules: [{
stopId: "STOPID1"
...
},{
stopId: "STOPID2"
...
}]
}
}
Grundsätzlich mag ich in alle Register mit ihren jeweiligen Planinformationen erhalten ein Ort. Ich benötige eine einzelne Abfrage folgendes zu erreichen:
- Abfrage alle mit dem Standort stoppt (zB LOC1.)
- Abfrage alle stopSchedules mit stops.stopId von 1.
- Rückkehr stoppt + Pläne
Was ich versuchte, war Aggregat zu verwenden, konnte aber nicht die stops.stopId in derselben Abfrage verwendet werden. Dies war mein Versuch:
db.myMetaDataDoc.aggregate([
{$match : {'tour.stops.locationId':'LOC1'}}, // all tour stops with locationId as LOC1
{$unwind : '$tour.stops'}, // break all stops
{$match : {'tour.stops.locationId':'LOC1'} }, // all tour with only stops with locationId as LOC1
{$unwind : '$schedule.stopSchedules'}, // break all stopschedules
{$match : {'schedule.stopSchedules.stopsId' : {$in :<stopId array>}} } // missing array of stopId
])
letzten $ Spiel entfernen, erhalte ich alle Zeilen mit Single "tour.stops" und Single "schedules.stopSchedules".
{ "_id" : ObjectId("xx1"), "myMetaDataDoc" : {"tour" : { "tourId" : "TOURID1", "stops" : { "locationId" : "LOC1", "stopId" : "STOPID1"} } , "schedule" : { "stopSchedules" : { "stopId" : "STOPID1", ...}}}}
{ "_id" : ObjectId("xx2"), "myMetaDataDoc" : {"tour" : { "tourId" : "TOURID1", "stops" : { "locationId" : "LOC1", "stopId" : "STOPID1"} } , "schedule" : { "stopSchedules" : { "stopId" : "STOPID2", ...}}}}
Aus diesem Satz von Zeilen Ich brauche nur solche, bei denen stops.stopId schedules.stopSchedules.stopId entspricht. Es scheint, Aggregat mag $ wo nicht. Sollte ich stattdessen mapreduce() verwenden? oder ein serverseitiges Skript?
Schätzen Sie alle Vorschläge.