MongoDB 如何限制结果和分页显示
2013-01-28 00:24
381 查看
在这篇文章我们将看一下怎样在MongoDB限制结果同样怎样去分页显示。MongoDB使用limit去限制许多返回结果,MongoDB使用skip去跳转来自结果集中的记录,使用limit并结合skip能使你在MongoDB中做到分页。
让我们看一下这是怎样工作的
开始插入以下数据到你的MongoDB数据库里
返回任意值,你执行下面语句就行了
这下面是得到的结果
仅仅返回集合中插入第一个和第二个的条目
以下是结果
这里的确显示的是第一个和第二个的条目,但如果我们想给它的名字排序将会怎样?
即使那样我们也能添加排序,运行下面
以下是结果
我们当然也能仅仅是返回其名字而不返回其它,这里在"MongoDB:怎样在你想要的结果包含并排除其它字段"后描述
运行下面
以下是结果
现在让我们看一下怎样返回插入的第三个和第四个的条目,这应该是John和Xavier。这种情况我们需要使用skip我们得在后面添加skip(2)
这里的命令看起来像什么,运行下面语句
以下下是结果
如果你传递的值不在集合条目范围内,你不会得到一个错误显示但也不会得到其结果。如果你跳转第六个条目,你将不会返回任何东西
如果你想在集合中返回的条目经过排序后插入磁盘,你可以使用 $natural参数,这个 $natural返回的条目是经过排序后放在磁盘上的,如果你使用-1,你能得到它们反转后的结果
以下是结果
当使用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文章感兴趣,你能在这儿找到它们:
Install MongoDB as a Windows Service
UPSERTs with MongoDB
How to sort results in MongoDB
Indexes in MongoDB: A quick overview
Multidocument updates with MongoDB
MongoDB: How to include and exclude the fields you want in results
让我们看一下这是怎样工作的
开始插入以下数据到你的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文章感兴趣,你能在这儿找到它们:
Install MongoDB as a Windows Service
UPSERTs with MongoDB
How to sort results in MongoDB
Indexes in MongoDB: A quick overview
Multidocument updates with MongoDB
MongoDB: How to include and exclude the fields you want in results
相关文章推荐
- MongoDB 如何限制结果和分页显示
- MongoDB 如何限制结果和分页显示
- Struts 中如何实现查询结果分页显示
- 面试题:如何对查询结果进行分页显示
- Struts中如何实现查询结果分页显示
- 如何分页显示数据库查询结果?
- php中如何分页显示查询数据结果
- Struts中如何实现查询结果分页显示
- Struts中如何实现查询结果分页显示
- 如何用extjs实现海量数据的快速分页显示
- 如何实现CSS限制字数,超出部份显示点点点...
- VS2010 RDLC 报表 表(table) 分页的时候如何显示表头?
- 如何在keil中显示程序的预处理结果
- 如何用C的CGI实现网页的分页显示功能?
- 如何实现js展开收起(折叠)效果呢?例如:一段内容过长了,我限制他显示为20个字内,但是超出了20的就先用省略号代替,然后后面添加一个展开,点击展开后就显示所有的内容,这时候展开按钮就变成了 折叠按钮
- 将sharepoint中的跨网站、列表查询的结果用SPGridView分页显示
- 如何将主窗体的查询结果在子窗体显示
- 如何在Struts 数据库应用程序中实现分页显示(1)
- 数据库限制结果集行数(可用于分页)
- SQL中如何显示COUNT结果为0的行