Here I am providing the path for the data folder as well as where to create the log files. So lets start mongodb by going to C:\mongodb\bin directory and issueing the follwoing command:-
C:\mongodb\bin>mongod.exe --config C:\mongodb\mongo.config
This will start mongodb.We can also see entries like below in the log file:-
2014-12-24T14:30:09.702+0000 [initandlisten] MongoDB starting : pid=11892 port=27017 dbpath=C:\mongodb\data 64-bit host=MC0WAMCC
2014-12-24T14:30:09.702+0000 [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2014-12-24T14:30:09.702+0000 [initandlisten] db version v2.6.6
2014-12-24T14:30:09.702+0000 [initandlisten] git version: 608e8bc319627693b04cc7da29ecc300a5f45a1f
2014-12-24T14:30:09.702+0000 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2014-12-24T14:30:09.702+0000 [initandlisten] allocator: system
2014-12-24T14:30:09.702+0000 [initandlisten] options: { config: "C:\mongodb\mongo.config", storage: { dbPath: "C:\mongodb\data" }, systemLog: { destination: "file", path: "C:\mongodb\log\mongo.log" } }
2014-12-24T14:30:09.721+0000 [initandlisten] journal dir=C:\mongodb\data\journal
2014-12-24T14:30:09.722+0000 [initandlisten] recover : no journal files present, no recovery needed
2014-12-24T14:30:09.872+0000 [initandlisten] waiting for connections on port 27017
2014-12-24T14:31:09.868+0000 [clientcursormon] mem (MB) res:54 virt:466
2014-12-24T14:31:09.868+0000 [clientcursormon] mapped (incl journal view):320
2014-12-24T14:31:09.868+0000 [clientcursormon] connections:0
Once mongodb is started we need to open another commandprompt to connect to mongo shell to issue commands.So open another window and go to C:\mongodb\bin> and execute the mongo.exe file:
C:\mongodb\bin>mongo.exe
We can see output like this:-
C:\mongodb\bin>mongo.exe
MongoDB shell version: 2.6.6
connecting to: test
By default it connects to the test database. But we perform CRUD opetation lets get some idea about some new terms that are introduced with mongodb and their relational database counterpart. As we are from RDBMS background its good to know these terms as they will be used frequently.
1.Databse - Database
2.Collection - Table
3.Document - Row
unlike RDBMS mongodb doesnt have the concept of schema, i.e, we dont have to define table structure to store and retrive data from it. We also dont have to create a database or table or collection. Mongodb does this for us on the fly. Mongodb uses JSON like dataformat called BSON (actually binary representation). Also mongo shell uses javascript to issue commands.
So lets create a database called tutorials for learning puropse. Issue command:- use tutorails from mongo shell and we can see the output like siwtched to tutorials without even we creating the tutorials database.Mongodb is lazy in the sense that at this point it will not be creat the databse until we insert some data into it.Databases and collections are created only when documents are first inserted.
C:\mongodb\bin>mongo.exe
MongoDB shell version: 2.6.6
connecting to: test
> use tutorials
switched to db tutorials
Now lets create a collection(table) called users and try to insert some document(rows) into it. To do this issue the below command
db.users.insert({username:"Sanjiv Kumar"})
The above command will create the collection users and will insert the document in it. We need not create the collection explicitly as mentioned earlier. We have saved our first document. Just cross check if it has been really saved or not by issuing the below command:-
db.user.find()
We can see output like this:-
{ "_id" : ObjectId("549ad320a99604bc0b1ee9b8"), "username" : "Sanjiv Kumar" }
Note that an _id field has been added to the document. We can think of the _id value as the document’s primary key. Every MongoDB document requires an _id, and if one isn’t present when the document is created, then a special MongoDB object ID will be generated and added to the document at that time.
Lets add one more user by issuing the below command
db.users.insert({username:"Rajeev Kumar"})
Now that we have more than one document in the collection lets try some complex queries. As seen earlier db.users.find() will return all the documents from the collection. But we can also pass simple query selector to the find() method. Lets issue the below command and see the output:-
db.users.find({username:"Rajeev Kumar"})
{ "_id" : ObjectId("549ad5afa99604bc0b1ee9b9"), "username" : "Rajeev Kumar" }
Now lets try to update any existing document by issuing the below command:-
db.users.update({username:"Sanjiv Kumar"},{$set:{country:"India"}})
You can cross check by issuing the find command whether the country is added or not, as you can see from the below that it has been added:-
db.users.update({username:"Sanjiv Kumar"},{$set:{country:"India"}})
Now lets try to remove any document by issuing the below command:-
db.users.remove({username:"Sanjiv Kumar"})
To drop the collection use the below command:
db.users.drop()
Thats all for this blog. We saw how to install mongodb and performed some very basic CRUD operation to get going.
Thanks for reading.
No comments:
Post a Comment