"MongoDB Learning Note 19" MongoDB Cursors and query options

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.