MongoDB - Cursors
db.collection.find()查询集合会返回一个包含查到的文档的游标。在mongo shell中,如果没有定义一个变量来该游标的内容,默认会迭代返回20个文档。
> db.users.count();db.users.count();
26
> db.users.find();db.users.find();
{ "_id" : ObjectId("5667dbba614c1d1953bce9ba"), "name" : "yb1", "age" : 1, "status" : "A" }
{ "_id" : ObjectId("5667dbba614c1d1953bce9bb"), "name" : "yb2", "age" : 2, "status" : "B" }
{ "_id" : ObjectId("5667dbba614c1d1953bce9bc"), "name" : "yb3", "age" : 3, "status" : "C" }
{ "_id" : ObjectId("5667dbba614c1d1953bce9bd"), "name" : "yb4", "age" : 4, "status" : "D" }
{ "_id" : ObjectId("5667dbba614c1d1953bce9be"), "name" : "yb5", "age" : 5, "status" : "E" }
{ "_id" : ObjectId("5667dbba614c1d1953bce9bf"), "name" : "yb6", "age" : 6, "status" : "F" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c0"), "name" : "yb7", "age" : 7, "status" : "G" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c1"), "name" : "yb8", "age" : 8, "status" : "H" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c2"), "name" : "yb9", "age" : 9, "status" : "I" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c3"), "name" : "yb10", "age" : 10, "status" : "J" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c4"), "name" : "yb11", "age" : 11, "status" : "K" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c5"), "name" : "yb12", "age" : 12, "status" : "L" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c6"), "name" : "yb13", "age" : 13, "status" : "M" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c7"), "name" : "yb14", "age" : 14, "status" : "N" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c8"), "name" : "yb15", "age" : 15, "status" : "O" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c9"), "name" : "yb16", "age" : 16, "status" : "P" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9ca"), "name" : "yb17", "age" : 17, "status" : "Q" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9cb"), "name" : "yb18", "age" : 18, "status" : "R" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9cc"), "name" : "yb19", "age" : 19, "status" : "S" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9cd"), "name" : "yb20", "age" : 20, "status" : "T" }
Type "it" for more
>
手动迭代游标:
> var myCursor = db.users.find();
> myCursor
{ "_id" : ObjectId("5667dbba614c1d1953bce9ba"), "name" : "yb1", "age" : 1, "status" : "A" }
{ "_id" : ObjectId("5667dbba614c1d1953bce9bb"), "name" : "yb2", "age" : 2, "status" : "B" }
{ "_id" : ObjectId("5667dbba614c1d1953bce9bc"), "name" : "yb3", "age" : 3, "status" : "C" }
{ "_id" : ObjectId("5667dbba614c1d1953bce9bd"), "name" : "yb4", "age" : 4, "status" : "D" }
{ "_id" : ObjectId("5667dbba614c1d1953bce9be"), "name" : "yb5", "age" : 5, "status" : "E" }
{ "_id" : ObjectId("5667dbba614c1d1953bce9bf"), "name" : "yb6", "age" : 6, "status" : "F" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c0"), "name" : "yb7", "age" : 7, "status" : "G" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c1"), "name" : "yb8", "age" : 8, "status" : "H" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c2"), "name" : "yb9", "age" : 9, "status" : "I" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c3"), "name" : "yb10", "age" : 10, "status" : "J" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c4"), "name" : "yb11", "age" : 11, "status" : "K" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c5"), "name" : "yb12", "age" : 12, "status" : "L" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c6"), "name" : "yb13", "age" : 13, "status" : "M" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c7"), "name" : "yb14", "age" : 14, "status" : "N" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c8"), "name" : "yb15", "age" : 15, "status" : "O" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9c9"), "name" : "yb16", "age" : 16, "status" : "P" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9ca"), "name" : "yb17", "age" : 17, "status" : "Q" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9cb"), "name" : "yb18", "age" : 18, "status" : "R" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9cc"), "name" : "yb19", "age" : 19, "status" : "S" }
{ "_id" : ObjectId("5667dbbb614c1d1953bce9cd"), "name" : "yb20", "age" : 20, "status" : "T" }
Type "it" for more
>
> var myCursor = db.users.find();
> while(myCursor.hasNext()){print(tojson(myCursor.next()));}
{
"_id" : ObjectId("5667dbba614c1d1953bce9ba"),
"name" : "yb1",
"age" : 1,
"status" : "A"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bb"),
"name" : "yb2",
"age" : 2,
"status" : "B"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bc"),
"name" : "yb3",
"age" : 3,
"status" : "C"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bd"),
"name" : "yb4",
"age" : 4,
"status" : "D"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9be"),
"name" : "yb5",
"age" : 5,
"status" : "E"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bf"),
"name" : "yb6",
"age" : 6,
"status" : "F"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c0"),
"name" : "yb7",
"age" : 7,
"status" : "G"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c1"),
"name" : "yb8",
"age" : 8,
"status" : "H"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c2"),
"name" : "yb9",
"age" : 9,
"status" : "I"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c3"),
"name" : "yb10",
"age" : 10,
"status" : "J"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c4"),
"name" : "yb11",
"age" : 11,
"status" : "K"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c5"),
"name" : "yb12",
"age" : 12,
"status" : "L"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c6"),
"name" : "yb13",
"age" : 13,
"status" : "M"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c7"),
"name" : "yb14",
"age" : 14,
"status" : "N"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c8"),
"name" : "yb15",
"age" : 15,
"status" : "O"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c9"),
"name" : "yb16",
"age" : 16,
"status" : "P"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9ca"),
"name" : "yb17",
"age" : 17,
"status" : "Q"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cb"),
"name" : "yb18",
"age" : 18,
"status" : "R"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cc"),
"name" : "yb19",
"age" : 19,
"status" : "S"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cd"),
"name" : "yb20",
"age" : 20,
"status" : "T"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9ce"),
"name" : "yb21",
"age" : 21,
"status" : "U"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cf"),
"name" : "yb22",
"age" : 22,
"status" : "V"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9d0"),
"name" : "yb23",
"age" : 23,
"status" : "W"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9d1"),
"name" : "yb24",
"age" : 24,
"status" : "X"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9d2"),
"name" : "yb25",
"age" : 25,
"status" : "Y"
}
{
"_id" : ObjectId("5667dbbc614c1d1953bce9d3"),
"name" : "yb26",
"age" : 26,
"status" : "Z"
}
>
> var myCursor = db.users.find();
> while(myCursor.hasNext()){printjson(myCursor.next());}
{
"_id" : ObjectId("5667dbba614c1d1953bce9ba"),
"name" : "yb1",
"age" : 1,
"status" : "A"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bb"),
"name" : "yb2",
"age" : 2,
"status" : "B"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bc"),
"name" : "yb3",
"age" : 3,
"status" : "C"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bd"),
"name" : "yb4",
"age" : 4,
"status" : "D"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9be"),
"name" : "yb5",
"age" : 5,
"status" : "E"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bf"),
"name" : "yb6",
"age" : 6,
"status" : "F"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c0"),
"name" : "yb7",
"age" : 7,
"status" : "G"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c1"),
"name" : "yb8",
"age" : 8,
"status" : "H"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c2"),
"name" : "yb9",
"age" : 9,
"status" : "I"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c3"),
"name" : "yb10",
"age" : 10,
"status" : "J"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c4"),
"name" : "yb11",
"age" : 11,
"status" : "K"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c5"),
"name" : "yb12",
"age" : 12,
"status" : "L"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c6"),
"name" : "yb13",
"age" : 13,
"status" : "M"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c7"),
"name" : "yb14",
"age" : 14,
"status" : "N"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c8"),
"name" : "yb15",
"age" : 15,
"status" : "O"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c9"),
"name" : "yb16",
"age" : 16,
"status" : "P"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9ca"),
"name" : "yb17",
"age" : 17,
"status" : "Q"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cb"),
"name" : "yb18",
"age" : 18,
"status" : "R"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cc"),
"name" : "yb19",
"age" : 19,
"status" : "S"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cd"),
"name" : "yb20",
"age" : 20,
"status" : "T"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9ce"),
"name" : "yb21",
"age" : 21,
"status" : "U"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cf"),
"name" : "yb22",
"age" : 22,
"status" : "V"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9d0"),
"name" : "yb23",
"age" : 23,
"status" : "W"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9d1"),
"name" : "yb24",
"age" : 24,
"status" : "X"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9d2"),
"name" : "yb25",
"age" : 25,
"status" : "Y"
}
{
"_id" : ObjectId("5667dbbc614c1d1953bce9d3"),
"name" : "yb26",
"age" : 26,
"status" : "Z"
}
>
> var myCursor = db.users.find();
> myCursor.forEach(printjson);
{
"_id" : ObjectId("5667dbba614c1d1953bce9ba"),
"name" : "yb1",
"age" : 1,
"status" : "A"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bb"),
"name" : "yb2",
"age" : 2,
"status" : "B"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bc"),
"name" : "yb3",
"age" : 3,
"status" : "C"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bd"),
"name" : "yb4",
"age" : 4,
"status" : "D"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9be"),
"name" : "yb5",
"age" : 5,
"status" : "E"
}
{
"_id" : ObjectId("5667dbba614c1d1953bce9bf"),
"name" : "yb6",
"age" : 6,
"status" : "F"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c0"),
"name" : "yb7",
"age" : 7,
"status" : "G"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c1"),
"name" : "yb8",
"age" : 8,
"status" : "H"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c2"),
"name" : "yb9",
"age" : 9,
"status" : "I"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c3"),
"name" : "yb10",
"age" : 10,
"status" : "J"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c4"),
"name" : "yb11",
"age" : 11,
"status" : "K"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c5"),
"name" : "yb12",
"age" : 12,
"status" : "L"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c6"),
"name" : "yb13",
"age" : 13,
"status" : "M"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c7"),
"name" : "yb14",
"age" : 14,
"status" : "N"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c8"),
"name" : "yb15",
"age" : 15,
"status" : "O"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9c9"),
"name" : "yb16",
"age" : 16,
"status" : "P"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9ca"),
"name" : "yb17",
"age" : 17,
"status" : "Q"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cb"),
"name" : "yb18",
"age" : 18,
"status" : "R"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cc"),
"name" : "yb19",
"age" : 19,
"status" : "S"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cd"),
"name" : "yb20",
"age" : 20,
"status" : "T"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9ce"),
"name" : "yb21",
"age" : 21,
"status" : "U"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9cf"),
"name" : "yb22",
"age" : 22,
"status" : "V"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9d0"),
"name" : "yb23",
"age" : 23,
"status" : "W"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9d1"),
"name" : "yb24",
"age" : 24,
"status" : "X"
}
{
"_id" : ObjectId("5667dbbb614c1d1953bce9d2"),
"name" : "yb25",
"age" : 25,
"status" : "Y"
}
{
"_id" : ObjectId("5667dbbc614c1d1953bce9d3"),
"name" : "yb26",
"age" : 26,
"status" : "Z"
}
>
使用迭代器索引
在mongo shell中,可以使用toArray()方法来迭代游标,并以array的形式返回游标的内容
> var myCursor = db.users.find();
> var usersArray = myCursor.toArray();
> var myDocument = usersArray[3]
> myDocumentmyDocument
{
"_id" : ObjectId("5667dbba614c1d1953bce9bd"),
"name" : "yb4",
"age" : 4,
"status" : "D"
}
>
也可以直接在游标上加下标的方式,查看文档:(cursor[index])
> var myCursor =db.users.find();
> var myCursor =myCursor[3];
> myCursormyCursor
{
"_id" : ObjectId("5667dbba614c1d1953bce9bd"),
"name" : "yb4",
"age" : 4,
"status" : "D"
}
>
cursor[index]等价于cursor.toArray()[index]
缺省情况下,在会话处于非活跃状态10分钟后,或者客户端已经遍历了游标的内容,mongodb会自动关闭游标。
如果想修改超时时间,使用cursor的时候,可以使用cursor.addOption()来修改noTimeout标记:
var myCursor = db.users.find().addOption(DBQuery.Option.noTimeout);
记得要及时关闭游标!
MongoDB - Cursors的更多相关文章
- mongodb collection method
https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/ db.coll_test.getIndexes()# ...
- Mongodb常用命令介绍
查看命令的方式: 1.在shell中运行db.listCommands() 2.在浏览器中访问管理员接口:http://ipaddress:28017/_commands 下面介绍在Mongodb中最 ...
- MongoDB使用小结:一些不常见的经验分享
最近一年忙碌于数据处理相关的工作,跟MongoDB打交道极多,以下为实践过程中的Q&A,后续会不定期更新补充. 另有<MongoDB使用小结:一些常用操作分享>,注:本文完成时Mo ...
- mongoDB学习记录---PHP扩展的find返回值
最近的一个项目中用到了MongoDB,主要是使用MongoDB的PHP扩展.MongoDB的扩展中用于一个用于查询的方法是find().下面针对在理解MongoDB扩展的find()方法中做的实验做个 ...
- MongoDB基本管理命令
MongoDB是一个NoSQL数据库系统:一个数据库可以包含多个集合(Collection),每个集合对应于关系数据库中的表:而每个集合中 可以存储一组由列标识的记录,列是可以自由定义的,非常灵活,由 ...
- 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用
在花了不少时间研究学习了MongoDB数据库的相关知识,以及利用C#对MongoDB数据库的封装.测试应用后,决定花一些时间来总结一下最近的研究心得,把这个数据库的应用单独作为一个系列来介绍,希望从各 ...
- MongoDB 常用故障排查工具
1.profile profiling levels: 0,关闭profile:1,只抓取slow查询:2,抓取所有数据. 启动profile并且设置Profile级别: 可以通过mongo shel ...
- MongoDB ServerStatus返回信息
ServerStatus返回信息 ServerStatus返回mongodb中很多信息 http://docs.mongodb.org/manual/reference/command/serverS ...
- MongoDB—— 读操作 Core MongoDB Operations (CRUD)
本文主要介绍内容:从MongoDB中请求数据的不同的方法 Note:All of the examples in this document use the mongo shell interface ...
随机推荐
- [NOI2017]整数
[NOI2017]整数 题目大意: \(n(n\le10^6)\)次操作维护一个长度为\(30n\)的二进制整数\(x\),支持以下两种操作: 将这个整数加上\(a\cdot2^b(|a|\le10^ ...
- JavaScript中的with语句
执行环境的类型有两种:全局执行环境和局部执行环境(函数执行环境). 1.全局执行环境的变量对象是window对象,是JS代码开始运行时的默认环境.全局执行环境的变量对象始终都是作用域链中的最后一个对象 ...
- [转]115个Java面试题和答案——终极列表(下)
第一篇讨论了面向对象编程和它的特点,关于Java和它的功能的常见问题,Java的集合类,垃圾收集器,本章主要讨论异常处理,Java小应用程序,Swing,JDBC,远程方法调用(RMI),Servle ...
- python 实现简单的KNN算法
from numpy import * import operator def createDataSet(): group = array([[3,104],[2,100],[1,81],[101, ...
- MYSQL复习笔记5-select-from-where子句
Date: 20140125Auth: Jin参考:http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#select一.select子句主要定 ...
- sql 查询慢的48个原因分析
sql 查询慢的48个原因分析. server memory 服务器配置选项配置为物理内存的 1.5 倍(虚拟内存大小设置的一半). 字句同时执行,SQL SERVER根据系统的负载情况决定最优的 ...
- mongo 实时同步工具 mongosync
文档地址:https://github.com/Qihoo360/mongosync/wiki/%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B #数据全量备份mongodum ...
- 《Go语言实战》笔记之协程同步 sync.WaitGroup
原文地址(欢迎互换友链): http://www.niu12.com/article/8 sync 包提供同步 goroutine 的功能 <p>文档介绍</p><cod ...
- yum安装 lnmp (linux+nginx+php7.1+mysql5.7)
1.第一步先更新yum update 2.yum安装nginx安装nginx最新源:yum localinstall http://nginx.org/packages/centos/7/noarch ...
- PHP中 "{}" 大括号的用法和总结
在PHP中,大括号“{}”可以起到如下作用: 1.将多个独立语句合并为一个复合语句,例如 if ... else ...中经常如此使用 2.在变量间接引用中进行定界,避免歧义.例如 ${$my_var ...