How to set the default value for mongodb field

  mongodb, question

I have encountered a problem. I want to automatically give a current time when inserting a piece of data for the first time, because I need to use upsert to ensure efficiency, so I cannot use find to process it one by one. How can I set a default value for this? After checking mongodb, it seems that MongoDB doesn’t support it. Please ask the Great God to dispel doubts.

Db.<coll>.update({ condition }, {$set: {field}, $ setinset: {date: newdate ()}}, {upsert: true}).

Add$setOnInsertIndicates that this field is only brought when inserting. Unlike relational databases, this time comes from your application, not the default value added by the database.