首页 / MONGODB / MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell
MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4363字,纯文字阅读大概需要7分钟。
内容图文
![MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell](/upload/InfoBanner/zyjiaocheng/473/74a260bf29d8433e80442de1712b4866.jpg)
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
标签:user format limit query handle for rem seq stat
本文系统来源:http://www.cnblogs.com/huey/p/6120497.html
内容总结
以上是互联网集市为您收集整理的MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell全部内容,希望文章能够帮你解决MongoDB - MongoDB CRUD Operations, Query Documents, Iterate a Cursor in the mongo Shell所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。