Reputation: 3658
I wanted to query the data which not inside a data range.
I was going through the docs and found $not
and $nin
, but I don't know how to apply the $not
logical operator and the $nin
operator. I tried to wrap the $not but it throws me an error telling
Unknown Top Level Operator: $nin or $not
I basically wanted to query get the data inside a date range, I can simply use the $gt
and $lt
, but I have a bit complex data.
I have two fields startDate
and endDate
and I should use these two fields to query the data based on the data range which the users select.
Support user selects 2017-10-01 to 2017-10-10
, I will have few data where the startDate
will be less than 2017-10-01
and endDate
between the date range selected the user (this scenario should not be excluded while querying), similarly I might have a condition opposite the above like, startDate
inside the date range which user selected and endDate
greater than 2017-10-10
.
I simply wanted to exclude where
**`startDate`** and **`endDate`** < '2017-10-01'
and
**`startDate`** and **`endDate`** > '2017-10-10'
My code which I tried,
$not: {
$or: [{
$and: [
{ "startDate" : { $lt: begin } },
{ "endDate" : { $lt: begin } }
]
},
{
$and: [
{ "startDate" : { $gt: end } },
{ "endDate" : { $gt: end } }
]
}]
}
Where begin
and end
is a moment object
Any help will be much appreciated.
Upvotes: 1
Views: 102
Reputation: 1438
excluding
startDate
and endDate
< '2017-10-01'
and
startDate
and endDate
> '2017-10-10'
This basically means you want to find documents, where startDate and endDate both fall between the selected date range. for this, you can use the following query
coll.find({'startDate': {'$gt': begin, '$lt': end},'endDate': {'$gt': begin, '$lt': end}})
Upvotes: 1