You could use a pre-save hook:
const geoframeSchema = mongoose.Schema({
count: Number,
createdBy: String,
createdAt: Date,
polygons: [
{
points: [{ lat: Number, lng: Number }],
},
],
});
geoframeSchema.pre('save', function() {
this.createdAt = doc._id.getTimestamp();
});
You'd have to make sure that doc._id.getTimestamp() is accessible from the schema or set it up as an instance method:
geoframeSchema.methods.getTimestamp = function() {
...
}
...and then call it in your pre-save hook:
geoframeSchema.pre('save', function() {
this.createdAt = this.getTimestamp();
});
In this case you could also use the schema options object to set up time stamps which will populate automatically:
const geoframeSchema = mongoose.Schema({
count: Number,
createdBy: String,
polygons: [
{
points: [{ lat: Number, lng: Number }],
},
],
}, {
timestamps: {
createdAt: 'createdAt',
updatedAt: 'updatedAt'
}
});
You can name your timestamps like so:
createdAt: 'timestampName'