2016-03-15 66 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