How create a Date field with default value as the current timestamp in MongoDb?
问题 How to create a date field with default value,the default value should be current timestamps whenever the insertion happened in the collection. 回答1: Thats pretty simple! When you're using Mongoose for example, you can pass functions as a default value. Mongoose then calls the function for every insertion. So in your Schema you would do something like: { timestamp: { type: Date, default: Date.now}, ... } Remember to only pass the function object itself Date.now and not the value of the