mefayed
mefayed

Reputation: 81

how to insert a document with current date and type date on mongodb compass

am using mongodb compass app on windows, i want to insert a document manually from the ui with current date.

i need the app to capture the current time and insert it with the doucment as date type,

"momentAdded":{
  "$date":true
}

the above code is returning 1970-01-01T00:00:00.001+00:00

Upvotes: 8

Views: 11738

Answers (1)

prasad_
prasad_

Reputation: 14287

In the Compass's Documents tab, ADD DATA -> Insert Document. You will find a dialog as shown in the picture.

enter image description here

Select the VIEW "Field-by-Field Editor" mode (the other one is the JSON mode). To create a new date field - enter example field name "dob", then select field type as Date, and enter date value (for example 2021-07-15T11:30:45). This will be saved as a date field in your collection's document. See Compass - Insert Document.

If you use the "JSON" VIEW, then create the date field with this syntax:

{
  "dob": { "$date": "2021-07-15T10:30:48.021Z" }
}

The newly inserted document will look like in the below picture. And, you can verify the field type is Date in the Analyze Your Data Schema tab.

enter image description here

Upvotes: 7

Related Questions