Auto populate date in MongoDB on insert

后端 未结 4 1281
说谎
说谎 2021-01-05 03:54

MongoDB provides a way to update a date field by the system on update operations: https://docs.mongodb.com/manual/reference/operator/update/currentDate/. Is there any equiva

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 04:22

    The $currentDate is an update operator which populates date field with current date through update operation.

    To auto populate date field while insertion of new MongoDB document,please try executing following code snippet

    var current_date=new Date();
    db.collection.insert({datefield:current_date})
    

    In above code snippet the statement

    new Date()

    creates a new JavaScript Date object which consists of a year, a month, a day, an hour, a minute, a second, and milliseconds

提交回复
热议问题