Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[MongoDB Learning Notes 19] cursors and query options for MongoDB

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

MongoDB uses cursors to process the set of result documents queried by find

Examples of the use of cursors:

> var cursor=db.post.find ({"name.firstname": "joe"}) > cursor {"_ id": ObjectId ("54ace1394ba07ed75df68f90"), "name": {"firstname": "joe", "lastname": "schome"}, "age": 28} {"_ id": ObjectId ("54ace753eab1d0ba4abb48ab"), "name": {"firstname": "joe", "lastname": "schome"} "age": 30} {"_ id": ObjectId ("54ace777eab1d0ba4abb48ac"), "name": {"firstname": "joe", "lastname": "schome", "comment": 10}, "age": 30} {"_ id": ObjectId ("54ace781eab1d0ba4abb48ad"), "name": {"firstname": "joe", "lastname": "schome", "comment": 6} "age": 30} {"_ id": ObjectId ("54ace785eab1d0ba4abb48ae"), "name": {"firstname": "joe", "lastname": "schome", "comment": 5}, "age": 30} {"_ id": ObjectId ("54ace789eab1d0ba4abb48af"), "name": {"firstname": "joe", "lastname": "schome", "comment": 7} "age": 30} >

Use limit to return the first two records:

> cursor.limit (2) {"_ id": ObjectId ("54ace1394ba07ed75df68f90"), "name": {"firstname": "joe", "lastname": "schome"}, "age": 28} {"_ id": ObjectId ("54ace753eab1d0ba4abb48ab"), "name": {"firstname": "joe", "lastname": "schome"}, "age": 30} >

Or use skip to skip the first three records:

> cursor.skip (3) {"_ id": ObjectId ("54ace781eab1d0ba4abb48ad"), "name": {"firstname": "joe", "lastname": "schome", "comment": 6}, "age": 30} {"_ id": ObjectId ("54ace785eab1d0ba4abb48ae"), "name": {"firstname": "joe", "lastname": "schome", "comment": 5} "age": 30} {"_ id": ObjectId ("54ace789eab1d0ba4abb48af"), "name": {"firstname": "joe", "lastname": "schome", "comment": 7}, "age": 30} >

Or use sort to specify the key-value sort:

> cursor.sort ({age: 1}) {"_ id": ObjectId ("54ace1394ba07ed75df68f90"), "name": {"firstname": "joe", "lastname": "schome"}, "age": 28} {"_ id": ObjectId ("54ace753eab1d0ba4abb48ab"), "name": {"firstname": "joe", "lastname": "schome"} "age": 30} {"_ id": ObjectId ("54ace777eab1d0ba4abb48ac"), "name": {"firstname": "joe", "lastname": "schome", "comment": 10}, "age": 30} {"_ id": ObjectId ("54ace781eab1d0ba4abb48ad"), "name": {"firstname": "joe", "lastname": "schome", "comment": 6} "age": 30} {"_ id": ObjectId ("54ace785eab1d0ba4abb48ae"), "name": {"firstname": "joe", "lastname": "schome", "comment": 5}, "age": 30} {"_ id": ObjectId ("54ace789eab1d0ba4abb48af"), "name": {"firstname": "joe", "lastname": "schome", "comment": 7} "age": 30}

If you use sort/limit/skip together, of course, you can adjust the order to get different results:

> cursor.sort ({"name.comment": 1}) .skip (2) .limit (1) {"_ id": ObjectId ("54ace785eab1d0ba4abb48ae"), "name": {"firstname": "joe", "lastname": "schome", "comment": 5}, "age": 30} >

Cursor life cycle:

The cursor consumes memory and other resources, and after the cursor is used, the resources are released for use by the database; the cursor terminates in the following cases

(1) after the cursor completes the iteration of the matching result, it will know itself.

(2) the cursor of the client is no longer in scope, and the driver sends a special message to the server to destroy it.

(3) if the cursor is not used within ten minutes, the cursor will be destroyed automatically.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report