Reputation: 1061
I have the following schema
{
f1: "test",
f2: "something",
type: "A",
date: "2018-11-01T00:00:00.000Z",
deleted: false
},
{
"f1": "check",
type: "B",
deleted: false
}
Now what I want is to get all data, and if type = "A", then add an extra condition to my match query, suppose compare its date with current date
.
My current query is:
db.getCollection('myCollection').aggregate([
{$match:{
{"deleted":false},
// I want to check if type is A then compare its date
}}
])
Upvotes: 5
Views: 24053
Reputation: 66475
You could try an $or
and say "If it's not type A or the date is x":
{$match:{
$and: [
{deleted: false},
$or: [
{type: {$ne: 'A'}},
{date: {$gte: ISODate("2018-01-01T00:00:00.0Z"), $lt: ISODate("2018-06-01T00:00:00.0Z")}}
]
]
}}
Upvotes: 13
Reputation: 4220
Use $match
with $or
condition.
db.getCollection('tests').aggregate([
{ $match: {
$or : [
{ "deleted": false, "type": "A", "date": ISODate("2018-11-01T00:00:00.000Z") },
{ "deleted": false, type: { $ne: "A" }}
]}
}
])
Upvotes: 5