How to set the default value for mongodb field

I have a problem. I want to automatically give a current time when I insert a piece of data for the first time, because I need to use upsert to ensure efficiency, so I can"t process it one by one with find. How can I set a default value for this? Checked the mongodb does not seem to support, ask the great god to solve the doubt.

Mar.28,2021

db.<coll>.update({}, {$set: {}, $setOnInsert: {date: new Date()}}, {upsert: true});

plus $setOnInsert indicates that this field is only available when inserted. Unlike a relational database, this time comes from your application, not the default value added to the database.

Menu