0

I tried to search MongoDB id in a particular date range using MongoDB query. but it's throwing an error can anyone give any suggestion.

Mongo DB Data

[{
id:1,
full_name:"naveen",
date:"2019-11-02"
},{
id:2,
full_name:"naveen123",
date:"2019-11-04"
}]

mongodb query

db.collection.find({id:1,date:{ '$gte': 2019-11-02,
     '$lt': 2019-11-06 }})
1
  • Here is an example of querying for a range: Query for ranges. Also, the date field is a string in the input document, but you are not using it as a string in the query filter. Commented Nov 7, 2019 at 14:30

1 Answer 1

1

You are missing commas for date values in the query.

Modify your query like below:

db.collection.find({id:1, date:{ "$gte": "2019-11-02", "$lt": "2019-11-06" }});

This is how you may like to do it, but not me.

There are some changes I suggest you should do if you are not already doing it.

1. If you are storing dates as strings in DB, don't do that.

use proper Date format, both while querying and storing.

2. use below format of query:

db.collection.find({id:1, date:{ /* cond1 */ }, 
     date: { /* cond2 */ }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.