1

I want query something based on date(11/11/2016) using like operator:

How to do the same in mongodb?

db.getCollection('TableName').find({"Date":/.*10/11/2016.*/})

I tried with this query but getting invalid result.

2
  • Is date is stored as string? Commented Nov 11, 2016 at 5:35
  • yes lakshmipriya. Got the result just tried with this Query db.getCollection('TableName').find({"Date":/.*11\/11\/2016 .*/}) Commented Nov 11, 2016 at 5:59

2 Answers 2

4

There is no like in mongo,You can use regex Here in this case you dont require regex,you can get your result using simple find query,Date stored in mongodb is in ISO formate,So first convert your date to ISO date and then find for eg

var convertDate = "11/11/2016";
convertDate  = new Date(11/11/2016);
db.getCollection('TableName').find({date : convertDate})

Or if you want like in mongo use this

1)Search directly

db.getCollection('TableName').find({date : {$regex : "11/11/2016"}})

2) Create a text index on date and then search

db.getCollection('TableName').find({$text:{$search:"11/11/2016"}})
Sign up to request clarification or add additional context in comments.

Comments

-1

Like/Regex in Mongodb can be done by . when you have / in value.

To get date starting with 10/11/, try this for your query:

db.getCollection('TableName').find({'Date':/^10.11./})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.