2016-03-15 65 views
0

这里我想清楚地解释我的问题。用两个条件查询计算猫鼬子文档

我需要在booking_date为“15-03-2016”且状态为“欠进程”的条件的基础上获得猫鼬子文档的COUNT个计数。

这是我的路线

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

如果我理解正确的话,你可以简单地使用这样的:

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) { 
    // ... 
}); 
+0

耶其工作biut不算子文档。我需要算子文档 – Nodemon

+0

@Nodemon更新了答案 –

+0

我怎样才能得到响应或文档 – Nodemon