
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Best way to store date/time in MongoDB?
There are two different ways by which you can store date/time in MongoDB. In the first approach, you can use Date objects like JavaScript. The Date object is the best way to store date/time in MongoDB. The syntax is as follows:
new Date();
In the second approach, you can use ISODate(). The syntax is as follows:
new ISODate();
To understand the above syntax, let us create a collection with documents following the first approach. The query to create a collection with document is as follows:
The first approach:
> db.ProductsInformation.insertOne({"ProductId":"Product-1","ProductDeliveryDateTime":new Date()}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ec6786fd07954a4890686") }
The second approach:
> db.ProductsInformation.insertOne({"ProductId":"Product-2","ProductDeliveryDateTime":new ISODate()}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ec6846fd07954a4890687") }
Display all documents from the collection with the help of find() method. The query is as follows:
> db.ProductsInformation.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6ec6786fd07954a4890686"), "ProductId" : "Product-1", "ProductDeliveryDateTime" : ISODate("2019-02-21T15:40:40.901Z") } { "_id" : ObjectId("5c6ec6846fd07954a4890687"), "ProductId" : "Product-2", "ProductDeliveryDateTime" : ISODate("2019-02-21T15:40:52.684Z") }
Note: The best way to store date/time objects is with Date object.
- Related Articles
- Best way to store weekly event in MySQL?
- What is the easiest way to store date in MySQL database?
- Best way to sum array size fields in MongoDB?
- Best way to change the date format in MySQL SELECT?
- What is the best way to initialize a JavaScript Date to midnight?
- Can anyone suggest how to pass time the best possible way?
- What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time in C#?
- How MySQL evaluates if I store date along with time value in a column having DATE data type?
- How to store array values in MongoDB?
- Convert date parts to date in MongoDB
- Best Way to compress mysqldump?
- Fastest way to store easily editable config data in PHP?
- Best way to null check in Kotlin
- How to store MongoDB result in an array?
- How to set default date time as system date time in MySQL?

Advertisements