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

1

8

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

Gadgetry answered 14/7, 2021 at 17:9 Comment(0)
G
7

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

Gertudegerty answered 15/7, 2021 at 6:50 Comment(2)
is there is a way to make it auto fetch the current date and time ? instead of adding it manual like dob: new Date()Gadgetry
I don't know/think there is such default assignment in Compass. You can do a manual update from command-line mongo shell for multiple documents.Gertudegerty

© 2022 - 2024 — McMap. All rights reserved.