- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to insert Date in MongoDB?
To insert date in MongoDB, use Date(). Let us create a collection with documents −
> db.demo421.insert({"DueDate":new Date(Date.now())}); WriteResult({ "nInserted" : 1 }) > db.demo421.insert({"DueDate":new Date("2020-01-15")}); WriteResult({ "nInserted" : 1 }) > db.demo421.insert({"DueDate":new Date("2018-12-31")}); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo421.find();
This will produce the following output −
{ "_id" : ObjectId("5e73a2ec9822da45b30346de"), "DueDate" : ISODate("2020-03-19T16:50:52.746Z") } { "_id" : ObjectId("5e73a2fb9822da45b30346df"), "DueDate" : ISODate("2020-01-15T00:00:00Z") } { "_id" : ObjectId("5e73a3079822da45b30346e0"), "DueDate" : ISODate("2018-12-31T00:00:00Z") }
Advertisements