Empty a collection in cosmoDB with mongo API

MikeZheng-3371 46 Reputation points
2022-06-23T16:11:14.247+00:00

Other than delete and re-create a collection, what is the easiest way to empty a collection of cosmos DB with mongo API?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,902 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Oury Ba-MSFT 20,911 Reputation points Microsoft Employee Moderator
    2022-06-23T19:25:45.743+00:00

    Hi @MikeZheng-3371 If you are looking to empty all documents in Cosmos DB Mongo DB you can create a TTL index using the MongoDB shell.

    globaldb:PRIMARY> db.coll.createIndex({"_ts":1}, {expireAfterSeconds: 10})
    {
    "_t" : "CreateIndexesResponse",
    "ok" : 1,
    "createdCollectionAutomatically" : true,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 4

    214350-image.png

    }
    The command in the above example will create an index with TTL functionality. Once the index is created, the database will automatically delete any documents in that collection that have not been modified in the last 10 seconds. Expire data with Azure Cosmos DB's API for MongoDB

    You will need to drop the index once all documents are deleted in the collection. db.collection.dropIndex()

    Please let me know if this is what you are looking for.

    Regards,
    Oury


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.