Database Manual / Introduction / Databases & Collections / Capped Collections

Query a Capped Collection查询封顶集合

When you query a capped collection without specifying a sort order, MongoDB returns results in the same order that they were inserted, meaning the oldest documents are returned first.当你在不指定排序顺序的情况下查询一个有上限的集合时,MongoDB会按照插入的顺序返回结果,这意味着最旧的文档会首先返回。

Use natural ordering to retrieve the most recently inserted elements from the collection efficiently. This is similar to using the tail command on a log file.使用自然顺序高效地从集合中检索最近插入的元素。这类似于在日志文件上使用tail命令。

About this Task关于此任务

Generally, TTL (Time To Live) indexes offer better performance and more flexibility than capped collections. TTL indexes expire and remove data from normal collections based on the value of a date-typed field and a TTL value for the index.通常,TTL(生存时间)索引比上限集合提供更好的性能和更大的灵活性。TTL索引过期,并根据日期类型字段的值和索引的TTL值从正常集合中删除数据。

Capped collections serialize write operations and therefore have worse concurrent insert, update, and delete performance than non-capped collections. Before you create a capped collection, consider if you can use a TTL index instead.封顶集合序列化写操作,因此与非封顶集合相比,其并发插入、更新和删除性能较差。在创建封顶集合之前,请考虑是否可以使用TTL索引。

Multiple Concurrent Writes多个并发写入

If there are concurrent writers to a capped collection, MongoDB does not guarantee that documents are returned in insertion order.如果一个有上限的集合有并发写入程序,MongoDB不保证文档按插入顺序返回。

Before you Begin开始之前

1

Create a capped collection创建一个有上限的集合

db.createCollection("log", { capped: true, size: 100000 } )
2

Insert sample data插入示例数据

db.log.insertMany( [
{
message: "system start",
type: "startup",
time: 1711403508
},
{
message: "user login attempt",
type: "info",
time: 1711403907
},
{
message: "user login fail",
type: "warning",
time: 1711404209
},
{
message: "user login success",
type: "info",
time: 1711404367
},
{
message: "user logout",
type: "info",
time: 1711404555
}
] )

Steps步骤

The following examples show you how to:以下示例向您展示了如何:

Return Documents in Insertion Order按插入顺序返回文件

Query the log collection for documents where type is info, and use the default sort order:查询log集合中typeinfo的文档,并使用默认排序顺序:

db.log.find( { type: "info" } )
[
{
_id: ObjectId("660204b74cabd75abebadbc2"),
message: 'user login attempt',
type: 'info',
time: 1711403907
},
{
_id: ObjectId("660204b74cabd75abebadbc4"),
message: 'user login success',
type: 'info',
time: 1711404367
},
{
_id: ObjectId("660204b74cabd75abebadbc5"),
message: 'user logout',
type: 'info',
time: 1711404555
}
]

Documents are returned in the order that they were inserted.文件按插入顺序返回。

Return Most Recent Documents返回最新文档

To return documents in reverse insertion order (meaning the most recent documents are first), specify the sort() method with the $natural parameter set to -1.要以反向插入顺序返回文档(意味着最新的文档排在第一位),请指定sort()方法,并将$natural参数设置为-1

The following query returns the three most recent documents from the log collection, starting with the most recent document:以下查询从日志集合中返回三个最新文档,从最新文档开始:

db.log.find().sort( { $natural: -1 } ).limit(3)
 [
{
_id: ObjectId("6601f2484cabd75abebadbbb"),
message: 'user logout',
type: 'info',
time: 1711404555
},
{
_id: ObjectId("6601f2484cabd75abebadbba"),
message: 'user login success',
type: 'info',
time: 1711404367
},
{
_id: ObjectId("6601f2484cabd75abebadbb9"),
message: 'user login fail',
type: 'warning',
time: 1711404209
}
]

Learn More了解更多