w3resource

MongoDB: cursor.skip() method

cursor.skip

The cursor.skip() method is used to return a cursor that begins returning results only after passing or skipping a number of documents.

Syntax:

cursor.skip()

Sample document in the restaurants collection:


{
  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zipcode": "10462"
  },
  "borough": "Bronx",
  "cuisine": "Bakery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  ],
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"
}
........

Example: MongoDB: cursor.skip() method

The following example will return a cursor that begins returning results for only 5 documents after passing or skipping 12 of documents.

db.restaurants.find({"cuisine" : "American "}).limit(5).skip(12);

Output:

> db.restaurants.find({"cuisine" : "American "}).limit(5).skip(12);
{ "_id" : ObjectId("55c3043ab165fa6355ec5cad"), "address" : { "building" : "625", "coord" : [ -73.990494, 40.7569545 ], "street" : "8 Avenue", "zipcode" : "10018" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-06-09T00:00:00Z"), "grade" : "A", "score" : 12
 }, { "date" : ISODate("2014-01-10T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2012-12-07T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2011-12-13T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2011-09-09T00:00:00Z"), "grade" : "A", "score" : 13 }
], "name" : "Cafe Metro", "restaurant_id" : "40363298" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5cb2"), "address" : { "building" : "87-69", "coord" : [ -73.8309503, 40.7001121 ], "street" : "Lefferts Boulevard", "zipcode" : "11418" }, "borough" : "Queens", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-02-25T00:00:00Z"), "grade" : "A", "s
core" : 7 }, { "date" : ISODate("2013-08-14T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2012-08-07T00:00:00Z"), "grade" : "A", "score" : 7 }, { "date" : ISODate("2012-03-26T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2011-11-04T00:00:00Z"), "grade" : "A", "sco
re" : 0 }, { "date" : ISODate("2011-06-29T00:00:00Z"), "grade" : "A", "score" : 4 } ], "name" : "Snack Time Grill", "restaurant_id" : "40363590" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5cb5"), "address" : { "building" : "437", "coord" : [ -73.975393, 40.757365 ], "street" : "Madison Avenue", "zipcode" : "10022" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-06-03T00:00:00Z"), "grade" : "A", "score"
 : 9 }, { "date" : ISODate("2013-06-07T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2012-06-29T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-02-06T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-06-23T00:00:00Z"), "grade" : "A", "score" :
 13 } ], "name" : "Berkely", "restaurant_id" : "40363685" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5cb6"), "address" : { "building" : "1031", "coord" : [ -73.9075537, 40.6438684 ], "street" : "East   92 Street", "zipcode" : "11236" }, "borough" : "Brooklyn", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-02-05T00:00:00Z"), "grade" : "A", "sc
ore" : 0 }, { "date" : ISODate("2013-01-29T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2011-12-08T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Sonny'S Heros", "restaurant_id" : "40363744" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5cba"), "address" : { "building" : "364", "coord" : [ -73.96084119999999, 40.8014307 ], "street" : "West  110 Street", "zipcode" : "10025" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-09-04T00:00:00Z"), "grade" : "
B", "score" : 20 }, { "date" : ISODate("2014-02-26T00:00:00Z"), "grade" : "B", "score" : 23 }, { "date" : ISODate("2013-03-25T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-02-21T00:00:00Z"), "grade" : "A", "score" : 8 } ], "name" : "Spoon Bread Catering", "restaurant_id" : "40
364179" }

The following example will return a cursor that begins returning results for only 5 documents after passing or skipping 14 of documents.

db.restaurants.find({"cuisine" : "American "}).limit(5).skip(14);

Output:

> db.restaurants.find({"cuisine" : "American "}).limit(5).skip(14);
{ "_id" : ObjectId("55c3043ab165fa6355ec5cb5"), "address" : { "building" : "437", "coord" : [ -73.975393, 40.757365 ], "street" : "Madison Avenue", "zipcode" : "10022" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-06-03T00:00:00Z"), "grade" : "A", "score"
 : 9 }, { "date" : ISODate("2013-06-07T00:00:00Z"), "grade" : "A", "score" : 5 }, { "date" : ISODate("2012-06-29T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-02-06T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2011-06-23T00:00:00Z"), "grade" : "A", "score" :
 13 } ], "name" : "Berkely", "restaurant_id" : "40363685" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5cb6"), "address" : { "building" : "1031", "coord" : [ -73.9075537, 40.6438684 ], "street" : "East   92 Street", "zipcode" : "11236" }, "borough" : "Brooklyn", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-02-05T00:00:00Z"), "grade" : "A", "sc
ore" : 0 }, { "date" : ISODate("2013-01-29T00:00:00Z"), "grade" : "A", "score" : 3 }, { "date" : ISODate("2011-12-08T00:00:00Z"), "grade" : "A", "score" : 10 } ], "name" : "Sonny'S Heros", "restaurant_id" : "40363744" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5cba"), "address" : { "building" : "364", "coord" : [ -73.96084119999999, 40.8014307 ], "street" : "West  110 Street", "zipcode" : "10025" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-09-04T00:00:00Z"), "grade" : "
B", "score" : 20 }, { "date" : ISODate("2014-02-26T00:00:00Z"), "grade" : "B", "score" : 23 }, { "date" : ISODate("2013-03-25T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-02-21T00:00:00Z"), "grade" : "A", "score" : 8 } ], "name" : "Spoon Bread Catering", "restaurant_id" : "40
364179" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5cbc"), "address" : { "building" : "0", "coord" : [ -84.2040813, 9.9986585 ], "street" : "Guardia Airport Parking", "zipcode" : "11371" }, "borough" : "Queens", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-05-16T00:00:00Z"), "grade" : "A", "s
core" : 9 }, { "date" : ISODate("2013-05-10T00:00:00Z"), "grade" : "A", "score" : 10 }, { "date" : ISODate("2012-05-15T00:00:00Z"), "grade" : "A", "score" : 9 }, { "date" : ISODate("2011-11-02T00:00:00Z"), "grade" : "C", "score" : 32 } ], "name" : "Terminal Cafe/Yankee Clipper", "restaurant_id" : "4
0364262" }
{ "_id" : ObjectId("55c3043ab165fa6355ec5cc3"), "address" : { "building" : "1", "coord" : [ -73.97166039999999, 40.764832 ], "street" : "East   60 Street", "zipcode" : "10022" }, "borough" : "Manhattan", "cuisine" : "American ", "grades" : [ { "date" : ISODate("2014-10-16T00:00:00Z"), "grade" : "B",
 "score" : 24 }, { "date" : ISODate("2014-05-02T00:00:00Z"), "grade" : "A", "score" : 4 }, { "date" : ISODate("2013-04-02T00:00:00Z"), "grade" : "A", "score" : 13 }, { "date" : ISODate("2012-10-19T00:00:00Z"), "grade" : "A", "score" : 12 }, { "date" : ISODate("2012-04-27T00:00:00Z"), "grade" : "B",
"score" : 17 }, { "date" : ISODate("2011-11-29T00:00:00Z"), "grade" : "A", "score" : 11 } ], "name" : "Metropolitan Club", "restaurant_id" : "40364347" }

Retrieve the restaurants data from here

Previous: cursor.size() method
Next: cursor.sort() method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/shell-methods/cursor/cursor-skip.php