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.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Cluesner
  • 69
  • 2
  • 2
  • 7

2 Answers2

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"}})
Parshuram Kalvikatte
  • 1,616
  • 4
  • 20
  • 40
-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./})
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
cartman619
  • 552
  • 4
  • 17