我想清楚地解释一下我的问题。
我需要在booking_date为“15-03-2016”且状态为“Underprocess”的情况下获取COUNT的mongoose子文档。
这是我的路线
router.get('/bookings', function(req, res){
Bookings.find({}, 'booking', function (err, docs) {
if(err)
throw err;
res.json(docs);
});
});
在跑步时跑到Json以下:
[
{
_id: "56a3174bfc518cd014af7abd",
booking:
[
{
name: "Vignesh",
mobile: "9282438685",
can_name: "Kinley",
can_quantity: "2",
can_cost: "80",
can_path: "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png",
delivery_date: "23-01-2016",
delivery_timeslot: "3pm-8pm",
order_id: "S16064",
subscription: "true",
subscription_type: "EveryDay",
total_cost: "560",
address: "12,Ramanrajan street,,padi,Chennai",
_id: "56a3174bfc518cd014af7abe",
delivered_at: "2016-01-22T18:30:00.000Z",
ordered_at: "2016-01-23T06:01:47.451Z",
status: "Delivered"
},
{
name: "Vignesh",
mobile: "9282438685",
can_name: "Kinley",
can_quantity: "2",
can_cost: "80",
can_path: "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png",
delivery_date: "24-01-2016",
delivery_timeslot: "3pm-8pm",
address: "12,Ramanrajan street,,padi,Chennai",
order_id: "S16064",
subscription_type: "EveryDay",
_id: "56a31ba2d55894ec15eac1cf",
ordered_at: "2016-01-23T06:20:18.479Z",
status: "UnderProcess"
},
{
name: "Vignesh",
mobile: "9282438685",
can_name: "Kinley",
can_quantity: "2",
can_cost: "80",
can_path: "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png",
delivery_date: "15-03-2016",
delivery_timeslot: "5pm-6pm",
order_id: "17653",
address: "12,Ramanrajan street,,padi,Chennai",
_id: "56daa80c62c4eb2c15ed86ca",
ordered_at: "2016-03-05T09:34:04.190Z",
status: "UnderProcess"
},
{
name: "Vignesh",
mobile: "9282438685",
can_name: "Kinley",
can_quantity: "2",
can_cost: "80",
can_path: "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png",
delivery_date: "15-03-2016",
delivery_timeslot: "7pm-8pm",
order_id: "13420",
address: "12,Ramanrajan street,,padi,Chennai",
_id: "56dab95a6f67fe481099b13a",
ordered_at: "2016-03-05T10:47:54.177Z",
status: "UnderProcess"
}
]
},
{
_id: "56a0bc8d3306f388131e56c6",
booking:
[
{
name: "Ganesh",
mobile: "9042391491",
can_name: "Bisleri",
can_quantity: "5",
can_cost: "250",
can_path: "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png",
delivery_date: "23-01-2016",
delivery_timeslot: "3pm-8pm",
order_id: "S12348",
subscription: "true",
subscription_type: "Alternate",
total_cost: "1000",
address: "15/A,Main Street,kodambakkam,Chennai",
_id: "56a3164dc2c549e811c0d08f",
delivered_at: "2016-01-22T18:30:00.000Z",
ordered_at: "2016-01-23T05:57:33.169Z",
status: "Delivered"
},
{
name: "Ganesh",
mobile: "9042391491",
can_name: "Bisleri",
can_quantity: "5",
can_cost: "250",
can_path: "http://test15.watervan.in/wp-content/uploads/2015/07/p-95-WV-Kinley-25l.png",
delivery_date: "15-03-2016",
delivery_timeslot: "3pm-8pm",
address: "15/A,Main Street,kodambakkam,Chennai",
order_id: "S12348",
subscription_type: "Alternate",
_id: "56a31c29d55894ec15eac1d0",
ordered_at: "2016-01-23T06:22:33.307Z",
status: "UnderProcess"
}
]
},
]
非常感谢帮助。
答案 0 :(得分:0)
如果我理解正确你可能只是使用这样的东西:
Bookings.count({
'booking.status': 'Underprocess',
'booking.delivery_date' : '15-03-2016'
}, function (err, docs) {
// ... count of top-level items which have booking with following attributes
});
或者,如果要计算子文档,则应使用aggregation
:
db.items.aggregate([
// unwind binding collection
{ $unwind : "$booking" },
// group and count by relevant attributes
{
$group : {
_id : {
status: "$booking.status",
delivery_date: "$booking.delivery_date"
},
count: { $sum: 1 }
}
},
// get proper counts
{
$match : {
"_id.status" : "Underprocess",
"_id.delivery_date" : "15-03-2016"
}
}
], function(err, docs) {
// ...
});