限制在MongoDB中返回的文档的2种方法
当您在MongoDB中运行查询时,您可以选择限制返回的文档。您可以在此处指定操作要返回的最大文档数。
例如,如果操作通常返回 2,000 个文档,但您指定限制为 1,000,则仅返回 1,000 个文档。但是,如果操作只返回 500 个文档,则返回所有 500 个文档(因为它们没有违反 1,000 个文档的限制)。
这有点像在SQL Server中使用子句,或者在MySQL,MariaDB,PostgreSQL和SQLite中使用子句。[TOP](https://database.guide/limit-the-rows-returned-in-a-sql-server-query-top-clause/ "Limit the Rows Returned in a SQL Server Query by using the TOP Clause")``LIMIT
限制返回的文档有助于防止返回和处理过多的文档,从而提高性能。
在MongoDB中,我们可以使用方法或聚合运算符来限制返回的文档。cursor.limit()``$limit
方法cursor.limit()
假设我们有一个包含以下文档的集合:pets
{ “_id” : 1, “name” : “Wag”, “type” : “Dog”, “weight” : 20 } { “_id” : 2, “name” : “Bark”, “type” : “Dog”, “weight” : 10 } { “_id” : 3, “name” : “Meow”, “type” : “Cat”, “weight” : 7 } { “_id” : 4, “name” : “Scratch”, “type” : “Cat”, “weight” : 8 } { “_id” : 5, “name” : “Bruce”, “type” : “Bat”, “weight” : 3 } { “_id” : 6, “name” : “Fetch”, “type” : “Dog”, “weight” : 17 } { “_id” : 7, “name” : “Jake”, “type” : “Dog”, “weight” : 30 }
我们可以使用该方法来限制查询此集合时返回的文档数。limit()
例:
db.pets.find().limit(3)
结果:
{ “_id” : 1, “name” : “Wag”, “type” : “Dog”, “weight” : 20 } { “_id” : 2, “name” : “Bark”, “type” : “Dog”, “weight” : 10 } { “_id” : 3, “name” : “Meow”, “type” : “Cat”, “weight” : 7 }
聚合运算符$limit
使用聚合管道时,可以使用聚合运算符将传递到管道中下一阶段的结果限制。$limit
例:
db.pets.aggregate([
{
$match: { }
},
{
$limit : 3
}
])
例:
{ “_id” : 1, “name” : “Wag”, “type” : “Dog”, “weight” : 20 } { “_id” : 2, “name” : “Bark”, “type” : “Dog”, “weight” : 10 } { “_id” : 3, “name” : “Meow”, “type” : “Cat”, “weight” : 7 }
通常,运算符不会像我们这里那样由空文档组成。它通常包括一个查询,用于筛选管道该部分的结果。但在这种情况下,我使用了一个空文档,以使示例更易于遵循。$match
鉴于我在第一阶段传递了一个空文档,我本可以这样做:
db.pets.aggregate([
{
$limit : 3
}
])
无论哪种方式,结果仅限于 3 个文档。
在聚合管道中使用时,操作员可能位于管道中间的某个位置。如果是这样,它将有限的文档传递到下一阶段。$limit
这里有一个例子来说明我的意思。
db.pets.aggregate([
{
$match: { }
},
{
$limit : 3
},
{
$match: { "type": "Cat" }
}
])
结果:
{ “_id” : 3, “name” : “Meow”, “type” : “Cat”, “weight” : 7 }
有限的结果中只有一只猫(即使原始文档中有两只猫),所以只有那只猫在第三阶段被匹配。
这与执行以下操作不同。
db.pets.aggregate([
{
$match: { "type": "Cat" }
},
{
$limit : 3
}
])
结果:
{ “_id” : 3, “name” : “Meow”, “type” : “Cat”, “weight” : 7 } { “_id” : 4, “name” : “Scratch”, “type” : “Cat”, “weight” : 8 }
在这种情况下,我们在原始文档中搜索了猫,然后将结果限制为仅 3 只。鉴于只有2只猫,对结果没有影响。$limit
限制排序的文档
如果在对文档进行排序后限制结果,则限制将考虑排序。
例:
db.pets.aggregate([
{
$sort: { "_id": -1 }
},
{
$limit : 3
}
])
结果:
{ “_id” : 7, “name” : “Jake”, “type” : “Dog”, “weight” : 30 } { “_id” : 6, “name” : “Fetch”, “type” : “Dog”, “weight” : 17 } { “_id” : 5, “name” : “Bruce”, “type” : “Bat”, “weight” : 3 }
在本例中,我按字段的降序(指定的降序)对所有文档进行排序。_id``-1
这里再次按升序排列(指定升序的值):1
db.pets.aggregate([
{
$sort: { "_id": 1 }
},
{
$limit : 3
}
])
结果:
{ “_id” : 1, “name” : “Wag”, “type” : “Dog”, “weight” : 20 } { “_id” : 2, “name” : “Bark”, “type” : “Dog”, “weight” : 10 } { “_id” : 3, “name” : “Meow”, “type” : “Cat”, “weight” : 7 }
在显式排序后限制结果时,请确保排序操作使用了稳定的排序。
稳定排序是每次执行时返回相同排序顺序的_排序_。另一方面,_不稳定的_排序是在多次执行时可能会返回不同的排序顺序的排序。
若要确保执行稳定排序,请在排序中至少包含一个仅包含唯一值的字段(例如,字段)。_id
更多信息
有关更多信息,请参阅 MongoDB 文档:
[cursor.limit()](https://docs.mongodb.com/manual/reference/method/cursor.limit/ "https://docs.mongodb.com/manual/reference/method/cursor.limit/")$limit(聚合)