Vivek Dhiman
Vivek Dhiman

Reputation: 1995

Date Comparison - MongoDB

In document there is date element like doj:20100223 in YYYYMMDD format. I am looking for the query which display all records where doj>2010. Below i have tried but didn't work.

db.TestData1.aggregate(
     { $project : {name : 1 }},
     { $match : { doj : { $gt : 2010}}}
)

Any suggestion..

Thanks

Upvotes: 0

Views: 221

Answers (1)

Calimero
Calimero

Reputation: 4288

Since your data appears to be stored in an integer field, your query should look like :

db.TestData1.aggregate(
     { $match : { doj : { $gt : 20109999}}},
     { $project : {name : 1 }}
)

Beware the $project operator reshapes every record, so you need to either include the doj field you want to query here, or move it after the $match in the pipeline.

Upvotes: 2

Related Questions