Sunday 24 April 2016

How to set up mongodb

How to set up mongodb
download the mongodb sodtware from the site, based on the OS.
Currently OS used is windows7 - 64bit


1)
Install mongo, by click on exe file

Open the command prompt
Now go to installation directory and start the mongo server by providing the db path and port number.

ex : mongod -dbpath C:\mongodb\data\db -port 13627

> go to the bin directory and check the logs file once the server is started

C:\mongodb\log

output :
[websvr] admin web console waiting for connections on port 28017


2)
Now start the mongo client or connect to mongodb

C:\mongodb\bin\mongo

output :

MongoDB shell version: 2.4.5
connecting to: test

> go to bin directory and check the log when we start the mongodb
C:\mongodb\log

output :
connection accepted from 127.0.0.1:50142 #2 (1 connection now open)


2.1 ) For help content

command : db.help()

2.2) to see the database

command  : show dbs

2.3) create collections/db in mongo

command :  use collectionName (if not exixting it will create)


2.4)  To list all collections:

command : show collections


3) Create Collection/table in mongo

ex :  db.createCollection("FirstCollection")
{ "ok" : 1 }

Check in logs file :

[FileAllocator] done allocating datafile \data\db\yourDB.1, size: 128MB,  took 0.249 secs


3.1) see the collection/table created in db
command : show collections
output :
FirstCollection
system.indexes

3.2) for inserting the data
command :
db.FirstCollection.insert({item:"shoes",id:"10"})
output:
{ "_id" : ObjectId("56fd238aecaeb8e797ff4a09"), "item" : "shoes", "id" : "10" }

3.3)
for select the data.
ex :
db.FirstCollection.find()
db.FirstCollection.find()



3.4)fetching specifics record

ex : db.FirstCollection.find({item:"shoes"})
output :
{ "_id" : ObjectId("56fd238aecaeb8e797ff4a09"), "item" : "shoes", "id" : "10" }


3.5) update the record

ex : > db.FirstCollection.update({item:"shoes"},{$set:{item:"11"}})

3.6) update the record if it not exist :

ex : db.FirstCollection.update({item:"shirt"},{id:"22"},{upsert:"true"})

3.7) delete a record

ex : db.FirstCollection.remove({item:"15"})

No comments:

Post a Comment