Installing and running MongoDB on windows environment
Below are the steps to download and run MongoDB on Windows
- Download stable release of MongoDB from http://www.mongodb.org/downloads
- Extract the tar/zip file in any folder.
- Create a directory named as “data\db” inside MongoDb folder.
- Start MongoDB using command “mongod
--dbpath ..\data”. Here ..\data is the path to data directory, which is
required to store data files. This is most important option we need to specify.
Please note that directory path you are providing should exists otherwise
process won’t start.
- Open a new command prompt and go to bin directory of MongoDB installation folder.
- Run command “mongo” which will let you to execute any MongoDB command through the prompt.
MongoDB commands
Below are some of MongoDB frequently used commands –
Displaying list of databases
show dbs
Selecting a particular database e.g. “local” database
Use local
Verifying db. Current db can be verified using below command
Db
Mongo Help. At any point of time, help command can be used
to see the Mongo commands
Insert Operation
Data can be inserted into MongoDB through collections. Here
collections are similar to tables in database which contains data.
a={name:"Surender"}
b={address:"Noida"}
db.address.insert(a)
db.address.insert(b)
If you want to add more documents through loop in “things”
collection, you can use
for (var i = 1; i <= 20; i++) db.things.insert( { x : 4 ,
j : i } )
Select Operation
Execute below command to find all records of address
collection. It will return first 20 records through cursor. If you want to iterate
the cursor and return more documents, use it command to return more records.
db.address.find()
Use db.address.find({name:”Surender”})
or db.address.find({address:”Noida”})
to return a particular record.
All MongoDB documents must
have a _id field with a unique value. In case _id is not passed
in insert script, mongodb automatically creates a unique ObjectId value for the field before inserting it into
the collection.
Sample document insert script –
db.profile.save({
name:
{"first": "Surender","last": "Goyat"},
dateofbirth:
new Date("XX-XX-XXXX"),
address:
{
"street":
"London Street",
"sector":
"61",
"city":
"Noida",
"state":
"UP",
"Pin":
"201301"
},
professionalsummary
: [
{
"company":
"Tavant",
"duration":
"1.5 years",
"designation":
"Project Manager"
},
{
"company":
"HCL",
"duration":
"7 years",
"designation":
"Technical Manager"
},
{
"company":
"MBT",
"duration":
"3 months",
"designation":
"System Engineer"
}
]
})
Above document can be searched through below command
db.profile.find({"name.first":"Surender","address.sector":"61"})
Update Operation
Existing document can be updated using update command as
show below. Here document already exist with _id:2
db.address.update(
{ _id: 2 },
{
$set: { 'phone': '9XXXXXXXXX'
}
}
)
There is a save() method which can work as
substitute of update. It will update the document if _id is passed, otherwise
insert the document.
References
Please refer below url for detailed documentation on
MongoDB.
Comments
Post a Comment