How to find the created time of collection in MongoDB

174 views
Skip to first unread message

Praneeth Tvss.

unread,
Jul 9, 2018, 2:14:12 AM7/9/18
to mongodb-user
Hi Everyone,

I need to find out the time of a collection when it was created.

I tried to find out using _id like below:
db.collection.findOne()._id
_id
.getTimestamp()

But it throws me an error:
```
2018-07-09T06:00:26.962+0000 E QUERY    [thread1] TypeError: db.collection.findOne(...)._id.getTimestamp is not a function :
@(shell):1:1
```
Since, _id is not an ObjectId for my record. my '_id' looks like `Data-source-<md5 generated id>`

I tried using the following commands also:
```
db.collection.stats()
$collStats()
explain
```

Could anyone please help me with this issue?.

Thank you!

Kevin Adistambha

unread,
Jul 24, 2018, 2:44:12 AM7/24/18
to mongodb-user

Hi Praneeth,

I believe in MongoDB 4.0, the mongod log is the only place where this information is recorded. For example:

2018-07-24T15:34:57.847+1000 I STORAGE  [conn8] createCollection: test.test with generated UUID: 9ca87591-a85b-444b-8713-ff85b4f67b9f

The log timestamp shows the time when the test collection inside the test database was created.

The _id timestamp you mentioned is only valid for an automatically generated ObjectId, and is valid for the creation timestamp of a single document. It looks like that you have a custom _id field, which is why you’re seeing that error.

Alternatively, your application might insert a timestamp into a separate collection when a new collection of interest is created.

Best regards,
Kevin

Praneeth Tvss.

unread,
Jul 25, 2018, 3:54:20 AM7/25/18
to mongodb-user
Thank you very much, Kevin. It helps us.
Reply all
Reply to author
Forward
0 new messages