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

No comments:

Post a Comment