Sunday, September 20, 2015

MongoDB CRUD with examples


MongoDB is a document-based database where there are no tables or SQL queries. Records are stored as documents JSON format

MongoDB CRUD Operations Introduction:

In MongoDB, records are stored as JSON format, that is like key-value pairs. This structure of storing as key-value pairs is similar to dictionaries, hashes, maps or associative-arrays in programming languages like Python, Perl, Java, Go etc. CRUD stands for Create, Read, Update and Delete operations in Mongo. Install MongoDB and Mongo Shell in your workstation to play with the query operations explained below.

Jump into Mongo shell,
Connect to mongo shell from your workstation(I have a datacenter cluster that has mongo to store lot of data about the cluster),
root@mycluster:~# mongo
MongoDB shell version: 2.0.4
connecting to: test
>
> show dbs  (equivalent to show databases in mysql, this command will list all databases)
school    0.078GB
students  0.078GB
test    (empty)

Here 'school', 'students' and 'test' are the two databases or call in as documents in mongo way.
Now connect to 'events' document and list out all the tables(collections) in the 'events' database(document)

> use students  (equivalent to use '<database_name>' in mysql)
switched to db events
>
> show collections (equivalent to 'show tables' in mysql)
grades
system.indexes
>

In the 'grades' collections, there are 20 documents as given below. Please read the documents with more attention, we will run all below explained query on these document,
> db.grades.find()
{ "_id" : ObjectId("50906d7fa3c412bb040eb577"), "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb578"), "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb579"), "student_id" : 0, "type" : "homework", "score" : 14.8504576811645 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57a"), "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57b"), "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57d"), "student_id" : 1, "type" : "homework", "score" : 21.33260810416115 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57e"), "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57f"), "student_id" : 2, "type" : "exam", "score" : 19.88180838833524 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb580"), "student_id" : 2, "type" : "quiz", "score" : 1.528220212203968 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb581"), "student_id" : 2, "type" : "homework", "score" : 60.9750047106029 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb582"), "student_id" : 2, "type" : "homework", "score" : 97.75889721343528 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb583"), "student_id" : 3, "type" : "exam", "score" : 92.6244233936537 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb584"), "student_id" : 3, "type" : "quiz", "score" : 82.59760859306996 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb585"), "student_id" : 3, "type" : "homework", "score" : 50.81577033538815 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb586"), "student_id" : 3, "type" : "homework", "score" : 92.71871597581605 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb587"), "student_id" : 4, "type" : "exam", "score" : 87.89071881934647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb588"), "student_id" : 4, "type" : "quiz", "score" : 27.29006335059361 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb589"), "student_id" : 4, "type" : "homework", "score" : 5.244452510818443 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb58a"), "student_id" : 4, "type" : "homework", "score" : 28.656451042441 }
>



Queries covered in this blog are,

db.grades.find()
  •    Finding documents with operations like greater than, lesser than etc as below
    • $gt, $gte, $lt, $lte (greater than, greater than equals, lesser than, lesser than equals)
    • $exists ( if any document with given attribute exists)
    • $type (BSON type-Binary of JSON type, imagine as data-type in programming language),
      • String type is 2
      • Array type is 4
      • Date 5
db.grades.findOne()
db.grades.insert()
db.grades.update()
db.grades.remove()

Above queries with equivalent SQL queries,
> db.grades.find({}, {score:1}) //select score from grades;
{ "_id" : ObjectId("50906d7fa3c412bb040eb577"), "score" : 54.6535436362647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb578"), "score" : 31.95004496742112 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb579"), "score" : 14.8504576811645 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57a"), "score" : 63.98402553675503 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57b"), "score" : 74.20010837299897 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57d"), "score" : 21.33260810416115 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57e"), "score" : 44.31667452616328 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57f"), "score" : 19.88180838833524 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb580"), "score" : 1.528220212203968 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb581"), "score" : 60.9750047106029 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb582"), "score" : 97.75889721343528 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb583"), "score" : 92.6244233936537 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb584"), "score" : 82.59760859306996 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb585"), "score" : 50.81577033538815 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb586"), "score" : 92.71871597581605 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb587"), "score" : 87.89071881934647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb588"), "score" : 27.29006335059361 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb589"), "score" : 5.244452510818443 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb58a"), "score" : 28.656451042441 }
> db.grades.find({score: {$gt: 95}}) //select * form grades where score > 95
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb582"), "student_id" : 2, "type" : "homework", "score" : 97.75889721343528 }
>
> db.grades.find({type: "quiz", score:{$gt:60}}) 
//select * form grades where type='quiz' and score > 60
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb584"), "student_id" : 3, "type" : "quiz", "score" : 82.59760859306996 }
>




No comments:

Post a Comment