CodesJava

Easy learning with example program codes

Mongodb query limit fields


MongoDB limit() : 

In MongoDB, we can manually limit the fields of document that we want to retrieve from a collection in the selected database using db.collection_name.find().limit(Number) method.

  • Type the command use DATABASE_NAME in the command prompt to create a new database or to select the already created database with the respective name.

For Example: use Hellodb

  • Type the command createCollection(name, options) in the command prompt to create a new collection manually.

For Example: db.createCollection(“HELLO”)

  • Type the command Collection_Name.insert(Document_name) to insert a document into the already created collection.

For Example: db.HELLO.insert ({“name”: “welcome”})

  • For automatic creation of the collection directly insert a document into a collection with the desired name using the Collection_Name.insert(Document_name) command.

For Example: db.HELLO.insert ({“name”: “welcome”})

  • Type the command collection_name.find() in the command prompt to see all the inserted document in a collection.

For Example: db.HELLO.find()

  • Type the command collection_name.find().limit(number) in the command prompt to see only a limited number of the documents, among all the inserted documents in a collection.

For Example: db.HELLO.find().limit(1)

  • Type the command collection_name.find().limit(number).skip(number) in the command prompt to see only a limited number of the specified documents, by skipping the previous documents in a collection.

For Example: db.HELLO.find().limit(1).skip(3)



Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki