Showing posts with label mongoDB shell. Show all posts
Showing posts with label mongoDB shell. Show all posts

Tuesday, 1 October 2013

Replication in MongoDB

!!!!!!!!!!!!!!!!!!!!!!!
Replication
!!!!!!!!!!!!!!!!!!!!!!!

cd data
mkdir rs1 rs2 rs3
cd..
mongod --replSet m101 --logpath "1.log" --dbpath /data/rs1 --port 27017 --smallfiles
mongod --replSet m101 --logpath "2.log" --dbpath /data/rs2 --port 27018 --smallfiles
mongod --replSet m101 --logpath "3.log" --dbpath /data/rs3 --port 27019 --smallfiles

this shell script (started from mongo folder) should create replica set of 3 servers on Windows

config = { _id: "rs1" , members: [
{_id:0, host: "<here should be name of your computer> : 27017" , priority:0, slaveDelay:5 } ,
{_id:1, host: "<here should be name of your computer> : 27018"} ,
{_id:1, host: "<here should be name of your computer> : 27019"}]


rs.initiate(config)


this commands will initiate replica set, where server with _id:0 will be hidden member



Friday, 30 August 2013

mongo shell aggregate framework examples

!!!!!!!!!!!!!!!!
Group
!!!!!!!!!!!!!!!!

db.products.aggregate([ {$group: {_id:"$category" , num_products : {$sum:1} }} ])
will return document "result" that have ids of categories that there is in products collection with field num_products with value of number of items of specified category

db.products.aggregate([ {$group: {_id: {"manufacturer" : "$manufacturer" , "category":"$category" } , num_products : {$ sum:1} }} ])
group by compound key example

db.products.aggregate([ {$group: {_id:"$manufacturer" , sum_prices: {$sum:"$price"} }} ])
calculation of sum of totall price of devices of produced by every manufacturer

db.products.aggregate([ {$group: {_id:"$manufacturer" , avg_price: {$avg:"$price"} }} ])
calculation of the average price of devices of produced by every manufacturer

db.products.aggregate([ {$group: {_id:"$manufacturer" , categories: {$push:"$category"} }} ])
getting collection groupped by manufacturer with array (set) of categories where every category as many time in array as many such category of such manufacturer is exists in collection

db.products.aggregate([ {$group: {_id:"$manufacturer" , categories: {$push:"$category"} }} ])
getting collection groupped by manufacturer with array (set) of categories where every category as many time in array as many such category of such manufacturer is exists in collection

db.zips.aggregate([{$group:{_id:"$state","pop": {$max:"$pop"}}}])
The aggregation query that will return the population of the postal code in each state with the highest population.

db.group.aggregate([{
$group:{_id:   { class_id: "$class_id" , student_id :  "$student_id" } , average : {"$avg"  : "$score"}}} ,
{$group: {_id : "$_id.class_id" , average :{ "$avg" : "$average"}}
}])
The two step group example, on the first step we creating collection with unique pairs class_id and student_id evaluate average score for this pairs, on the second step we getting calculatting average score of certain student for certain class

!!!!!!!!!!!!!!!!
Project
!!!!!!!!!!!!!!!!
db.zips.aggregate([{$project:{_id:0, city:{$toLower:"$city"}, pop:1, state:1, zip:"$_id"}}])

projection example:
from such collection:
{
"city" : "ACMAR",
"loc" : [
-86.51557,
33.584132
],
"pop" : 6055,
"state" : "AL",
"_id" : "35004"
}

will return such:
{
"city" : "acmar",
"pop" : 6055,
"state" : "AL",
"zip" : "35004"
}

db.products.aggregate([ {$group: {_id:"$manufacturer" , sum_prices: {$sum:"$price"} }} ])
calculation of sum of totall price of devices of produced by every manufacturer

db.products.aggregate([ {$group: {_id:"$manufacturer" , avg_price: {$avg:"$price"} }} ])
calculation of the average price of devices of produced by every manufacturer


!!!!!!!!!!!!!!!!
Match
!!!!!!!!!!!!!!!!

db.zips.aggregate([{ $match : {state: NY} }])
kind of fillter, will return only documents where state is NY

!!!!!!!!!!!!!!!!!!!!!!!!!!!
Sort, Skip and Limit
!!!!!!!!!!!!!!!!!!!!!!!!!!!

db.zips.aggregate([{ $sort: {state : 1} }  , {$skip:10} , {$limit:5}]) 
query with just a sort stage to sort by state, ascending, skip the first 10 and get only next 5

!!!!!!!!!!!!!!!!
First and Last
!!!!!!!!!!!!!!!!

db.fun.aggregate([{$sort:{c:1}}, {$group:{_id:"$a", c:{$first:"$c"}}}])
will return collection of different "a" and for "c" key for the document from this collection will be the smallest "c" value for the corresponded "a"

!!!!!!!!!!!!!!!!
Unwind
!!!!!!!!!!!!!!!!

db.posts.aggregate([{ $unwind:"$comments"}]) 
from the posts collection will return new collection in which for every array element in comments arrat will be create new document

Wednesday, 28 August 2013

mongo shell indexes usage + miscellaneous comands

!!!!!!!!!!!!!!!!
Indexes
!!!!!!!!!!!!!!!!

db.students.ensureIndex({class:1, student_name:-1})
adding an index to a collection named students, having the index key be classstudent_name.
class: ascending sorted index
student_name: descending sorted index

db.system.indexes.find()
getting all added indexes in the database

db.students.getIndexes()
getting all added indexes in the collection students

db.students.dropIndex({'class':1, 'student_name':-1})
dropping index created by db.students.ensureIndex({class:1, student_name:-1}) command

db.students.ensureIndex({''student_name':-1} , {unique:true})
create index on student_name that is unique (this action, actually, force student_name be unique)

db.students.ensureIndex({''student_name':-1} , {unique:true} , {dropDups:true})
create index on student_name that is unique and delete all duplicates of the same student_name

db.students.ensureIndex({''student_name':-1} , {unique:true} , {sparce:true})
create index on student_name that is unique and only on the documents that has student_name not null

db.scores.totalIndexSize()
getting size of al indexes in the collection scores

db.scores.find({type:"essay" , score:50} ).hint({type: 1})
if in the collection score 2 indexes were added (one on type and one on score), the hint command forces to use index on type

db.scores.find({type:"essay" , score:50}).hint({$natural:1})
forces not tu use index at all

!!!!!!!!!!!!!!!!
Miscellaneous
!!!!!!!!!!!!!!!!

db.scores.find({type:"essay" , score:50}).explain()
getting information how find was actually perfomed

db.scores.stats()
getting collection statistics

db.system.profile.find()
finding logs of the selected DB

db.students.drop()
dropping students collection

db.dropDatabase()
dropping database

mongoimport -d school -c students < students.json
import documents from students.json to the school database to the students collection

Friday, 9 August 2013

Mongo shell CRUD examples

Here I want to post some examples of using MongoDB shell. Examples were taken from https://education.10gen.com from M101J course.

Here main CRUD commands usage.

!!!!!!!!!!!!!!!!
Insert
!!!!!!!!!!!!!!!!

db.fruit.insert({name:"apple" , color: "red"}, shape: "round");
insert a document into the "fruit" collection with the attributes of "name" being "apple", "color" being "red", and "shape" being round

!!!!!!!!!!!!!!!!
Find
!!!!!!!!!!!!!!!!

db.users.findOne({username:"dwight"}, {_id:false, email: true});
find one document where the key username is "dwight", and retrieve only the key named email

db.scores.find({type:"essay" , score:50} , {student: true, _id: false});
find all documents with an essay score equal to 50 and only retrieve the student field

db.scores.find({ score : { $gte : 50 , $lte : 60 } } );
finds documents with a score between 50 and 60, inclusive

db.users.find( { name : { $gte : "F" , $lte : "Q" } } );
find all users with name between "F" and "Q"

db.users.find({name:{$regex:"q"}, email:{$exists:true}});
retrieves documents from a users collection where the name has a "q" in it, and the document has an email field

db.users.find({name:{}$type:2})
retrieves documents from a users collection where field name is a String

db.scores.find({$or:[{score:{$lt:50}},{score:{$gt:90}}]});
find all documents in the scores collection where the score is less than 50 or greater than 90

db.people.find({$and : [{name : {$gt :" C"}} , {name : {$regex:"a"}}]});
find all people whose name sorts after the letter "C" and contains the letter "a"

db.products.find( { tags : "shiny" } );
could retrieve: { _id : 42 , name : "Whizzy Wiz-o-matic", tags : [ "awesome", "shiny" , "green" ] } and { _id : 1040 , name : "Snappy Snap-o-lux", tags : "shiny" }
find search in fields values and on top level arrays

db.users.find( { friends : { $all : [ "Joe" , "Bob" ] }, favorites : { $in : [ "running" , "pickles" ] } } )
here example of  $all and $in operators; $all works like kind of "and" in Java, and $in works like kind of "or"
could retrieve: { name : "Cliff" , friends : [ "Pete" , "Joe" , "Tom" , "Bob" ] , favorites : [ "pickles", "cycling" ] }

db.catalog.find({"price":{$gt:10000},"reviews.rating":{$gte:5}});
finds all products that cost more than 10,000 and that have a rating of 5 or better.

db.scores.find({type:"exam"}).sort({score:-1}).skip(50).limit(20);
retrieves exam documents, sorted by score in descending order, skipping the first 50 and showing only the next 20

db.scores.count({type:"essay", score:{$gt:90}})
count the documents in the scores collection where the type was "essay" and the score was greater than 90

!!!!!!!!!!!!!!!!
Update
!!!!!!!!!!!!!!!!

db.foo.update({_id:"Texas"},{population:30000000})
deletes everything in Texas (except _id) and insert {population:30000000}

db.users.update({username:"splunker"}, {$set:{country:"RU"}})
command for updating the country to 'RU' for only user with username:"splunker"

db.users.update({username:"jimmy"} , {$unset:{interests:1}})
deleting jimmy's interests

db.users.update({_id : 0} , {$set : {"a.2" : 5}});
set 3rd element value = 5

db.friends.update( { _id : "Mike" }, { $push : { interests : "skydiving" } } );
add to the "interests" array, element with value "skydiving" from the right hand side

db.friends.update( { _id : "Mike" }, { $pop : { interests : -1 } } );
remove from the "interests" array, the left-most element

db.friends.update( { _id : "Mike" }, { $pushAll: { interests : [ "skydiving" , "skiing" ] } } );
add to the "interests" array, elements with values "skydiving" and "skiing" from the right hand side

db.friends.update( { _id : "Mike" }, { $pull: { interests : "skiing"} } );
remove from the "interests" array, the element with value "skiing"

db.friends.update( { _id : "Mike" }, { $addToSet : { interests : "skydiving" } } );
if element exisits in array, "addToSet" does nothing, otherwise it acts like a "push"

db.scores.update( { score : { $lt: 70 } } , { $inc : { score : 20 } } , { multi : true } );
give every record whose score was less than 70 an extra 20 points

!!!!!!!!!!!!!!!!
Remove
!!!!!!!!!!!!!!!!

db.scores.remove({score : {$lt:60}});
delete every record whose score was less than 60

!!!!!!!!!!!!!!!!
Other
!!!!!!!!!!!!!!!!

db.runCommand { getLastError : 1}
getting result of the last command