Reputation: 2791
Im building a query for mongo
db.dectar_rides.aggregate({$match:{ ride_status:{$in:["Completed"]}}},
{$group:{_id:"$user.name", email:{ $first: '$user.email'},total_viajes: {$sum:1}}}
);
but I want to add restriction by date, and then it doesn't work
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}}},
);
but if I run this sepparately, it works properly:
db.dectar_rides.find({"booking_information.pickup_date": {$gte: new ISODate("2016-09-01T00:00:00Z"),
$lt : new ISODate("2016-10-31T23:59:59Z") }});
How do I make $match
work with these parameters?
Upvotes: 1
Views: 919
Reputation: 103365
You need to enclose embedded fields with quotation marks. When using the dot notation to access the field, you must enclose the dotted name in quotes.
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 }
}
},
]);
Also, for querying an array, the $in
operator is only necessary when you want to match any value in a specified list of values but when it's only a single element you want to match then you might as well use the implicit $eq
oerator, as in the above.
Upvotes: 2