Chcę pokazywać produkty według identyfikatorów (56e641d4864e5b780bb992c6
i 56e65504a323ee0812e511f2
) i wyświetlać cenę po odjęciu przez rabat, jeśli jest dostępna.
mogę liczyć na ostateczną cenę za pomocą agregatu, ale ten powrót wszystkich dokumentów w kolekcji, jak zrobić to wrócić tylko mecze identyfikatory
"_id" : ObjectId("56e641d4864e5b780bb992c6"),
"title" : "Keyboard",
"discount" : NumberInt(10),
"price" : NumberInt(1000)
"_id" : ObjectId("56e65504a323ee0812e511f2"),
"title" : "Mouse",
"discount" : NumberInt(0),
"price" : NumberInt(1000)
"_id" : ObjectId("56d90714a48d2eb40cc601a5"),
"title" : "Speaker",
"discount" : NumberInt(10),
"price" : NumberInt(1000)
to moje zapytanie
productModel.aggregate([
{
$project: {
title : 1,
price: {
$cond: {
if: {$gt: ["$discount", 0]}, then: {$subtract: ["$price", {$divide: [{$multiply: ["$price", "$discount"]}, 100]}]}, else: "$price"
}
}
}
}
], function(err, docs){
if (err){
console.log(err)
}else{
console.log(docs)
}
})
i jeśli dodać tę $in
zapytanie, zwraca pustą tablicę
productModel.aggregate([
{
$match: {_id: {$in: ids}}
},
{
$project: {
title : 1,
price: {
$cond: {
if: {$gt: ["$discount", 0]}, then: {$subtract: ["$price", {$divide: [{$multiply: ["$price", "$discount"]}, 100]}]}, else: "$price"
}
}
}
}
], function(err, docs){
if (err){
console.log(err)
}else{
console.log(docs)
}
})
To wszystko, teraz działa. ale zwykle wyrzucam id jako string w findOneAndUpdate lub innym zapytaniu i działa dobrze, czy problem występuje tylko w agregacie? –
@MuhammadFasalirRahman Na to właśnie odpowiedziałem. '.find()' może używać 'schematu', który oczywiście ma domyślny typ' ObjectId' dla pola '_id'. Rurociągi agregujące nie ** używają schematu '' Schema', jak już to wyjaśniłem. –
To nie działa w mangurze 5 –