MongoDB uses cursors to process the set of result documents found by the Find query;
Examples of 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": "Schom E "}," age ":--} {" _id ": ObjectId (" 54ace753eab1d0ba4abb48ab ")," name ": {" FirstName ":" Joe "," LastName ":" Schome "}," Age ": >
or skip the top 3 records using skip:
> Cursor.skip (3) {"_id": ObjectId ("54ace781eab1d0ba4abb48ad"), "name": {"FirstName": "Joe", "LastName": "Schome "," comment ": 6}," Age ": $ {" _id ": ObjectId (" 54ace785eab1d0ba4abb48ae ")," name ": {" FirstName ":" Joe "," Lastn Ame ":" Schome "," comment ": 5}," age ": + {" _id ": ObjectId (" 54ace789eab1d0ba4abb48af ")," name ": {" FirstName ": "Joe", "LastName": "Schome", "comment": 7}, "Age": >
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 }
The combination of Sort/limit/skip, of course, can be adjusted in 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 ":}>
This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1600283
"MongoDB Learning Note 19" MongoDB Cursors and query options