0

MongoDB Server Community: v4.2.7

Is it possible to define a collection where a date field will be included with every document insert?

Ie. if I insert { field1: "foo", field2: "bar" }

I want a created field to be included: { field1: "foo", field2: "bar", created: Date(...) }

Is this possible?

Edit: For more details, I want to do this so I can create a TTL index on the created field so documents are automatically pruned every x seconds. As I understand it, I need to manually insert the date field on each document insert. I would like to avoid this, if possible.

Bobs Burgers
  • 761
  • 1
  • 5
  • 26

1 Answers1

1

As of now MongoDB doesn't support the auto creation of timestamps (besides the one which exists in the ObjectId) upon insertion. You'll need to save the document with the created field when creating the document.

You may find the thread here helpful.

Iyanu Adelekan
  • 396
  • 2
  • 7