Reputation: 2996
My query now is
events = mdb.events.aggregate([
{"$match": {"type": "ClientService"}},
{"$project": {"value": 1, "day": {"$dayOfYear": "$timestamp"}, "count": {"$add": [1]}}},
{"$group": {"_id": {"day": "$day", "value": "$value"}, "count": {"$sum": "$count"}}},
{"$sort": {"day": -1, "value": 1}}
])
How to modify this query to use "$and" statement in $match query to find documents by more than one parameter?
Upvotes: 1
Views: 14222
Reputation: 1579
From MongoDB docs.
MongoDB provides an implicit AND operation when specifying a comma separated list of expressions. Using an explicit AND with the $and operator is necessary when the same field or operator has to be specified in multiple expressions.
Anyways the syntax for the '$and' remains the same in aggregation i.e.
{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
So your query will look like
events = mdb.events.aggregate([
{"$match": {"$and" : [{"type": "ClientService"},{"some_key":"value"}]}},
{"$project": {"value": 1, "day": {"$dayOfYear": "$timestamp"}, "count": {"$add": [1]}}},
{"$group": {"_id": {"day": "$day", "value": "$value"}, "count": {"$sum": "$count"}}},
{"$sort": {"day": -1, "value": 1}}
])
The same is true for $or
,$not
, $ne
etc.
Upvotes: 1