How to create TTL Index on long timestamp in MongoDB

自作多情 提交于 2019-12-10 19:51:43

问题


In my mongo database I have field timestamp which holds time of creation in timestamp ie: "timestamp": 1544029233021 I want to create TTL index on this field, but in docs example is done on "createdAt": new Date(), which is ISODate("2018-12-13T17:00:10.433Z")

Is it possible to in any way to make TTL Index work on timestamp field?

Because this doesnt work:

db.coll.createIndex( { "timestamp": 1 }, { expireAfterSeconds: 3600 } )

回答1:


The documents aren't being expired because the timestamp value is an integer.

TTL indexes will only work on documents where the indexed field is a Date or holds an array of Dates:

If the indexed field in a document is not a date or an array that holds a date value(s), the document will not expire.

(https://docs.mongodb.com/manual/core/index-ttl/#expiration-of-data)



来源:https://stackoverflow.com/questions/53766763/how-to-create-ttl-index-on-long-timestamp-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!