Cara menggunakan ttl collection mongodb

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Amazon DocumentDB (dengan kompatibilitas MongoDB) adalah layanan basis data dokumen yang cepat, dapat diskalakan, sangat tersedia, dan terkelola penuh yang mendukung beban kerja MongoDB. Amazon DocumentDB kompatibel dengan API MongoDB 3.6 dan 4.0. Bagian ini mencantumkan fungsionalitas yang didukung. Untuk dukungan menggunakan API dan driver MongoDB, silakan berkonsultasi dengan Forum Komunitas MongoDB. Untuk dukungan menggunakan layanan Amazon DocumentDB, silakan hubungi tim dukungan AWS yang sesuai. Untuk perbedaan fungsional antara Amazon DocumentDB dan MongoDB, silakan lihat Perbedaan Fungsional: Amazon DocumentDB dan MongoDB.

Perintah dan operator MongoDB yang hanya berlaku untuk internal atau tidak berlaku untuk layanan terkelola penuh tidak didukung dan tidak termasuk dalam daftar fungsi yang didukung.

Kami telah menambahkan lebih dari 50+ kemampuan tambahan sejak diluncurkan, dan akan terus bekerja mundur dari pelanggan kami untuk memberikan kemampuan yang mereka butuhkan. Untuk informasi tentang peluncuran terbaru, lihat Pengumuman Amazon DocumentDB.

Jika ada fitur yang tidak didukung yang Anda ingin kami bangun, beri tahu kami dengan mengirimkan email dengan accountID Anda, fitur yang diminta, dan kasus penggunaan ke tim layanan Amazon DocumentDB.

When you create a , you can set up automatic removal of documents older than a specified number of seconds by using the

expireAfterSeconds parameter:

db.createCollection(
"weather24h",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "hours"
},
expireAfterSeconds: 86400
}
)

The expiration threshold is the

{
"metadata": {"sensorId": 5578, "type": "temperature"},
"timestamp": ISODate("2021-05-18T10:00:00.000Z"),
"temp": 12
}
0 field value plus the specified number of seconds. Consider the following document in the
{
"metadata": {"sensorId": 5578, "type": "temperature"},
"timestamp": ISODate("2021-05-18T10:00:00.000Z"),
"temp": 12
}
1 collection:

{
"metadata": {"sensorId": 5578, "type": "temperature"},
"timestamp": ISODate("2021-05-18T10:00:00.000Z"),
"temp": 12
}

The document would expire from the database at

{
"metadata": {"sensorId": 5578, "type": "temperature"},
"timestamp": ISODate("2021-05-18T10:00:00.000Z"),
"temp": 12
}
2. Once all documents in a bucket are expired, the background task that removes expired buckets removes the bucket during the next run. See

for more information.

Enable Automatic Removal on a Collection

To enable automatic removal of documents for an existing , issue the following command:

db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})

Change the expireAfterSeconds Parameter

To change the expireAfterSeconds parameter value, issue the following command:

db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})

Retrieve the Current Value of expireAfterSeconds

To retrieve the current value of expireAfterSeconds, use the command:

db.runCommand( { listCollections: 1 } )

The result document contains a document for the time series collection which contains the

db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})
0 field.

{
cursor: {
id: <number>,
ns: 'test.$cmd.listCollections',
firstBatch: [
{
name: <string>,
type: 'timeseries',
options: {
expireAfterSeconds: <number>,
timeseries: { ... }
},
...
},
...
]
}
}

Disable Automatic Removal

To disable automatic removal, use the command to set expireAfterSeconds to

db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})
3:

db.runCommand({
collMod: "weather24h",
expireAfterSeconds: "off"
})

Behavior

Timing of Delete Operations

MongoDB doesn't guarantee that expired data will be deleted immediately upon expiration. Once all documents in a bucket are expired, the background task that removes expired buckets removes the bucket during the next run. The maximum span of time that a single bucket is allowed to cover is controlled by the

db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})
4 of the time series collection:

db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})
4

Covered Time Span

db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})
6 (default)

one hour

db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})
7

24 hours

db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})
8

30 days

The background task that removes expired buckets runs every 60 seconds. Therefore, documents may remain in a collection during the period between the expiration of the document, the expiration of all other documents in the bucket and the running of the background task.

Because the duration of the removal operation depends on the workload of your mongod instance, expired data may exist for some time beyond the 60 second period between runs of the background task.