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.

MongoDB

Set Up MongoDB on Windows

Download a ZIP file containing the appropriate version of MongoDB from MongoDB.org.

Install MongoDB for Windows
In Windows Explorer, locate the downloaded MongoDB .msi file and double click .msi  file. By  default it will installed MongoDB to C:\mongodb.

Setup MongoDB environment
MongoDB requires a data directory to store all data. MongoDB’s default data directory path is C:\MongoDB\data\db. Create folder using Command Prompt.
Command :- md \data\db

Start MongoDB
To start MongoDB, run mongod.exe. For example, from the Command Prompt.
Command :- C:\mongodb\bin\mongod.exe

Connect to MongoDB
You should see output something like the following:

 C:\mongodb\bin\mongod –dbpath c:\mongodb\data\db    
Thu Oct 01 08:27:43.083 [initandlisten] MongoDB starting : pid=8608 port=27017 dbpath=c:\mongodb\data\db 64-bit host=s2ua3031x9m
Thu Oct 01 08:27:43.084 [initandlisten] db version v2.4.8
Thu Oct 01 08:27:43.084 [initandlisten] git version: a350fc38922fbda2cec8d5dd842237b904eafc14
Thu Oct 01 08:27:43.085 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack=’Service Pack 1′) BOOST_LIB_VERSION=1_49
Thu Oct 01 08:27:43.085 [initandlisten] allocator: system
Thu Oct 01 08:27:43.085 [initandlisten] options: { dbpath: "c:\mongodb\data\db"}
Thu Oct 01 08:27:43.122 [initandlisten] journal dir=c:\mongodb\data\db\journal
Thu Oct 01 08:27:43.123 [initandlisten] recover : no journal files present, no recovery needed
Thu Oct 01 08:27:43.206 [FileAllocator] allocating new datafile c:\mongodb\data\db\local.ns, filling with zeroes…
Thu Oct 01 08:27:43.207 [FileAllocator] creating directory c:\mongodb\data\db\_tmp
Thu Oct 01 08:27:43.246 [FileAllocator] done allocating datafile c:\mongodb\data\db\local.ns, size: 16MB, took 0.037 secs
Thu Oct 01 08:27:43.247 [FileAllocator] allocating new datafile c:\mongodb\data\db\local.0, filling with zeroes…
Thu Oct 01 08:27:43.373 [FileAllocator] done allocating datafile c:\mongodb\data\db\local.0, size: 64MB, took 0.125 secs
Thu Oct 01 08:27:43.374 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 reslen:37 167ms
Thu Oct 01 08:27:43.375 [initandlisten] waiting for connections on port 27017
Thu Oct 01 08:27:43.375 [websvr] admin web console waiting for connections on port 28017

To verify that you can connect to the new MongoDB server, open a second command window and execute c:\mongodb\bin\mongo.