Mongoose Array İçinde Arama
-
crystalmeth bunu yazdı
bu şekilde çekebilirsin
db.collection.aggregate(// Start with a $match pipeline which can take advantage of an index and limit documents processed {$match: { "notlar.onay": true } }, { $unwind: "$notlar" }, { $match: { "notlar.onay": true } })
Sonuç:
Bu sorguda isimi nerede eşleştiriyoruz hocam?
-
biri bunu yazdıcrystalmeth bunu yazdı
bu şekilde çekebilirsin
db.collection.aggregate(// Start with a $match pipeline which can take advantage of an index and limit documents processed {$match: { "notlar.onay": true } }, { $unwind: "$notlar" }, { $match: { "notlar.onay": true } })
Sonuç:
Bu sorguda isimi nerede eşleştiriyoruz hocam?
match altına yapıştır gitsin
Örnek:
db:
[ { isim: "ahmet", soyisim: "mehmet", notlar: [ { onay: false, yer: "istanbul" }, { onay: true, yer: "bursa" }, { onay: false, yer: "ankara" }, ] }, { isim: "yavuzss", soyisim: "soyisimss", notlar: [ { onay: false, yer: "adana" }, { onay: true, yer: "erzincan" }, { onay: false, yer: "erzurum" }, ] } ]
Query:
db.collection.aggregate( // Start with a $match pipeline which can take advantage of an index and limit documents processed { $match: { "notlar.onay": true, "isim": "yavuzss" } }, { $unwind: "$notlar" }, { $match: { "notlar.onay": true } })
Result:
[ { "_id": ObjectId("5a934e000102030405000001"), "isim": "yavuzss", "notlar": { "onay": true, "yer": "erzincan" }, "soyisim": "soyisimss" } ]
-
Yok hocam, istediğim gibi sonuç vermiyor.
Eğer içinde true değeri yoksa boş dönüyor.
Sanki where isim ='ahmet' and aktif = true gibi oluyor.
İstediğim veri yoksa da o isim soyisim dönecek. notlar boş dönecek.
Yada notlarda 10 tane true varsa 10 tanesi de dönecek.
-
Sanırım diğer arkadaşların dediği gibi bu işi mongo tarafında değil, javascript tarafında çözeceğiz map ile.
-
let onayliNot = result.notlar.filter(function (a) { return a.onay == true; });
Bu şekilde js tarafında çözdüm. -
biri bunu yazdı
let onayliNot = result.notlar.filter(function (a) { return a.onay == true; });
Bu şekilde js tarafında çözdüm.Belki biraz daha fazla kaynak tüketiyordur ama daha hızlı kendisi :)