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);
记得要及时关闭游标!