是的,您可以跳过skip()
在MongoDB中使用的某些文档。使用limit()
以显示你有多少个文件要显示跳过一些后。让我们创建一个包含文档的集合-
> db.demo682.insertOne({FirstName:"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462a804263e90dac94402") } > db.demo682.insertOne({FirstName:"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462ac04263e90dac94403") } > db.demo682.insertOne({FirstName:"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462af04263e90dac94404") } > db.demo682.insertOne({FirstName:"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462b304263e90dac94405") } > db.demo682.insertOne({FirstName:"Adam"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462ba04263e90dac94406") } > db.demo682.insertOne({FirstName:"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462be04263e90dac94407") } > db.demo682.insertOne({FirstName:"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea462c404263e90dac94408") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo682.find();
这将产生以下输出-
{ "_id" : ObjectId("5ea462a804263e90dac94402"), "FirstName" : "John" } { "_id" : ObjectId("5ea462ac04263e90dac94403"), "FirstName" : "Sam" } { "_id" : ObjectId("5ea462af04263e90dac94404"), "FirstName" : "Bob" } { "_id" : ObjectId("5ea462b304263e90dac94405"), "FirstName" : "David" } { "_id" : ObjectId("5ea462ba04263e90dac94406"), "FirstName" : "Adam" } { "_id" : ObjectId("5ea462be04263e90dac94407"), "FirstName" : "Chris" } { "_id" : ObjectId("5ea462c404263e90dac94408"), "FirstName" : "Carol" }
以下是跳过3个文档并显示2的查询-
> db.demo682.find().skip(3).limit(2);
这将产生以下输出-
{ "_id" : ObjectId("5ea462b304263e90dac94405"), "FirstName" : "David" } { "_id" : ObjectId("5ea462ba04263e90dac94406"), "FirstName" : "Adam" }