MongoDB 如何限制结果和分页显示,MongoDB使用l


在这篇文章我们将看一下怎样在MongoDB限制结果同样怎样去分页显示。MongoDB使用limit去限制许多返回结果,MongoDB使用skip去跳转来自结果集中的记录,使用limit并结合skip能使你在MongoDB中做到分页。

让我们看一下这是怎样工作的:

开始插入以下数据到你的MongoDB数据库里:

db.Blog.insert( { name : "Denis",  age : 20, city : "Princeton" } )
db.Blog.insert( { name : "Abe",    age : 30, city : "Amsterdam" } )
db.Blog.insert( { name : "John",   age : 40, city : "New York"  } )
db.Blog.insert( { name : "Xavier", age : 10, city : "Barcelona" } )
db.Blog.insert( { name : "Zen",    age : 50, city : "Kyoto"     } )

返回任意值,你执行下面语句就行了:

db.Blog.find()

这下面是得到的结果:

{ "_id" : ObjectId("51028ae0a8c33b71ed76a807"), "name" : "Denis", "age" : 20, "city" : "Princeton" }
{ "_id" : ObjectId("51028ae0a8c33b71ed76a808"), "name" : "Abe", "age" : 30, "city" : "Amsterdam" }
{ "_id" : ObjectId("51028ae2a8c33b71ed76a809"), "name" : "John", "age" : 40, "city" : "New York" }
{ "_id" : ObjectId("51028ae2a8c33b71ed76a80a"), "name" : "Xavier", "age" : 10, "city" : "Barcelona" }
{ "_id" : ObjectId("51028ae4a8c33b71ed76a80b"), "name" : "Zen", "age" : 50, "city" : "Kyoto" }

限制 MongoDB 的结果集数量

仅仅返回集合中插入第一个和第二个的条目:

db.Blog.find().limit(2)

以下是结果:

{ "_id" : ObjectId("5103e22c88a39c3c0b2585e1"), "name" : "Denis", "age" : 20, "city" : "Princeton" }
{ "_id" : ObjectId("5103e22d88a39c3c0b2585e2"), "name" : "Abe", "age" : 30, "city" : "Amsterdam" }

这里的确显示的是第一个和第二个的条目,但如果我们想给它的名字排序将会怎样?

即使那样我们也能添加排序,运行下面:

db.Blog.find().sort({name: 1}).limit(2)

以下是结果:

{ "_id" : ObjectId("5103e22d88a39c3c0b2585e2"), "name" : "Abe", "age" : 30, "city" : "Amsterdam" }
{ "_id" : ObjectId("5103e22c88a39c3c0b2585e1"), "name" : "Denis", "age" : 20, "city" : "Princeton" }

我们当然也能仅仅是返回其名字而不返回其它,这里在"MongoDB:怎样在你想要的结果包含并排除其它字段"后描述:

运行下面:

db.Blog.find(null, {name: 1, _id: 0}).sort({name: 1}).limit(2)

以下是结果:

{ "name" : "Abe" }
{ "name" : "Denis" }

MongoDB 数据分页

现在让我们看一下怎样返回插入的第三个和第四个的条目,这应该是John和Xavier。这种情况我们需要使用skip我们得在后面添加skip(2)

这里的命令看起来像什么,运行下面语句:

db.Blog.find(null, {name: 1, _id: 0}).sort({name: 1}).limit(2).skip(2)

以下下是结果:

{ "name" : "John" }
{ "name" : "Xavier" }

如果你传递的值不在集合条目范围内,你不会得到一个错误显示但也不会得到其结果。如果你跳转第六个条目,你将不会返回任何东西:

db.Blog.find(null, {name: 1, _id: 0}).sort({name: 1}).limit(2).skip(6)

如果你想在集合中返回的条目经过排序后插入磁盘,你可以使用 $natural参数,这个 $natural返回的条目是经过排序后放在磁盘上的,如果你使用-1,你能得到它们反转后的结果:

db.Blog.find().sort( { $natural: -1 } )

以下是结果:

{ "_id" : ObjectId("5103eaa688a39c3c0b2585ed"), "name" : "Zen", "age" : 50, "city" : "Kyoto" }
{ "_id" : ObjectId("5103eaa588a39c3c0b2585ec"), "name" : "Xavier", "age" : 10, "city" : "Barcelona" }
{ "_id" : ObjectId("5103eaa588a39c3c0b2585eb"), "name" : "John", "age" : 40, "city" : "New York" }
{ "_id" : ObjectId("5103eaa588a39c3c0b2585ea"), "name" : "Abe", "age" : 30, "city" : "Amsterdam" }
{ "_id" : ObjectId("5103eaa588a39c3c0b2585e9"), "name" : "Denis", "age" : 20, "city" : "Princeton" }

当使用sort()出现的一个警告,要注意下面的限制:

Warning :排序功能需要整个排序在32兆字节里才能够完成。当排序选项超过了32兆,MongoDB将返回一个错误。应使用cursor.limit()或在字段中创建一个角标那样你能避免这个错误(Warning The sort function requires that the entire sort be able to complete within 32 megabytes. When the sort option consumes more than 32 megabytes, MongoDB will return an error. Use cursor.limit(), or create an index on the field that you’re sorting to avoid this error.)

英文原文:MongoDB: How to limit results and how to page through results

这是所有的文章,如果你对我其它MongoDB文章感兴趣,你能在这儿找到它们:

Install MongoDB as a Windows ServiceUPSERTs with MongoDBHow to sort results in MongoDBIndexes in MongoDB: A quick overviewMultidocument updates with MongoDBMongoDB: How to include and exclude the fields you want in results

相关内容