[MongoDB Learning Notes 19] cursors and query options for MongoDB
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.