Để bỏ qua một số tài liệu cụ thể, hãy sử dụng bỏ qua () cùng với giới hạn. Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> 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ị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -
> db.demo246.find();
Điều này sẽ tạo ra kết quả sau -
{ "_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" } Sau đây là truy vấn trong MongoDB để bỏ qua n tài liệu đầu tiên -
> db.demo246.find().skip(2).limit(1);
Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e46b0ea1627c0c63e7dba67"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }