mongodb: find(), limit() and skip()

The post shows how to use limit() and skip() to partial shows data.

version

bash-3.2$ mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017

show all documents

> db.member.find()
{ "_id" : ObjectId("5978cda6558e820054f7be9c"), "name" : "chengyihe" }
{ "_id" : ObjectId("5978cdac558e820054f7be9d"), "name" : "woody" }
{ "_id" : ObjectId("5978cdaf558e820054f7be9e"), "name" : "max" }
{ "_id" : ObjectId("5978cdb2558e820054f7be9f"), "name" : "daniel" }

show the first two documents

> db.member.find().limit(2)
{ "_id" : ObjectId("5978cda6558e820054f7be9c"), "name" : "chengyihe" }
{ "_id" : ObjectId("5978cdac558e820054f7be9d"), "name" : "woody" }

after skipping the first document, show the first two documents

> db.member.find().limit(2).skip(1)
{ "_id" : ObjectId("5978cdac558e820054f7be9d"), "name" : "woody" }
{ "_id" : ObjectId("5978cdaf558e820054f7be9e"), "name" : "max" }

conclusion
The post shows some example to demonstrate the functionality of limit() and skip().

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: