MongoDB 如何限制结果和分页显示
2013-01-28 10:30
351 查看
原文地址:http://www.oschina.net/translate/mongodb-how-to-limit-results
在这篇文章我们将看一下怎样在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限制结果同样怎样去分页显示。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文章感兴趣,你能在这儿找到它们:
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 如何限制结果和分页显示
- php中如何分页显示查询数据结果
- Struts中如何实现查询结果分页显示
- Struts中如何实现查询结果分页显示
- 如何分页显示数据库查询结果?
- 面试题:如何对查询结果进行分页显示
- Struts中如何实现查询结果分页显示
- Struts 中如何实现查询结果分页显示
- 如何将查询结果显示在选择屏幕上的代码
- 如何在WinForm中对DataGrid进行分页显示收藏
- 【Reporting Services 报表开发】— 如何设置报表分页列标题每一页都显示
- jqueryEasyui的datagrid分页显示数据,后台用thinkphp,如何实现?
- 如何在oracle中限制返回结果集的大小
- 如何在搜索结果出来之前,让页面显示“等待中。。。”
- 在使用Hibernate时,因为一个查询需要更多的表连接而要使用SQL来解决性能问题。然而返回的结果集中包含了没有映射的Entity类中的表字段,在这个SQL中还有使用如何将层次关系的父子结点显示为横行
- 去掉liferay分页的显示结果数和分页功能
- 检索结果的分页显示实现(php)
- 如何在Sql Server上使用一条SQL查询结果总数并且分页
- 如何在搜索结果出来之前,让页面显示“等待中...”