Mongodb query documents

In MongoDB, we can manually retrieve a document from a collection in the selected database using db.collection_name.find() 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.findOne() in the command prompt to see a single document in a collection.

For Example: db.HELLO.findOne()