Ich bin neu in Mongodb & mit es in Web-Anwendung, die ich mit MEAN-Stack erstellen. Mein Ziel ist es, zwei Tabellen abzufragen, indem Sie sie verbinden und eine Filterbedingung auf sie anwenden. Zum Beispiel: Ich habe zwei Tische - Bike-BikeID, Registrier-Nr., Marke, Modell & Termin - Termin, Status, Bike (ref Bike-Objekt) und ich möchte nur jene Bikes zeigen, die keinen Termin mit Status = haben 'Gebucht'. Ich möchte das folgende SQL in Mongoose durchführen.Abfrage mit Joins & Filter in Mongoose
Ich benutze den folgenden Code, aber ich bekomme nicht die gewünschten Ergebnisse. Kann mir jemand bei dieser Anfrage helfen?
app.get('/api/getbikeappo*',function(req,res){
var msg="";
//.find({cust:req.query._id})
//.populate('cust','email')
ubBike.aggregate([
{
$match:
{
cust : req.query._id
}
},
{
$lookup:
{
from: "appos",
localField: "_id",
foreignField: "bike",
as : "appointments"
}
},
{
$match:
{
"appointments" : {$eq : []}
}
}
])
.exec(function(err,bikes){
res.send(bikes);
if(err) throw err;
});
});
bikes - collection
{
"_id": {
"$oid": "57fb600fdd9070681de19c18"
},
"brand": "Splendor",
"model": "Splendor",
"year": "2002",
"kms": 0,
"regno": "TN02M8937",
"cust": {
"$oid": "57f8c44466cab97c1355a09a"
},
"__v": 0
}
{
"_id": {
"$oid": "57fb6025dd9070681de19c19"
},
"brand": "Activa",
"model": "Activa",
"year": "2016",
"kms": 0,
"regno": "TN14M3844",
"cust": {
"$oid": "57f8c44466cab97c1355a09a"
},
"__v": 0
}
appointment collection
----------------------
{
"_id": {
"$oid": "57fb6040dd9070681de19c1a"
},
"appoidt": {
"$date": "2016-10-15T18:30:00.000Z"
},
"reqdt": {
"$date": "2016-10-10T09:32:48.694Z"
},
"status": "Booked",
"bike": {
"$oid": "57fb600fdd9070681de19c18"
},
"cust": {
"$oid": "57f8c44466cab97c1355a09a"
},
"__v": 0
}
-----------------
Expected output is
{
"_id": {
"$oid": "57fb6025dd9070681de19c19"
},
"brand": "Activa",
"model": "Activa",
"year": "2016",
"kms": 0,
"regno": "TN14M3844",
"cust": {
"$oid": "57f8c44466cab97c1355a09a"
},
"__v": 0
}
Stellen Sie eine Probe Ihrer Dokumente und auch die Ausgabe, die Sie erwarten? –
@ClementAmarnath getan –