Mongo db basic installation

367 views
261 views

Published on

Basic Installation Of MongoDB and command line

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
367
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mongo db basic installation

  1. 1. MongoDB Basic InstallationMr. Parkhe Kishor B.Sr. Software EngineerHighmark Credit Information Services Pvt. Ltd., PuneEmail: parkhekishor@gmail.com Mobile No. +919595745346email: parkhekishor@gmail.com MobileNo. +919595745346
  2. 2. MongoDB Basic InstallationThis guide shows, how to install MongoDB on Windows and Centos.1. Windows installation2. Centos InstallationStep 1.1Download latest production release MongoDB from MongoDB download page .There are three build of for MongoDB.Windows server 2008 R2Windows 64 bitsWindows 32 bits (development up to 2GB)Note: Type following commend in the command prompt to find architecture ofyour windows platform.C:>wmic os get osarchitectureOSArchitecture32-bitemail: parkhekishor@gmail.com MobileNo. +919595745346
  3. 3. Step 1.2:Start command prompt, create data folder for MongoDB.(default locationc:datadb).This will start main MongoDB database process and waiting for connectionmessage in consoleemail: parkhekishor@gmail.com MobileNo. +919595745346
  4. 4. Step 1.3:Copy MongoDB download package intoC: MongoDB folder and unzipemail: parkhekishor@gmail.com MobileNo. +919595745346
  5. 5. Step 1.4:Connect to MongoDB using mongo.exeOpen another command prompt and issue the following commandsC:MongoDBmongodb-win32-i386-2.2.2bin>mongo.exemongo.exe will connect mongod.exe running on local host interface on27017 port .you can check http services at http://localhost:28017Step 1.5:test database and retrieve records. Type following commands in mongoshell>db.test.save ( , a : “test”- )>db.test.find(){ "_id" : ObjectId("50f8f47dc49f1d211f947d60"), "a" : "test" }>email: parkhekishor@gmail.com MobileNo. +919595745346
  6. 6. 2. CentOS InstallationStep 2.1 :Download latest production release MongoDB from MongoDB download page .• There are two build of for MongoDB.• Linux 64 bits• Linux 32 bits (development up to 2GB)Step 2.2 :Start terminal create data folder for MongoDB.(default location root#datadb).Step 2.3 :Copy MongoDB download package into rootMongoDB folder and unzip.then go to bin folder of MongoDB package and type commands./mongodthis will start main MongoDB database process.email: parkhekishor@gmail.com MobileNo. +919595745346
  7. 7. Step 1.4:Connect to MongoDB using mongo.exeOpen another command prompt and issue the following commands[root@HMCLUSTER2 /]# cd mongodb-linux-x86_64-2.0.6/bin[root@HMCLUSTER2 bin]# ./mongomongo.rpm will connect mongod.rpm running on local host interface on 27017port .you can check http services at http://localhost:28017Step 1.5:test database and retrieve records. Type following commands in mongo shell>db.test.save ( , a : “test”- )>db.test.find(){ "_id" : ObjectId("50f8f47dc49f1d211f947d60"), "a" : "test" }>email: parkhekishor@gmail.com MobileNo. +919595745346
  8. 8. Connecting to Database1. From command prompt start mongo. By default mongo look for databaseserver on local host on the port 27017.2. To connect server on different port or interface use the --port and --host3. Select data base use command db, it show name of current database.4. To list all database use command in mongo shellshow dbs.5. Switch to new database name mydb with following operationuse db name6. Show mongo help in mongo shell using following operationhelpemail: parkhekishor@gmail.com MobileNo. +919595745346
  9. 9. Create collection and insert Documents1. Switch to mydb with following operationuse mydb2. Create two document name doc1 and doc2 using JavaScript operationdoc1 = ,name : “mongo” -doc2 = {x: 1}3. Insert two document into collection say blog with following operationdb.blog.insert( doc1)db.blog.insert( doc2)when you insert document then MongoDB create both database mydb and blogcollection.4. Confirm that collection is exist with following commands,show collections5. Conform that document insert in collection using find () methodsdb.blog.find();6. Inserting multiple documents using for loop.email: parkhekishor@gmail.com MobileNo. +919595745346
  10. 10. Implementing Curser1. When you querying, MongoDB return curser object that contain result ofquery.2. Iterating over the curser with loopvar curser= db.blog.find();while( curser.hasnext() ){printjson( curser.next() );}3. Use array operation with cursercurser[4];email: parkhekishor@gmail.com MobileNo. +919595745346
  11. 11. Querying for Specific Documents1. define queryq=, Field : “condition” -By passing this query document as parameter to find() method.db.blog.find(q) or db.blog.find( , field : “condition” - )2. Query and Projectione.g. doc1= , id : 1, name : “mongo” -to find all docs where id has a value 1 but return only name.query = { id : 1 } and projection = { name : 1}db.blog.find(query, projection)MongoDB return the following result,,name : “mongo” -3. Limiting number of document in result setdb.blog.find().limit(3)email: parkhekishor@gmail.com MobileNo. +919595745346
  12. 12. Core MongoDB Operation1. Create2. Read3. Update4. DeleteRestriction on document_id field must be unique in collectionThe field name can not start with the $ characterThe field name can not start with the . Character1 Create1.1 insert and bulk insert1.2 create with save1.3 create with upsertemail: parkhekishor@gmail.com MobileNo. +919595745346
  13. 13. 2 Read operation2.1 find2.2 findOne2.3 projection3 Update3.1 update field ($set)3.2 update array ($push)3.3 update and upsert (true or 1)3.4 update multiple matching documents4 DELETE4.1 remove collection4.2 remove matching documents4.3 remove single matching documentsemail: parkhekishor@gmail.com MobileNo. +919595745346
  14. 14. Querying1. Specifying which query returnExample consider following document,, id : ObjetcId(….),name : Ram,age : 16,email : ram@gmail.com,city : pune,pin : 413710}email: parkhekishor@gmail.com MobileNo. +919595745346
  15. 15. 1. You have user collection, if you are interested only in name and emailfields,> db.user.find (, -, , “name” : 1, “email “: 1- )Out put{“_id” : (…),name : “ram”,email : ram@gmail.com}2. Conditional Query> db.user.find ( , “name” : “ram” - )Out put, id : ObjetcId(….),name : Ram, age : 16, email : ram@gmail.com,city : pune,pin : 413710}email: parkhekishor@gmail.com MobileNo. +919595745346
  16. 16. 3 . Query Conditionals“$lt” = “<““$gt” =“>”“$lte” =“<=““$gte”=“>=“ are all comparison operators.> db.user.find(, “age” : ,$gt : 10- -)It gives all which has age greater than 10.>db.user.find(, “age” : ,$gt : 10 ,$lt : 50- -)it return all documents, field age in 10 to 50 range.> db.user.find ({"name" : { "$ne" : "ram" } })Out put{ "_id" : ObjectId("50ff718bc5d2a43b68806826"), "name" :"ram1", "email" : "ram@gmail.com", "id" : "10" }email: parkhekishor@gmail.com MobileNo. +919595745346
  17. 17. 4. OR QueriesThere are two way to do OR query in MongoDB4.1 “$in”If you have more than one possible value to match for a single key, thenuse array criteria> db.user.find (,"name" : , "$in" : *“ram”, “ram1” + - -)opposite of “$in” is “$nin” which return all documents that don’t matchany of criteria.4.2 “$or”>db.user.find(,"$or" : *,“name" : “ram” -, ,“age" : 10-+-)this query return all documents that satisfy either name or age criteria.4.3 “$and”by using “AND” query you can narrow result set.>db.user.find(,"$and" : *,“name" : “ram” -, ,“age" : 10-+-)email: parkhekishor@gmail.com MobileNo. +919595745346
  18. 18. 4.4 “$not”> db.user.find({".id1" : {$not :{$gt: 11}} }){ "_id" : ObjectId("50ff6950c5d2a43b68806825"), { "name" :"ram", "email" : "ram@gmail.com", "id" : "10" }{ "_id" : ObjectId("50ff718bc5d2a43b68806826"), "name" :"ram1", "email" : "ram@gmail.com", "id" : "10" }{ "_id" : ObjectId("50ff7efdc5d2a43b68806827"), "name" :"ram1", "email" : "ram@gmail.com", "id1" : 10 }4.5 Regular expressionRegular expressions are flexible for string matching.> db.user.find({"name" : /ram/i -) ‘i ‘ use for case - insensitive.email: parkhekishor@gmail.com MobileNo. +919595745346
  19. 19. Querying ArrayExample suppose array is a list of colors,1 like this:> db.user.insert({colors : [ "red" , "white" ,"green", "black"]})the following query:> db.user.find({colors : "red"})Out put:{ "_id" : ObjectId("50ff83f9c5d2a43b68806829"), "colors" : ["red", "white", "green", "black" ] }2 if you need to match array more than on element the you use $all,> db.user.find(,colors : ,$all : *"red“ , “ black " +--){ "_id" : ObjectId("50ff83f9c5d2a43b68806829"), "colors" : ["red", "white", "green", "black" ] }email: parkhekishor@gmail.com MobileNo. +919595745346
  20. 20. 3 “$size”this operator allow you query to array of given size.>db.user.find ( { colors : {$size : 3} )this can’t be combine with another $ conditional operator (like $gt ).4 “$slice”The special "$slice" operator can be used to return a subset of elementsfor an array key.for example we had a blog post collection and we wanted first 10comments.>db.blog.posts.findOne(criteria, {"comments" : {"$slice" : 10}})Alternatively you find last 10 comments>db.blog.posts.findOne(criteria, {"comments" : {"$slice" : -10}})email: parkhekishor@gmail.com MobileNo. +919595745346
  21. 21. "$slice" can also return pages in the middle of the results by taking anoffset and the number of elements to return:> db.blog.posts.findOne(criteria, {"comments" : {"$slice" : [23, 10]}})this would keep first 23 elements and return 24th to 34th.email: parkhekishor@gmail.com MobileNo. +919595745346
  22. 22. Querying Embedded DocumentsThere are two ways querying embedded documents1 Query for whole document2 Query for individual documentExample : {name : {first : “ram” ,last : “roy”}}First query for whole document> db.user.find(, name : name : , first : “ram” ,last : “sharma” - -email: parkhekishor@gmail.com MobileNo. +919595745346
  23. 23. Query for just a specific key or keys of an embedded document.>db.user.find( , “name.first” : “ram”-)Example{"content" : “what is MongoDB? ","comments" : [{"author" : “ram","score" : 3,"comment" : "nice post"},{"author" : “sham","score" : 6,"comment" : "terrible post"}]}email: parkhekishor@gmail.com MobileNo. +919595745346
  24. 24. Complex queryTo find documents comments by ram, issue following commands,>db.user.find(, comments : , author : “ram” - -)alternatively>db.user.find(, “comments.author” : “ram” -)email: parkhekishor@gmail.com MobileNo. +919595745346

×