- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Inserting Date() in MongoDB through Mongo shell?
In order to insert Date() in MongoDB through Mongo shell, use the following syntax
var yourVariableName= new Date(year,month, day, hour, minute); db.yourCollectionName({yourDateFieldName:yourVariableName});
Let us first create a date variable
> var creatingDate = new Date(2019, 03, 29, 13, 12);
Let us create a collection with documents:
>db.insertingDateUsingVariableDemo.insertOne({"UserName":"John","UserMessages":["Hi","Hello","Awesome"],"UserPostDate":creatingDate});
Following is the query to display all documents from a collection with the help of find() method
> db.insertingDateUsingVariableDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9d1b19a629b87623db1b21"), "UserName" : "John", "UserMessages" : [ "Hi", "Hello", "Awesome" ], "UserPostDate" : ISODate("2019-04-29T07:42:00Z") }
- Related Articles
- Format date value in MongoDB shell?
- How to list all collections in the Mongo shell?
- How to list all databases in the Mongo shell?
- How to list all users in the Mongo shell?
- How to insert an 8-byte integer into MongoDB through JavaScript shell?
- Inserting the current datetime in MongoDB?
- Format date while inserting records in MySQL
- Escaping quotes while inserting records in MongoDB?
- Prettyprint in MongoDB shell as default?
- Use result from MongoDB in shell script?
- Does Mongo shell treats numbers as float by default.? How can we work it around explicitly?
- Get output of MongoDB shell script?
- Variable collection name in MongoDB shell with JavaScript?
- How to print document value in MongoDB shell?
- How to unset a variable in MongoDB shell?

Advertisements