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

[
{
    _id: "568ccd6e646489f4106470ec",
    area_name: "Padi",
    warehouse_name: "Hapserve Online Water Service",
    name: "Ganesh",
    email: "[email protected]",
    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: "[email protected]",
    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路线
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,则只获取一个子文档
每个父ID,但在上面日期为11-01-2016的JSON中。对于一个父ID,该日期有2个子文档,而另一个父ID,该日期有1个子文档。
无法将Mongoose查询检索写入匹配完成的每个子文档。
我们将感谢您的帮助…

最佳答案

听起来您可能想尝试使用aggregation framework$project$setDifference运算符实现带筛选器的预订数组。
$map运算符检查$cond数组中的每个元素,$map运算符基于true条件仅返回所需的字段,而不是数组元素返回相反的false值。booking然后,operator通过与另一个具有[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);
    });
});

08-25 14:03