Saturday 21 February 2015

mongoDB : Limit number of documents in the result


“limit” method is used to limit the number of documents in the result.

Syntax:
db.collection_name.find().limit(no_of_documents)

db.employee.find().limit(2)
Above statement limit the number of documents to 2.

> db.employee.find()
{ "_id" : ObjectId("54b348e01deb1f5f980626a2"), "id" : 1, "firstName" : "Joel", "lastName" : "chelli" }
{ "_id" : ObjectId("54b348e01deb1f5f980626a3"), "id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru" }
{ "_id" : ObjectId("54b348e01deb1f5f980626a4"), "id" : 3, "firstName" : "Gopi", "lastName" : "Battu" }
{ "_id" : ObjectId("54b348e01deb1f5f980626a5"), "id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy" }
>
> db.employee.find().limit(2)
{ "_id" : ObjectId("54b348e01deb1f5f980626a2"), "id" : 1, "firstName" : "Joel", "lastName" : "chelli" }
{ "_id" : ObjectId("54b348e01deb1f5f980626a3"), "id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru" }
>

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment