Để bỏ qua số lượng tài liệu cụ thể, sử dụng skip() kết hợp limit().
Tạo collection:
> db.demo246.insertOne({"StudentFirstName":"Chris","StudentLastName":"Brown"});
{ "acknowledged" : true, "insertedId" : ObjectId("5e46b0d71627c0c63e7dba65") }
> db.demo246.insertOne({"StudentFirstName":"John","StudentLastName":"Doe"});
{ "acknowledged" : true, "insertedId" : ObjectId("5e46b0e21627c0c63e7dba66") }
> db.demo246.insertOne({"StudentFirstName":"John","StudentLastName":"Smith"});
{ "acknowledged" : true, "insertedId" : ObjectId("5e46b0ea1627c0c63e7dba67") }
> db.demo246.insertOne({"StudentFirstName":"Carol","StudentLastName":"Taylor"});
{ "acknowledged" : true, "insertedId" : ObjectId("5e46b0f91627c0c63e7dba68") }
Hiển thị 4 bản ghi:
> db.demo246.find();
{ "_id" : ObjectId("5e46b0d71627c0c63e7dba65"), "StudentFirstName" : "Chris", "StudentLastName" : "Brown" }
{ "_id" : ObjectId("5e46b0e21627c0c63e7dba66"), "StudentFirstName" : "John", "StudentLastName" : "Doe" }
{ "_id" : ObjectId("5e46b0ea1627c0c63e7dba67"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }
{ "_id" : ObjectId("5e46b0f91627c0c63e7dba68"), "StudentFirstName" : "Carol", "StudentLastName" : "Taylor" }
Bỏ qua 2 bản ghi đầu, lấy 1 bản ghi tiếp theo:
> db.demo246.find().skip(2).limit(1);
Kết quả trả về bản ghi thứ 3 (John Smith):
{ "_id" : ObjectId("5e46b0ea1627c0c63e7dba67"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }