You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/mongodb/time-to-live.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: MongoDB per-document TTL feature in Azure Cosmos DB
2
+
title: MongoDB per-document time-to-live feature in Azure Cosmos DB
3
3
description: Learn how to set time to live value for documents using Azure Cosmos DB's API for MongoDB, to automatically purge them from the system after a period of time.
4
4
author: gahl-levy
5
5
ms.author: gahllevy
@@ -8,13 +8,13 @@ ms.subservice: mongodb
8
8
ms.devlang: csharp
9
9
# ms.devlang: csharp, java, javascript
10
10
ms.topic: how-to
11
-
ms.date: 02/16/2022
11
+
ms.date: 06/05/2024
12
12
ms.custom: devx-track-csharp
13
13
---
14
14
# Expire data with Azure Cosmos DB's API for MongoDB
Time-to-live (TTL) functionality allows the database to automatically expire data. Azure Cosmos DB's API for MongoDB utilizes Azure Cosmos DB's core TTL capabilities. Two modes are supported: setting a default TTL value on the whole collection, and setting individual TTL values for each document. The logic governing TTL indexes and per-document TTL values in Azure Cosmos DB's API for MongoDB is the [same as in Azure Cosmos DB](indexing.md).
17
+
Time-to-live (TTL) functionality allows the database to automatically expire data. Azure Cosmos DB for MongoDB utilizes Azure Cosmos DB's core TTL capabilities. Two modes are supported: setting a default TTL value on the whole collection, and setting individual TTL values for each document. The logic governing TTL indexes and per-document TTL values in Azure Cosmos DB's API for MongoDB is the [same as in Azure Cosmos DB](indexing.md).
18
18
19
19
## TTL indexes
20
20
To enable TTL universally on a collection, a ["TTL index" (time-to-live index)](indexing.md) needs to be created. The TTL index is an index on the `_ts` field with an "expireAfterSeconds" value.
The command in the above example will create an index with TTL functionality.
27
+
The command in the above example creates an index with TTL functionality.
28
28
29
29
The output of the command includes various metadata:
30
30
@@ -38,7 +38,7 @@ The output of the command includes various metadata:
38
38
}
39
39
```
40
40
41
-
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.
41
+
Once the index is created, the database will automatically delete any documents in that collection that haven't been modified in the last 10 seconds.
42
42
43
43
> [!NOTE]
44
44
> `_ts` is an Azure Cosmos DB-specific field and is not accessible from MongoDB clients. It is a reserved (system) property that contains the timestamp of the document's last modification.
Per-document TTL values are also supported. The document(s) must contain a root-level property "ttl" (lower-case), and a TTL index as described above must have been created for that collection. TTL values set on a document will override the collection's TTL value.
64
+
Per-document TTL values are also supported. The document(s) must contain a root-level property "ttl" (lower-case), and a TTL index as described above must have been created for that collection. TTL values set on a document overrides the collection's TTL value.
65
65
66
-
The TTL value must be an int32. Alternatively, an int64 that fits in an int32, or a double with no decimal part that fits in an int32. Values for the TTL property that do not conform to these specifications are allowed but not treated as a meaningful document TTL value.
66
+
The TTL value must be an int32. Alternatively, an int64 that fits in an int32, or a double with no decimal part that fits in an int32. Values for the TTL property that don't conform to these specifications are allowed but not treated as a meaningful document TTL value.
67
67
68
-
The TTL value for the document is optional; documents without a TTL value can be inserted into the collection. In this case, the collection's TTL value will be honored.
68
+
The TTL value for the document is optional; documents without a TTL value can be inserted into the collection. In this case, the collection's TTL value is honored.
69
69
70
70
The following documents have valid TTL values. Once the documents are inserted, the document TTL values override the collection's TTL values. So, the documents will be removed after 20 seconds.
The following documents have invalid TTL values. The documents will be inserted, but the document TTL value will not be honored. So, the documents will be removed after 10 seconds because of the collection's TTL value.
78
+
The following documents have invalid TTL values. The documents are inserted, but the document TTL value won't be honored. So, the documents will be removed after 10 seconds because of the collection's TTL value.
79
79
80
80
```JavaScript
81
81
globaldb:PRIMARY>db.coll.insert({id:1, location:"Paris", ttl:20.5}) //TTL value contains non-zero decimal part.
0 commit comments