Working with DATES in mongoDB - Inserting date (current and custom) and querying it

You are here : Home / MongoDB Tutorial


Contents of page >


1) First let's create and insert documents with date field in collection in MongoDB


1.1) Insert date in MongoDB using new Date()
db.employee.insert({_id :1,  firstName:"ank", joining_date : new Date()})
Above will create collection (or table) (if collection already exists it will insert documents in it).


1.2) Insert date in MongoDB using ISODate()
db.employee.insert({_id :2,  firstName:"sam", joining_date : ISODate()})


1.3) Insert custom/userDefined date in MongoDB using new Date()
db.employee.insert({_id :3,  firstName:"neh", joining_date : new Date("2017-03-24T17:00:00.832Z")})

1.4) Insert custom/userDefined date in MongoDB using ISODate()
db.employee.insert({_id :4,  firstName:"amy", joining_date : ISODate("2017-03-23T18:00:00.832Z")})


2) Now, let's query collection in MongoDB


Query all documents of collection using find() method>
db.employee.find()
Output >
{ "_id" : 1, "firstName" : "ank", "joining_date" : ISODate("2017-04-02T17:11:01.360Z") }
{ "_id" : 2, "firstName" : "sam", "joining_date" : ISODate("2017-04-02T17:11:01.367Z") }
{ "_id" : 3, "firstName" : "neh", "joining_date" : ISODate("2017-03-24T17:00:00.832Z") }
{ "_id" : 4, "firstName" : "amy", "joining_date" : ISODate("2017-03-23T18:00:00.832Z") }

3) Find employee whose joining date is equal to "2017-04-02T17:11:01.360Z" in MongoDB


3.1) using new Date()
db.employee.find( { joining_date :  new Date("2017-04-02T17:11:01.360Z") } )
Output>
{ "_id" : 1, "firstName" : "ank", "joining_date" : ISODate("2017-04-02T17:11:01.360Z") }


OR
3.2) using ISODate()
db.employee.find( { joining_date :  new ISODate("2017-04-02T17:11:01.360Z") } )
Output>
{ "_id" : 1, "firstName" : "ank", "joining_date" : ISODate("2017-04-02T17:11:01.360Z") }


4) Find employee whose joining date is greater than "2017-03-25T17:00:00.832Z" in MongoDB
db.employee.find( { joining_date : {$gt : new Date("2017-03-25T17:00:00.832Z") } } )
Output>
{ "_id" : 1, "firstName" : "ank", "joining_date" : ISODate("2017-04-02T17:11:01.360Z") }
{ "_id" : 2, "firstName" : "sam", "joining_date" : ISODate("2017-04-02T17:11:01.367Z") }


5) Summary -
So in this MongoDB tutorial we learned how to Working with DATES in mongoDB - Inserting date (current and custom) and querying it.


First insert documents with date field in collection in MongoDB


Insert date in MongoDB
db.employee.insert({_id :1,  firstName:"ank", joining_date : new Date()})
db.employee.insert({_id :2,  firstName:"sam", joining_date : ISODate()})


Insert custom/userDefined date in MongoDB using new Date()
db.employee.insert({_id :3,  firstName:"neh", joining_date : new Date("2017-03-24T17:00:00.832Z")})
db.employee.insert({_id :4,  firstName:"amy", joining_date : ISODate("2017-03-23T18:00:00.832Z")})


Find employee whose joining date is equal to "2017-04-02T17:11:01.360Z"
db.employee.find( { joining_date :  new Date("2017-04-02T17:11:01.360Z") } )


Find employee whose joining date is greater than "2017-03-25T17:00:00.832Z"
db.employee.find( { joining_date : {$gt : new Date("2017-03-25T17:00:00.832Z") } } )


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.


RELATED LINKS>

Labels: MongoDB
eEdit
Must read for you :