Mongodb update documents

In MongoDB, we can insert a document using the upsert method into a collection, or we can update or modify the existing documents of a collection in the selected database using db.collection_name.update() method in MongoDB.

  • 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 the inserted document in a collection.

For Example: db.HELLO.find()

  • Type the command collection_name.update(Selection_Criteria, Updated_Data) in the command prompt to update or modify the document.

For Example: db.HELLO.update({‘name’:’Mongo’},{$set:{‘name’:’MongoDB’}})

  • Type the command collection_name.find() in the command prompt to check the updated document in a collection.

For Example: db.HELLO.find()

Please follow and like us:
Content Protection by DMCA.com