如何编写Query以检索匹配的每个子文档数组

时间:2016-01-11 12:29:13

标签: json node.js mongodb mongoose

在这里,我需要清楚地解释我的问题。如何使用mongoose和nodejs编写查询以检索与条件匹配的每个子文档数组。

这是我现有的JSON:

[
{
    _id: "568ccd6e646489f4106470ec",
    area_name: "Padi",
    warehouse_name: "Hapserve Online Water Service",
    name: "Ganesh",
    email: "ganesh@excrin.com",
    mobile_no: "9042391491",
    otp: "4466",
    __v: 0,
    date: "06-01-2016",
    booking: 
    [
        {
            can_quantity: "4",
            delivery_date: "06-01-2016",
            delivery_timeslot: "10am-3pm",
            subscription: "true",
            subscription_type: "Weekly",
            total_cost: "240",
            order_id: "S13833",
            can_name: "Tata Waters",
            address: "15/A,Ramanrajan street,,Padi,Chennai",
            can_cost: "240",
            _id: "568ccd6e646489f4106470ee",
            ordered_at: "2016-01-06T08:16:46.825Z",
            status: "UnderProcess"
        },
        {
            can_name: "Bisleri",
            can_quantity: "4",
            can_cost: "200",
            delivery_date: "11-01-2016",
            delivery_timeslot: "3pm-8pm",
            order_id: "11537",
            address: "27,Main Street,Padi,Chennai",
            _id: "5693860edb988e241102d196",
            ordered_at: "2016-01-11T10:38:06.749Z",
            status: "UnderProcess"
        }
    ]
},
{
    _id: "56937fb8920629a0164604d8",
    area_name: "Poonamallee",
    warehouse_name: "Tata Waters",
    name: "M.Kalaiselvan",
    email: "kalai131192@gmail.com",
    mobile_no: "9003321521",
    otp: "2256",
    __v: 0,
    date: "2016-01-11T10:11:04.266Z",
    booking: 
    [
        {
            can_quantity: "4",
            delivery_date: "06-01-2016",
            delivery_timeslot: "10am-3pm",
            subscription: "true",
            subscription_type: "Alternate",
            total_cost: "640",
            order_id: "S13406",
            can_name: "Kinley",
            address: "133,Bajanai koil street, Melmanagar,Poonamallee,Chennai",
            can_cost: "160",
            _id: "56937fb8920629a0164604da",
            ordered_at: "11-01-2016",
            status: "UnderProcess"
        },
        {
            can_name: "Tata Waters",
            can_quantity: "2",
            can_cost: "120",
            delivery_date: "11-01-2016",
            delivery_timeslot: "10am-3pm",
            order_id: "11387",
            address: "140,Bajanai koil street, Melmanagar,Poonamallee,Chennai",
            _id: "56937ff7920629a0164604dc",
            ordered_at: "2016-01-11T10:12:07.719Z",
            status: "UnderProcess"
        },
        {
            can_name: "Bisleri",
            can_quantity: "4",
            can_cost: "200",
            delivery_date: "12-01-2016",
            delivery_timeslot: "10am-3pm",
            order_id: "16853",
            address: "140,Bajanai koil street, Melmanagar,Poonamallee,Chennai",
            _id: "56938584db988e241102d194",
            ordered_at: "2016-01-11T10:35:48.911Z",
            status: "UnderProcess"
        },
        {
            can_name: "Hapserve",
            can_quantity: "6",
            can_cost: "150",
            delivery_date: "11-01-2016",
            delivery_timeslot: "10am-3pm",
            order_id: "17397",
            address: "133,Bajanai koil street, Melmanagar,Poonamallee,Chennai",
            _id: "569385bbdb988e241102d195",
            ordered_at: "2016-01-11T10:36:43.918Z",
            status: "UnderProcess"
        },
        {
            can_name: "Bisleri",
            can_quantity: "5",
            can_cost: "250",
            delivery_date: "11-01-2016",
            delivery_timeslot: "10am-3pm",
            order_id: "14218",
            address: "133,Bajanai koil street, Melmanagar,Poonamallee,Chennai",
            _id: "56939a13c898ef7c0cc882b0",
            ordered_at: "2016-01-11T12:03:31.324Z",
            status: "Cancelled"
        }
    ]
  }
]

在这里,我需要检索交货日期为今天的每个文件

所以这是我的nodejs route

router.get('/booking-date/:date', function(req, res){
        var id = req.params.date;
        RegisterList.find({'booking.delivery_date':id}, {'booking.$':1}, function(err, docs){
            if(err)
                throw err;
            res.json(docs);
        });
    });

虽然使用此功能但无法获取所有数据。只从集合中检索两个数据。

示例如果我搜索日期11-01-2016 am只获取一个子文档 每个父ID,但在上面的json中为日期11-01-2016。对于一个父ID,该日期有2个子文档,另一个父ID具有该日期的1个子文档。

我无法将mongoose查询检索写入匹配完成的每个子文档。 帮助将不胜感激......

2 个答案:

答案 0 :(得分:1)

听起来您可能想尝试 aggregation framework ,您可以 $project 预订数组,并使用 $setDifference $map $cond 运营商。

$map 运算符检查booking数组中的每个元素, $cond 运算符仅返回基于的所需字段如果是真正的条件,则返回错误的值而不是数组元素。 $setDifference 运算符然后通过与具有[false]值的另一个集合进行比较来删除数组中的所有false值,最终结果只是返回的匹配项:

router.get('/booking-date/:date', function(req, res){
    var id = req.params.date,
        pipeline = [
            {
                "$match": { 'booking.delivery_date': id }
            },
            { 
                "$project": {        
                    "booking": { 
                        "$setDifference": [
                            { 
                                "$map": {
                                    "input": "$booking",
                                    "as": "el",
                                    "in": {
                                        "$cond": [
                                            { "$eq": [ "$$el.delivery_date", id ] },
                                            "$$el",
                                            false
                                        ]
                                    }
                                }
                            },
                            [false]
                        ]
                    }
                }
            }
        ];

    RegisterList.aggregate(pipeline, function(err, docs){
        if(err) throw err;
        res.json(docs);
    });
});

答案 1 :(得分:0)

$ projection运算符只投影第一个匹配元素,请参考here

项目所有子文档{bookings:1},然后过滤应用程序中的子文档。