Other Cool

MongoDb: A Quick overview

Mongodb contains databases

Databases have collections

Collections have Documents

Documents are exactly like JSON Objects.

Example:

blog.posts.find({num:1}); //blog is database, posts is collection, find, a method, that searches for document that has num value 1.

CRUD Operations..
..translated to MongoDB.

Create – Insert
Read – Find
Update – Update
Delete – Remove

Quick Intro To Shell:
– shell is a js environment.

Commands…

– help       //shows help
– show dbs  //shows all databases.
– use demo       //if db name demo doesn’t exist it will be created

::::Querying:::::
– db.coll.find()       //actually returns a cursor
– db.things.find()       //even though coll doesn’t exist so null.
– db.things.find(selection, projection)       //projection field=true==show only
– db.things.find().pretty()       //gives formatted json
– db.things.insert(jsondoc)

$gt // greater than
$lt
$types //defined by numbers 2 is string
$exists //if it exits
$ne //not equal to
$regex
$or //is prefix operator comes before query
$and //is prefix too, usual query is better way

db.scores.find({ scores: {$gt:59, $lt:90}, {type:”essay”} }, {name:true});
db.things.find({profession: {$exists: false}})
db.things.find({profession: {$regex: “^A”}})
db.people.find({ $or : [query, query]});

//Array: value in array is automatically searched too
db.coll.find({fav:”red”});
//match all in array instead of one value as before.
$all //like AND — all
$in //lin OR — any

Nested Documents…
//mongodb searches only top level docs so
…find({ email : { work : “hg@g.com” }})
//only match doc that has email record that matches this object extactly meaning same field, value, and order.
Using Dot Notation
…find({ “email.work” : “hg@g.com” })
//means that find doc with where email doc has work field with this value

Cursor
//.find() actually returns cursor but shell do magic
var cur = db.people.find();
cur.hasNext()
cur.next();

as long as you dont have checked cursor or called it you can impose limits, bcuz they are processed by db server and therefore can’t be applied after retiriving on client

cur.limit(5) -> returns cursor with 5 docs.
cur.sort({name:-1})
cur.skip(3)

cur.limit(5).sort(…).skip(3)… //Order matters.
db.coll.count(query)
::Updating:::
//in mongoshell update do 4 thigns
1st replace
2nd update
3rd thing, upsert
4th update multiple {multi:true}

//update only updates single doc by default.
db.coll.update(query, newDoc); //will replace..
db.coll.update(query, {$set : newDoc}); //will update or create..

$set // make update work like update.
$inc // field must be number
$unset //only remove a field not whole doc

Array Updating

//manipulate n element of array, name.2
db.coll.(query, {$set : {“name.2”: 5});
$push //only one that doesn’t treat array like set.
$pop : {name : 1}, $pop : {name : -1} //for let
$pushAll : { name : [2,3,4] } add all to array
$pull : {name : value }//like push but deletes a single value
$pullAll : { name : [..] } deletes all the matching.
$addToSet : { name : value } //only adds if doesn’t exist

db.coll.update( query, value , {upsert:true}); //will add if doesn’t exists…use with $set to further be careful.

db.coll.update( query, value , {multi:true}) //updates all matches
//updating happen async

::Removing::
//could already do it for fields using unset

db.coll.drop()
db.coll.remove({}); //removes all matched one by one.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s