查询以两个条件计算猫鼬子文档 [英] Query to Count Mongoose Subdocument with two Condition

查看:68
本文介绍了查询以两个条件计算猫鼬子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据booking_date"15-03-2016"并且status"Underprocess"的条件获取COUNT个子文档.

I need to get COUNT of subdocument on the base of conditions where booking_date is "15-03-2016" and status is "Underprocess".

这是我的路线

router.get('/bookings', function(req, res){
        Bookings.find({}, 'booking', function (err, docs) {
            if(err)
                throw err;
            res.json(docs);
        });
    });

在运行此命令时会遇到Json:

while running this am getting below 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"
        }
    ]
},
]

任何帮助将不胜感激.

推荐答案

如果我正确理解,您可能会简单地使用以下内容:

If I understood correctly you might simply use something like this:

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:

Or if you want to count subdocuments, then aggregation should be used:

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

这篇关于查询以两个条件计算猫鼬子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆