博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MongoDB - Cursors
阅读量:5059 次
发布时间:2019-06-12

本文共 14317 字,大约阅读时间需要 47 分钟。

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);

 记得要及时关闭游标!

 

转载于:https://www.cnblogs.com/abclife/p/5033101.html

你可能感兴趣的文章
python与 Ajax跨域请求
查看>>
Java实体书写规范
查看>>
App右上角数字
查看>>
从.NET中委托写法的演变谈开去(上):委托与匿名方法
查看>>
六、PowerDesigner 正向工程 和 逆向工程 说明
查看>>
小算法
查看>>
201521123024 《java程序设计》 第12周学习总结
查看>>
贪吃蛇游戏改进
查看>>
新作《ASP.NET MVC 5框架揭秘》正式出版
查看>>
“前.NET Core时代”如何实现跨平台代码重用 ——源文件重用
查看>>
【POJ1845】Sumdiv(数论/约数和定理/等比数列二分求和)
查看>>
在WPF中使用Caliburn.Micro搭建MEF插件化开发框架
查看>>
IdentityServer4-用EF配置Client(一)
查看>>
UWP: 掌握编译型绑定 x:Bind
查看>>
asp.net core系列 35 EF保存数据(2) -- EF系列结束
查看>>
WPF程序加入3D模型
查看>>
WPF中实现多选ComboBox控件
查看>>
读构建之法第四章第十七章有感
查看>>
C#中的IEnumerable<T>知识点
查看>>
android访问链接时候报java.net.MalformedURLException: Protocol not found
查看>>