To skip values in MongoDB, use skip() along with limit(). For 5 values, use limit(5). Let us create a collection with documents −
> db.demo633.insertOne({"Value":10}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c0be76c954c74be91e6c1") } > db.demo633.insertOne({"Value":20}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c0bea6c954c74be91e6c2") } > db.demo633.insertOne({"Value":30}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c0bec6c954c74be91e6c3") } > db.demo633.insertOne({"Value":40}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c0bef6c954c74be91e6c4") } > db.demo633.insertOne({"Value":50}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c0bf16c954c74be91e6c5") } > db.demo633.insertOne({"Value":60}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c0bf36c954c74be91e6c6") } > db.demo633.insertOne({"Value":70}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c0bf86c954c74be91e6c7") } > db.demo633.insertOne({"Value":80}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c0bfa6c954c74be91e6c8") }
Display all documents from a collection with the help of find() method −
> db.demo633.find();
This will produce the following output −
{ "_id" : ObjectId("5e9c0be76c954c74be91e6c1"), "Value" : 10 } { "_id" : ObjectId("5e9c0bea6c954c74be91e6c2"), "Value" : 20 } { "_id" : ObjectId("5e9c0bec6c954c74be91e6c3"), "Value" : 30 } { "_id" : ObjectId("5e9c0bef6c954c74be91e6c4"), "Value" : 40 } { "_id" : ObjectId("5e9c0bf16c954c74be91e6c5"), "Value" : 50 } { "_id" : ObjectId("5e9c0bf36c954c74be91e6c6"), "Value" : 60 } { "_id" : ObjectId("5e9c0bf86c954c74be91e6c7"), "Value" : 70 } { "_id" : ObjectId("5e9c0bfa6c954c74be91e6c8"), "Value" : 80 }
Following is the query to split query and skip 5 values −
> var dividePage=2; > db.demo633.find().skip((dividePage-1)*5).limit(5);
This will produce the following output −
{ "_id" : ObjectId("5e9c0bf36c954c74be91e6c6"), "Value" : 60 } { "_id" : ObjectId("5e9c0bf86c954c74be91e6c7"), "Value" : 70 } { "_id" : ObjectId("5e9c0bfa6c954c74be91e6c8"), "Value" : 80 }