Tony
Tony

Reputation: 171

Is there any way I can know creation date of an Index for MongoDB tables(collections)?

Is there any way I can know creation date of an Index for MongoDB tables(collections)? Recently, we saw some indexes which lead to some problem of space and performance and wonder if we can get the timestamp for create_date of the indexes. And I'm not sure if there is a way to do that in the most recent version of mongodb. and if not so, is there any workaround to do that? Thanks a lot.

Upvotes: 13

Views: 2749

Answers (2)

Wang Li
Wang Li

Reputation: 51

db.t1.aggregate( [ { $indexStats: { } } ] ) , check since

Upvotes: 4

Ramit Mittal
Ramit Mittal

Reputation: 573

Check the rs.oplog collection in the local database.

db.oplog.rs.find({ "o.createIndexes": { "$exists": true } }).sort({ts: -1})

A sample response: (when an index with name unique_email was created on users collection in test database.

{ op: 'c',
  ns: 'test.$cmd',
  ui: UUID("47041281-c7d2-4d28-90ef-2baa49c6c31f"),
  o: 
   { createIndexes: 'users',
     v: 2,
     unique: true,
     key: { email: 1 },
     name: 'unique_email',
     background: false },
  ts: Timestamp({ t: 1659682522, i: 2 }),
  t: 62,
  v: 2,
  wall: 2022-08-05T06:55:22.326Z }

CAVEATS

oplog retention is limited both in terms of size of the oplog collection and how long ago the operation was performed. You can read more about these parameters in the official documentation. All write operations are recorded in the oplog, including index creation, but you are more likely to find the oplog entry if the index was created recently.
Therefore, this method will only work in some cases.

Upvotes: 1

Related Questions