Setting up TTL on CosmosDB for Mongo

Daniel Ivanov 20 Reputation points
2024-12-17T09:47:29.25+00:00

Hey there,

We are using Azure Cosmos DB for Mongo for our database; server version is 6.0.

We want to activate TTL for one of the collections, but the interface seems to be blocked and asking to create a TTL index. Here is what we see.

Screenshot 2024-12-17 at 11.34.42

We followed the instructions for creating the index on the _ts property, but nothing happens. We tried the index via the Indexing Policy tab, and we also tried via a shell from MongoDB Compass. An index does seem to get registered, but TTL does not work and the Settings tab keeps displaying the message from above screenshot.

Has anybody bumped into that issue before and managed to resolve it? If so, please assist.

Any further information that might be useful to have, let me know and I will provide as soon as possible.

Thank you in advance!

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,710 questions
0 comments No comments
{count} votes

Accepted answer
  1. Mahesh Kurva 1,625 Reputation points Microsoft Vendor
    2024-12-17T20:07:50.87+00:00

    Hi @Daniel Ivanov,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    As I understand it, you're encountering an issue with enabling TTL in Azure Cosmos DB for MongoDB. The message in the screenshot indicates that a TTL index needs to be created.

    Here are a few additional things to check and try:

    1. Make sure the TTL index is created correctly on the _ts field. This command should be run in a MongoDB shell or a tool like MongoDB Compass or Studio 3T.
         db.collection.createIndex({ "_ts": 1 }, { expireAfterSeconds: 3600 })
      
    2. After running the command, verify that the index has been created and ensure that the _ts index with the expireAfterSeconds option is listed.
         db.collection.getIndexes()
      
    3. If there are any errors during index creation, they should be displayed in the shell or tool you are using. Common issues include syntax errors or unsupported options.
    4. Sometimes, the Azure portal might not fully support certain operations. Using native MongoDB tools like the MongoDB shell, MongoDB Compass, or Studio 3T is recommended.
    5. Ensure that your documents have the _ts field populated. This field is a system property in Azure Cosmos DB that stores the timestamp of the document’s last modification.

    User's image

    Here is a similar thread that might be helpful to you.

     https://zcusa.951200.xyz/en-us/answers/questions/699608/azure-cosmosdb-api-for-mongodb-ttl-index-failed-to
    For more information, please refer the documents:

    https://zcusa.951200.xyz/en-us/azure/cosmos-db/mongodb/time-to-live?source=recommendations

    https://zcusa.951200.xyz/en-us/azure/cosmos-db/nosql/time-to-live?source=recommendations

    https://zcusa.951200.xyz/en-us/azure/cosmos-db/mongodb/indexing?source=recommendations

    Hope this helps. Do let us know if you any further queries.

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.