14.Iterate a Cursor in the mongo Shell-官方文档摘录
2017-07-31 11:02
381 查看
1 迭代游标
2 迭代指数
可以简化成
3 关闭游标,不活跃的游标需要等到10分钟才自动关闭,可以通过指定时间来进行关闭
4
The
The following examples describe ways to manually iterate the cursor to access the documents or to use the iterator index.
You can call the cursor variable in the shell to iterate up to 20 times [1] and print the matching documents, as in the following example:
You can also use the cursor method
As an alternative print operation, consider the
You can use the cursor method
See JavaScript cursor methods and your driver documentation for more information on cursor methods.
The
Additionally, some drivers provide access to the documents by using an index on the cursor (i.e.
Consider the following example:
The
After setting the
See your driver documentation for information on setting the
New in version 3.4: Operations of type
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,
number of timed out cursors since the last server restart
number of open cursors with the option
number of “pinned” open cursors
total number of open cursors
Consider the following example which calls the
The result is the following document:
SEE ALSO
var myCursor = db.users.find( { type: 2 } ); while (myCursor.hasNext()) { print(tojson(myCursor.next())); }
var myCursor = db.users.find( { type: 2 } ); myCursor.forEach(printjson);
2 迭代指数
var myCursor = db.inventory.find( { type: 2 } ); var documentArray = myCursor.toArray(); var myDocument = documentArray[3];
可以简化成
var myCursor = db.users.find( { type: 2 } ); var myDocument = myCursor[1];
3 关闭游标,不活跃的游标需要等到10分钟才自动关闭,可以通过指定时间来进行关闭
var myCursor = db.users.find().noCursorTimeout();
4
find(),
aggregate(),
listIndexes, and
listCollections 最大是16MB
var myCursor = db.inventory.find(); var myFirstDocument = myCursor.hasNext() ? myCursor.next() : null; myCursor.objsLeftInBatch();
The
db.collection.find()method returns a cursor. To access the documents, you need to iterate the cursor. However, in the
mongoshell, if the returned cursor is not assigned to a variable using the
varkeyword, then the cursor is automatically iterated up to 20 times [1] 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 themongoshell, when you assign the cursor returned from the
find()method to a variable using the
varkeyword, the cursor does not automatically iterate.
You can call the cursor variable in the shell to iterate up to 20 times [1] and print the matching documents, as in the following example:
var myCursor = db.users.find( { type: 2 } ); myCursor
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.
[1] | (1, 2) You can use the DBQuery.shellBatchSizeto change the number of iteration from the default value 20. SeeWorking with the mongo Shell for more information. |
Iterator Index
In themongoshell, 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 themongoshell, you can use the
cursor.noCursorTimeout()method:
var myCursor = db.users.find().noCursorTimeout();
After setting the
noCursorTimeoutoption, 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
noCursorTimeoutoption.
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. The amount of data in the batch will not exceed themaximum BSON document size. To override the default size of the batch, seebatchSize()and
limit().
New in version 3.4: Operations of type
find(),
aggregate(),
listIndexes, and
listCollectionsreturn a maximum of 16 megabytes per batch.
batchSize()can enforce a smaller limit, but not a larger one.
find()and
aggregate()operations have an initial batch size of 101 documents by default. Subsequent
getMoreoperations issued against the resulting cursor have no default batch size, so they are limited only by the 16 megabyte message size.
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 operationto 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
Thedb.serverStatus()method returns a document that includes a
metricsfield. The
metricsfield contains a
metrics.cursorfield with the following information:
number of timed out cursors since the last server restart
number of open cursors with the option
DBQuery.Option.noTimeoutset 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
cursorfield from the
metricsfield:
db.serverStatus().metrics.cursor
The result is the following document:
{ "timedOut" : <number> "open" : { "noTimeout" : <number>, "pinned" : <number>, "total" : <number> } }
SEE ALSO
db.serverStatus()
相关文章推荐
- 4.Data Types in the mongo Shell-官方文档摘录
- 3.Write Scripts for the mongo Shell-官方文档摘录
- 1.Configure the mongo Shell-官方文档摘录
- MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell
- 2.Access the mongo Shell Help-官方文档摘录
- shell iterate the directory in recursive way
- How to Pin a Cursor in the Shared Pool using DBMS_SHARED_POOL.KEEP (文档 ID 726780.1)
- WebSocket官方文档翻译——HTML5 Web Sockets:A Quantum Leap in Scalability for the Web
- 在响应链中使用视图控制器Using View Controllers in the Responder Chain[苹果官方文档的翻译]
- MongoDB - The mongo Shell, Data Types in the mongo Shell
- Sencha Touch 2 官方文档翻译之 Using Views in your Applications(使用视图)
- Android官方文档翻译 十 2.3Styling the Action Bar
- Rhyme/Spring5.0.2.RELEASE官方英文文档学习笔记(1) Introduction to the Spring IoC container and beans
- Influxdb-官方文档摘录
- Influxdb-官方文档摘录
- Query in Java as in Mongo shell 【Jongo】
- Unity官方手册翻译之旅---Switching between Unity versions in the documentation
- 简读The Swift Programming Language官方文档--集合类型
- 设置TextBox中光标的位置到最后(set the cursor position in a TextBox or InputText)
- "ORA-01102 cannot mount database in EXCLUSIVE mode",官方文档: