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 })  
MongoDB

Query Documents using Mongo Shell

The db.collection.find() method retrieves all documents from a collection.

 > db.users.find()  
{ "_id" : ObjectId("55236e5d82bf196454a94ece"), "name" : "yIIhato", "email" : "QJFDIhwi@VdQdJrvRa.com" }
{ "_id" : ObjectId("55236e5d82bf196454a94ecf"), "name" : "BejpPVb", "email" : "rXhenUnL@gIiEMTwZv.com" }
{ "_id" : ObjectId("55236e5d82bf196454a94ed0"), "name" : "jAXiuug", "email" : "hqChGtpS@MxnAHNIai.com" }

Specify Equality Condition
To specify equality condition, use the query document { : } to select all documents. The following example retrieves from the users collection all documents where the name field has the value “Himen Patel”.

 >db.users.find({Name : "Himen Patel"})  
{ "_id" : ObjectId("56130d23ba30465f56b57448"), "Name" : "Himen Patel", "Email" : "email2himen@xxx.com", "Address1" : "5619 Loyola", "City" : "Columbus", "Zip Code" : "43221", "Country" : "USA" }


Specify Conditions Using Query Operators
A query document can use the query operators to specify conditions in a MongoDB query. The following example selects all documents in the users collection where the value of the name field is either ‘Himen Patel’ or ‘Hiren Patel’.

 > db.users.find( { Name : { $in: [ 'Himen Patel', 'Hiren Patel' ] } } )  
{ "_id" : ObjectId("56130d23ba30465f56b57448"), "Name" : "Himen Patel", "Email" : "email2himen@xxx.com", "Address1" : "5619 Loyola", "City" : "Columbus", "Zip Code" : "43221", "Country" : "USA" }
{ "_id" : ObjectId("561318adba30465f56b57449"), "Name" : "Hiren Patel", "Email" : "email2hiren@xxx.com", "Address1" : "131 Beacon Ave", "City" : "Jersey City", "Zip Code" : "07306", "Country" : "USA" }

Specify AND Conditions

 > db.users.find( { Name: 'Himen Patel', Email: 'email2himen@xxx.com' } )  
{ "_id" : ObjectId("56130d23ba30465f56b57448"), "Name" : "Himen Patel", "Email" : "email2himen@xxx.com", "Address1" : "5619 Loyola", "City" : "Columbus", "Zip Code" : "43221", "Country" : "USA" }

Specify OR Conditions

 > db.users.find(  
... {
... $or: [ { Name : "Himen Patel" }, { Name : "Hiren Patel"} ]
... }
... )
{ "_id" : ObjectId("56130d23ba30465f56b57448"), "Name" : "Himen Patel", "Email" : "email2himen@xxx.com", "Address1" : "5619 Loyola", "City" : "Columbus", "Zip Code" : "43221", "Country" : "USA" }
{ "_id" : ObjectId("561318adba30465f56b57449"), "Name" : "Hiren Patel", "Email" : "email2hiren@xxx.com", "Address1" : "131 Beacon Ave", "City" : "Jersey City", "Zip Code" : "07306", "Country" : "USA" }
>
MongoDB

Insert Documents using Mongo Shell

Inserted a document
The db.collection.insert() method adds new documents into a collection. Below command will insert a document into a collection named users.

 var doc =   
{
"Name": "Himen Patel",
"Email": "email2himen@xxx.com",
"Address1": "5619 Loyola",
"City": "Columbus",
"Zip Code": "43221",
"Country": "USA"
}
db.users.insert(doc);

The above command will create the collection if the collection does not exist in the document users and  will returns a WriteResult object with the status of the operation.

A successful insert of the document returns the following object:

 WriteResult({ "nInserted" : 1 })  

The nInserted field will specifies the number of documents inserted. If the operation encounters an error, the WriteResult object will contain the error information.

Retrieving the inserted document
If the insert operation is successful, you can verify the insertion by querying the collection.

 db.users.find()

or

db.users.find({Name: "Himen Patel"}) //By passing parameter with value

The document you inserted should return.

 { "_id" : ObjectId("56130d23ba30465f56b57448"), "Name" : "Himen Patel", "Email" : "email2himen@xxx.com", "Address1" : "5619 Loyola", "City" : "Columbus", "Zip Code" : "43221", "Country" : "USA" }  

MongoDB

Importing JSON Records into MongoDB with mongoimport

If MongoDB is installed in the C:\MongoDB folder, and the file to be imported is located at C:\Data.json, the format of the mongoimport command is:

 C:\>mongodb\bin\mongoimport –host localhost:27017 –db myblogdb –collection posts > C:\Data.json  

In this example, “myblogdb” is the name of the database and “posts” is the name of the collection into which the data will be inserted.

Note that “localhost:27017” is the default server and port for MongoDB

The database and collection will be created if they do not already exist.