MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell
The db.collection.find() method returns a cursor. To access the documents, you need to iterate the cursor. However, in the mongo shell, if the returned cursor is not assigned to a variable using the varkeyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents in the results.
The following examples describe ways to manually iterate the cursor to access the documents or to use the iterator index.
Manually Iterate the Cursor
In the mongo shell, when you assign the cursor returned from the find() method to a variable using the var keyword, the cursor does not automatically iterate.
You can call the cursor variable in the shell to iterate up to 20 times and print the matching documents, as in the following example:
> var myCursor = db.users.find( { type: 2 } );
> myCursor
{ "_id" : 3, "name" : "ahn", "age" : 22, "type" : 2, "status" : "A", "favorites" : { "artist" : "Cassatt", "food" : "cake" }, "finished" : [ 6 ], "badges" : [ "blue", "red" ], "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }
{ "_id" : 4, "name" : "xi", "age" : 34, "type" : 2, "status" : "D", "favorites" : { "artist" : "Chagall", "food" : "chocolate" }, "finished" : [ 5, 11 ], "badges" : [ "red", "black" ], "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ] }
{ "_id" : 5, "name" : "xyz", "age" : 23, "type" : 2, "status" : "D", "favorites" : { "artist" : "Noguchi", "food" : "nougat" }, "finished" : [ 14, 6 ], "badges" : [ "orange" ], "points" : [ { "points" : 71, "bonus" : 20 } ] }
You can also use the cursor method next() to access the documents, as in the following example:
var myCursor = db.users.find( { type: 2 } ); while (myCursor.hasNext()) {
print(tojson(myCursor.next()));
}
As an alternative print operation, consider the printjson() helper method to replace print(tojson()):
var myCursor = db.users.find( { type: 2 } ); while (myCursor.hasNext()) {
printjson(myCursor.next());
}
You can use the cursor method forEach() to iterate the cursor and access the documents, as in the following example:
var myCursor = db.users.find( { type: 2 } ); myCursor.forEach(printjson);
See JavaScript cursor methods and your driver documentation for more information on cursor methods.
Iterator Index
In the mongo shell, you can use the toArray() method to iterate the cursor and return the documents in an array, as in the following:
var myCursor = db.inventory.find( { type: 2 } );
var documentArray = myCursor.toArray();
var myDocument = documentArray[3];
The toArray() method loads into RAM all documents returned by the cursor; the toArray() method exhausts the cursor.
Additionally, some drivers provide access to the documents by using an index on the cursor (i.e.cursor[index]). This is a shortcut for first calling the toArray() method and then using an index on the resulting array.
Consider the following example:
var myCursor = db.users.find( { type: 2 } );
var myDocument = myCursor[1];
The myCursor[1] is equivalent to the following example:
myCursor.toArray()[1];
Cursor Behaviors
Closure of Inactive Cursors
By default, the server will automatically close the cursor after 10 minutes of inactivity, or if client has exhausted the cursor. To override this behavior in the mongo shell, you can use the cursor.noCursorTimeout() method:
var myCursor = db.users.find().noCursorTimeout();
After setting the noCursorTimeout option, you must either close the cursor manually with cursor.close() or by exhausting the cursor’s results.
See your driver documentation for information on setting the noCursorTimeout option.
Cursor Isolation
As a cursor returns documents, other operations may interleave with the query. For the MMAPv1 storage engine, intervening write operations on a document may result in a cursor that returns a document more than once if that document has changed. To handle this situation, see the information on snapshot mode.
Cursor Batches
The MongoDB server returns the query results in batches. Batch size will not exceed the maximum BSON document size. For most queries, the first batch returns 101 documents or just enough documents to exceed 1 megabyte. Subsequent batch size is 4 megabytes. To override the default size of the batch, see batchSize() and limit().
For queries that include a sort operation without an index, the server must load all the documents in memory to perform the sort before returning any results.
As you iterate through the cursor and reach the end of the returned batch, if there are more results, cursor.next() will perform a getmore operation to retrieve the next batch. To see how many documents remain in the batch as you iterate the cursor, you can use the objsLeftInBatch() method, as in the following example:
var myCursor = db.inventory.find(); var myFirstDocument = myCursor.hasNext() ? myCursor.next() : null; myCursor.objsLeftInBatch();
Cursor Information
The db.serverStatus() method returns a document that includes a metrics field. The metrics field contains a metrics.cursor field with the following information:
- number of timed out cursors since the last server restart
- number of open cursors with the option DBQuery.Option.noTimeout set to prevent timeout after a period of inactivity
- number of “pinned” open cursors
- total number of open cursors
Consider the following example which calls the db.serverStatus() method and accesses the metricsfield from the results and then the cursor field from the metrics field:
db.serverStatus().metrics.cursor
The result is the following document:
{
"timedOut" : <number>
"open" : {
"noTimeout" : <number>,
"pinned" : <number>,
"total" : <number>
}
}
SEE ALSO: db.serverStatus()
MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell的更多相关文章
- MongoDB - MongoDB CRUD Operations, Query Documents, Project Fields to Return from Query
By default, queries in MongoDB return all fields in matching documents. To limit the amount of data ...
- MongoDB - MongoDB CRUD Operations, Query Documents
Query Method MongoDB provides the db.collection.find() method to read documents from a collection. T ...
- MongoDB - MongoDB CRUD Operations, Query Documents, Query for Null or Missing Fields
Different query operators in MongoDB treat null values differently. The examples on this page use th ...
- 14.Iterate a Cursor in the mongo Shell-官方文档摘录
1 迭代游标 } ); while (myCursor.hasNext()) { print(tojson(myCursor.next())); } } ); myCursor.forEach(pri ...
- MongoDB - MongoDB CRUD Operations, Delete Documents
Delete Methods MongoDB provides the following methods to delete documents of a collection: Method De ...
- MongoDB - MongoDB CRUD Operations, Insert Documents
MongoDB provides the following methods for inserting documents into a collection: db.collection.inse ...
- MongoDB - MongoDB CRUD Operations, Update Documents
Update Methods MongoDB provides the following methods for updating documents in a collection: Method ...
- MongoDB - The mongo Shell, mongo Shell Quick Reference
mongo Shell Command History You can retrieve previous commands issued in the mongo shell with the up ...
- MongoDB - Introduction of the mongo Shell
Introduction The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mong ...
随机推荐
- C#高级编程 (第六版) 学习 第五章:数组
第五章 数组 1,简单数组 声明:int[] myArray; 初始化:myArray = new int[4]; 为数组分配内存. 还可以用如下的方法: int[] myArray = new in ...
- js登录界面代码自用
var btn = document.getElementById("a4"); var usne = document.getElementById("username ...
- Ansible基础配置与常用模块使用
环境介绍: Ansible服务端IP:192.168.2.215 Ansible客户端IP:192.168.2.216.192.168.2.218.192.168.2.113 一.创建Ansibl ...
- 爬虫学习之-git拉取远程错误
本文讲的是把git在最新2.9.2,合并pull两个不同的项目,出现的问题如何去解决 如果合并了两个不同的开始提交的仓库,在新的 git 会发现这两个仓库可能不是同一个,为了防止开发者上传错误,于是就 ...
- get_list_by_where
/** * 查询数据 * @param $param * @param bool $get_rows 或者总数 * @param bool $get_one 或者一条记录 * @param bool ...
- POI操作Excel异常Cannot get a text value from a numeric cell
控制台抛出异常:java.lang.IllegalStateException: Cannot get a text value from a numeric cell 在java中用POI解析exc ...
- 微信小程序组件 客服
<!-- 话务 --> <view class='detail-tel flexca'> <image class='image-full' src='../../img ...
- 选项卡控件(TabControl)的操作
移除选项卡和删除不同:前者可以从控件中移除不需要的选项,后者可以删掉整个控件.
- luogu 1712 区间(线段树+尺取法)
题意:给出n个区间,求选择一些区间,使得一个点被覆盖的次数超过m次,最小的花费.花费指的是选择的区间中最大长度减去最小长度. 坐标值这么大,n比较小,显然需要离散化,需要一个技巧,把区间转化为半开半闭 ...
- bzoj1272 Gate Of Babylon(计数方法+Lucas定理+乘法逆元)
Description Input Output Sample Input 2 1 10 13 3 Sample Output 12 Source 看到t很小,想到用容斥原理,推一下发现n种数中选m个 ...