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的更多相关文章

  1. mongodb collection method

    https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/ db.coll_test.getIndexes()# ...

  2. Mongodb常用命令介绍

    查看命令的方式: 1.在shell中运行db.listCommands() 2.在浏览器中访问管理员接口:http://ipaddress:28017/_commands 下面介绍在Mongodb中最 ...

  3. MongoDB使用小结:一些不常见的经验分享

    最近一年忙碌于数据处理相关的工作,跟MongoDB打交道极多,以下为实践过程中的Q&A,后续会不定期更新补充. 另有<MongoDB使用小结:一些常用操作分享>,注:本文完成时Mo ...

  4. mongoDB学习记录---PHP扩展的find返回值

    最近的一个项目中用到了MongoDB,主要是使用MongoDB的PHP扩展.MongoDB的扩展中用于一个用于查询的方法是find().下面针对在理解MongoDB扩展的find()方法中做的实验做个 ...

  5. MongoDB基本管理命令

    MongoDB是一个NoSQL数据库系统:一个数据库可以包含多个集合(Collection),每个集合对应于关系数据库中的表:而每个集合中 可以存储一组由列标识的记录,列是可以自由定义的,非常灵活,由 ...

  6. 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用

    在花了不少时间研究学习了MongoDB数据库的相关知识,以及利用C#对MongoDB数据库的封装.测试应用后,决定花一些时间来总结一下最近的研究心得,把这个数据库的应用单独作为一个系列来介绍,希望从各 ...

  7. MongoDB 常用故障排查工具

    1.profile profiling levels: 0,关闭profile:1,只抓取slow查询:2,抓取所有数据. 启动profile并且设置Profile级别: 可以通过mongo shel ...

  8. MongoDB ServerStatus返回信息

    ServerStatus返回信息 ServerStatus返回mongodb中很多信息 http://docs.mongodb.org/manual/reference/command/serverS ...

  9. MongoDB—— 读操作 Core MongoDB Operations (CRUD)

    本文主要介绍内容:从MongoDB中请求数据的不同的方法 Note:All of the examples in this document use the mongo shell interface ...

随机推荐

  1. openfire安装完毕后无法登录控制台(忘记密码)的解决方法

    openfire登录管理控制提示: Login failed:make sure your username and password are correct and that you’re an a ...

  2. Unity3D 粒子系统

    我们先来看看Particle System在Inspector视窗中的属性: 1.Transform:可以控制粒子在世界或者本地坐标的改变.但是有点注意的是,如果你改变Scale属性值是不会影响粒子的 ...

  3. 简化js--函数返回

    <script type="text/javascript"> function a(){ return false; } function b(){ return t ...

  4. IntelliJ IDEA maven项目的基础配置

    htt 选择编程风格File->Settings->Apprearance 配置maven路径,同样也是在settings Java Web项目配置 File->Project St ...

  5. 支持向量机(SVM)介绍

    一.了解支持向量机   支持向量机(support vector machings, SVM) 是一种二分类模型,其基本模型定义为特征空间上的间隔最大的线性分类器,其学习策略便是间隔最大化,最终可转化 ...

  6. RTP/RTCP、TCP、UDP、RTMP、RTSP

    OSI中的层 功能 TCP/IP协议族 应用层 文件传输,电子邮件,文件服务,虚拟终端 TFTP,FTP,HTTP,SNMP,SMTP,DNS,RIP,Telnet 表示层 数据格式化,代码转换,数据 ...

  7. shadow 优化

    http://gamedevs.org/uploads/rendering-in-battlefield3.pdf mark 当初看过 ======= sm有两阶段 1生成sm 2采样 第一阶段的优化 ...

  8. Hibernate简介与实例

    一.Hibernate简介 1.什么是Hibernate? Hibernate是数据持久层的一个轻量级框架.数据持久层的框架有很多比如:iBATIS,myBatis,Nhibernate,Siena等 ...

  9. hdu 1408 acdearm &quot;Money, Money, Money&quot;

    #include<stdio.h> int main() { long long int x; while(~scanf("%lld",&x)) { if(x% ...

  10. 【笔记】git 的常用操作命令(持续更新。。。)

    项目正在如火如荼的开展,代码量的繁多不得不令我们运用 git 这个有用的工具去管理我们共同协作的代码 git 在这里不作什么介绍了,百度一大堆的教程 首推廖雪峰老师的:http://www.liaox ...