Maria
Maria

Reputation: 5

How to write a query in Mongo to remove records where DateTimeOffset is greater than 30 days

A capture from mongoDb with the structure of a Date time

Upvotes: 0

Views: 162

Answers (1)

Chandan Singh Sirola
Chandan Singh Sirola

Reputation: 112

You can set TTL indexes while creating your records.TTL indexes are special single-field indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time or at a specific clock time.

To create a TTL index, use the db.collection.createIndex() method with the expireAfterSeconds option on a field whose value is either a date or an array that contains date values.

For example, to create a TTL index that removes the record after 30 days on the createdDate field of the User collection, use the following operation in the mongo shell:

db.User.createIndex( { "createdDate": 1 }, { expireAfterSeconds: 2592000 } )

src: https://docs.mongodb.com/manual/core/index-ttl/

Upvotes: 1

Related Questions