Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb $匹配多个参数_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Mongodb $匹配多个参数

Mongodb $匹配多个参数,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我正在为mongo构建一个查询 db.dectar_rides.aggregate({$match:{ ride_status:{$in:["Completed"]}}}, {$group:{_id:"$user.name", email:{ $first: '$user.email'},total_viajes: {$sum:1}}} ); 但我想按日期添加限制,然后它就不起作用了

我正在为mongo构建一个查询

 db.dectar_rides.aggregate({$match:{ ride_status:{$in:["Completed"]}}},
                           {$group:{_id:"$user.name", email:{ $first: '$user.email'},total_viajes: {$sum:1}}}
                           );

但我想按日期添加限制,然后它就不起作用了

 db.dectar_rides.aggregate({$match:{ ride_status:{$in:["Completed"]}, 
                                     booking_information.pickup_date: {$gte: new ISODate("2016-09-01T00:00:00Z"),
                                                                       $lt : new ISODate("2016-10-31T23:59:59Z") }}},
                           {$group:{_id:"$user.name", email:{ $first: '$user.email'},total_viajes: {$sum:1}}},
                           );

但如果我单独运行,它会正常工作:

db.dectar_rides.find({"booking_information.pickup_date": {$gte: new ISODate("2016-09-01T00:00:00Z"),
                                                          $lt : new ISODate("2016-10-31T23:59:59Z") }});


如何使
$match
与这些参数配合使用?

您需要用引号将嵌入的字段括起来。使用访问字段时,必须

此外,对于查询数组,运算符仅在您希望匹配指定值列表中的任何值时才有必要,但当它只是您希望匹配的单个元素时,您最好使用隐式运算符,如上所述

db.dectar_rides.aggregate([
    { 
        "$match": { 
            "ride_status": "Completed", 
            "booking_information.pickup_date": {
                "$gte": new ISODate("2016-09-01T00:00:00Z"),
                "$lt": new ISODate("2016-10-31T23:59:59Z") 
            }
        }
    },
    {
        "$group": {
            "_id": "$user.name", 
            "email": { "$first": '$user.email' },
            "total_viajes": { "$sum": 1 }
        }
    },
]);