MongoDB

Remove Documents using Mongo Shell

The db.collection.remove() method removes documents from a collection.

  • You can remove all documents from a collection.
  • Remove all documents that match a condition.
  • Or limit the operation to remove just a single document.
 db.users.remove({})  //Remove All Documents  
db.users.remove({Name : "Himen Patel" }) //Remove Documents that Match a Condition
db.users.remove({Name : "Himen Patel" },1 ) //Remove a Single Document that Matches a Condition
MongoDB

Upsert Option using Mongo Shell

By default, if no document matches the update query, the update() method does nothing.

However, by specifying upsert: true, the update() method either updates matching document or documents, or inserts a new document using the update specification if no matching document exists.

The following operation either updates a matching document by replacing it with a new document or adds a new document if no matching document exists.

 > db.users.update(  
... { Name : "Hymen Patel" },
... {
... "Name": "Himen Paatel",
... "Email": "email2himen@dotnet-funda.com",
... "Address1": "1257 Commons Drive",
... "City": "Cooperstown",
... "Zip Code": "13326",
... "Country": "USA"
... },
... { upsert: true }
... )

The update operation returns a WriteResult object which contains the status of the operation, including whether the db.collection.update() method modified an existing document or added a new document.

 WriteResult({  
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("561324b1b6bb748311f00aac")
})

Specify upsert: true for the update specific fields operation

 db.users.update(   
{ Name : "Himen Patel" },
{$set :
{ "Email" : "admin@dotnet-fundamentals.com"} },
{upsert: true}
)
MongoDB

Replace a document using Mongo Shell

The following operation replaces the document with item equal to “Himen Patel”. The newly replaced document will only contain the the _id field and the fields in the replacement document.

 > db.users.update(  
... { Name : "Himen Patel" },
... {
... "Name": "Himen Paatel",
... "Email": "email2himen@dotnet-funda.com",
... "Address1": "1257 Commons Drive",
... "City": "Cooperstown",
... "Zip Code": "13326",
... "Country": "USA"
... }
... )

A successful update of the document returns the following object.

 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })  
MongoDB

Modify Documents using Mongo Shell

MongoDB provides the update() method to update the documents of a collection. The method accepts as its parameters:

  • an update conditions document to match the documents to update,
  • an update operations document to specify the modification to perform, and
  • an options document.

To specify the update condition, use the same structure and syntax as the query conditions.

 > db.users.update( { Name : "Himen Patel" }, {$set : { "Email" : "admin@dotnet-fundamentals.com"} })   

A successful update of the document returns the following object:

 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })  


Update multiple documents.
By default, the update() method updates a single document. To update multiple documents, use the multi option in the update() method.

 db.users.update( { Name : "Himen Patel" }, {$set : { "Email" : "admin@dotnet-fundamentals.com"} }, {multi: true})  

A successful update of the document returns the following object.

 WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 })