MongoDB - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MongoDB Insert

MongoDB Insert

shape Description

Mongo is a database document, these document can be structured and can be saved anywhere inside the database. Mongo doesn't provide any restrictions for declaring a schema and checking the progressions of storage engine, saving documents and updating documents that are internally handled by the MongoDB. MongoDB Insert or Saving data contains the following types of documents such as:

Insert document

shape Description

MongoDB Insert or save functions are used for inserting the data inside the collection of MongoDB.

shape Syntax

The syntax for MongoDB Insert document is as follows:
>db.collection_name.insert(document)

shape Examples

STEP - 1 : For inserting a single documents. [c] MongoDB shell version: 3.2.7 connecting to: test > use testdb switched to db testdb > db.testdb.insert({"name": "James"}) WriteResult({ "nInserted" : 1 }) > db.testdb.find().pretty() { "_id" : ObjectId("577e15d4e24946f4cb1d93a1"), "name" : "James" } [/c] Here in the above example, a document by name james is successfully inserted into the MongoDB database. STEP - 2 : For inserting multiple documents. [c]MongoDB shell version: 3.2.7 connecting to: test > use testdb switched to db testdb > db.testdb.insert([{"name" : "Derek Banas"}, ... {"name" : "Hank Aaron"}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.testdb.find().pretty() { "_id" : ObjectId("577e17ff18b4e3a09e9c6246"), "name" : "Derek Banas" } { "_id" : ObjectId("577e17ff18b4e3a09e9c6247"), "name" : "Hank Aaron" } > [/c] Here in the above example, a multiple documents by names Derek Banas and Hank Aaron is successfully inserted into the MongoDB database.

Update document

shape Description

Save and update functions are used for updating the documents into collection inside the MongoDB. Save functions will change the document that is already present in the database and update function will update the esteems in the actual document inside the database.

shape Syntax

The syntax for update document is as follows:
>db.collection_name.update(selection_criteria,updated_data)

shape Examples

From the below example one can easily understand the concept of update document in MongoDB Database. [c]MongoDB shell version: 3.2.7 connecting to: test > use testdb switched to db testdb > db.testdb.insert([{"name" : "Derek Banas"}, ... {"name" : "Hank Aaron"}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.testdb.find().pretty() { "_id" : ObjectId("577e17ff18b4e3a09e9c6246"), "name" : "Derek Banas" } { "_id" : ObjectId("577e17ff18b4e3a09e9c6247"), "name" : "Hank Aaron" } > db.testdb.update({"name":"Derek Banas"}, ... {$set:{"name":"Louis Derek Banas"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.testdb.find().pretty() { "_id" : ObjectId("577e15d4e24946f4cb1d93a1"), "name" : "Louis Derek Banas" } { "_id" : ObjectId("577e17ff18b4e3a09e9c6247"), "name" : "Hank Aaron" } >[/c] Here in the above example, a document by name Derek Banas is successfully updated to Louis Derek Banas inside the MongoDB database.

Remove document

shape Description

To delete a document inside the collections then one should use remove function from MongoDB database.

shape Syntax

The syntax for update document is as follows:
>db.collection_name.remove(deletion_criteria)

shape Examples

From the below example one can easily understand the concept of remove document in MongoDB Database. [c]MongoDB shell version: 3.2.7 connecting to: test > use testdb switched to db testdb > db.testdb.insert([{"name" : "Derek Banas"}, ... {"name" : "Hank Aaron"}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.testdb.find().pretty() { "_id" : ObjectId("577e17ff18b4e3a09e9c6246"), "name" : "Derek Banas" } { "_id" : ObjectId("577e17ff18b4e3a09e9c6247"), "name" : "Hank Aaron" } > db.testdb.remove({"name":"Derek Banas"}) WriteResult({ "nRemoved" : 1 }) > db.testdb.find().pretty() { "_id" : ObjectId("577e17ff18b4e3a09e9c6247"), "name" : "Hank Aaron" } >[/c] Here in the above example, the document by name Derek Banas is successfully removed from the MongoDB database.

Summary

shape Key Points

  • MongoDB Saving Data - Is a database documents and these documents are stored in the form of schema.
  • MongoDB Insert Document - Is used for inserting the data inside the collections of MongoDB Database.
  • Update Document - Is used to update the information inside the collections of MongoDB Database.
  • Remove Document - Is used to delete the document inside the collections of MongoDB Database.