Komal L
Komal L

Reputation: 1

Looking for function similar to "BETWEEN" in MongoDB

I need to get all records from MongoDB collection "employee" where joining_date is between current_date and current_date + 5 days. I couldn't find anything similar to BETWEEN operator in MongoDB documentation. Below query works fine in Google BigQuery. Looking for similar solution in MongoDB.

select * from employee where joining_date BETWEEN current_date() and DATE_ADD(current_date(), interval 5 DAY);

Upvotes: 0

Views: 561

Answers (1)

It'sNotMe
It'sNotMe

Reputation: 1260

The $gt and $lt Comparison Query Operators can be used to find matches within a range of dates. Here's one approach.

db.employee.find({
    "joining_date": {
        $gt: new Date(),
        $lt: new Date(new Date().setDate(new Date().getDate() + 5))
    }
})

Upvotes: 1

Related Questions