Monday, November 10, 2014

MongoDB CRUD performance - Insert Operation

How to insert a document in mongodb ?
  1. Simple Insert - One Document at a Time
  2. Bulk Insert - Multiple Document at a Time.
Simple Insert :
db.collection.insert(<bson document>);

eg: db.collection.insert({"key":1000});

Java Example:
collection.insert(new BasicDBObject("key", 1000));

Bulk Insert:
db.collection.insert(<bson document 1>
<bson document 2>
)

eg:

db.collection.insert({"key":1000},{"key",2000});

Java Example:
collection.insert(new BasicDBObject("key", 1000),new BasicDBObject("key",2000));


MongoDB insert Performance using Java:

Code:
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class MongoInsertPerformance {
static int size = 10000;

public static void main(String[] args) throws UnknownHostException {

MongoClient mongoClient = new MongoClient("127.0.0.1");
DB db = mongoClient.getDB("mydb");
DBCollection collection = db.getCollection("test");
long startTime = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
collection.insert(new BasicDBObject("" + i, "" + i));
}
long endTime = System.currentTimeMillis();
System.out.println("Total Time taken in insert is "
+ (endTime - startTime));

// Bulk insert
collection.drop();
collection = db.getCollection("test");
startTime = System.currentTimeMillis();
List<DBObject> bulkInsertList = new ArrayList<DBObject>();
for (int i = 0; i < size; i++) {
bulkInsertList.add(new BasicDBObject("" + i, "" + i));
}
collection.insert(bulkInsertList);
endTime = System.currentTimeMillis();
System.out.println("Total Time taken in insert is "
+ (endTime - startTime));
}

}


Output with 10K :
Total Time taken in simple insert is 2019

Total Time taken in bulk insert is 192


Output with 100K :
Total Time taken in simple insert is 13661
Total Time taken in bulk insert is 1197









Friday, September 19, 2014

M101J: MongoDB for Java Developers Final: Question 1

M101J: MongoDB for Java Developers Final: Question 1
Step 1:
download the Enron email dataset enron.zip

Step 2:
extract enron.zip and from command prompt type
mongorestore --host 192.168.50.4 --port 27017  messages.bson

Step 3:
Check the data that has been imported.
using
1) db.enron.messages.find().count()  it should be 120,477 documents after restore.

2) db.messages.find({"headers.From":"andrew.fastow@enron.com", "headers.To": "john.lavorato@enron.com"}).count() will result in 1.
this will ensure you have correct data to work on

Solution:
type below query:
db.messages.find({"headers.From":"andrew.fastow@enron.com", "headers.To": "jeff.skilling@enron.com"}).count()

you will get your answer as 3




Sunday, September 7, 2014

M101J: MongoDB for Java Developers Homework 5.4

M101J: MongoDB for Java Developers Homework 5.4

Answer is 298015

M101J: MongoDB for Java Developers Homework 5.3

M101J: MongoDB for Java Developers Homework 5.3

Answer is 1

M101J: MongoDB for Java Developers Homework 5.2

M101J: MongoDB for Java Developers Homework 5.2


Query:
db.zips.aggregate([ { $group:{ "_id":{ "state":"$state", "city":"$city" }, "pop":{ $sum:"$pop" } } }, { $match:{ "_id.state":{ $in:[ "CA", "NY" ] }, "pop":{ $gt:25000 } } }, { $group:{ "_id":null, "pop":{ $avg:"$pop" } } } ])


Answer is 44805

M101J: MongoDB for Java Developers Homework 5.1

M101J: MongoDB for Java Developers Homework 5.1

Question: Finding the most frequent author of comments on your blog.

 Solution:

you need to use webshell to find the most frequent author of comments 

Step 1:

Understand Structure of posts collection

{
    "_id" : ObjectId("540d427e132c1f13547188cc"),
    "body" : "empty_post",
    "permalink" : "cxzdzjkztkqraoqlgcru",
    "author" : "machine",
    "title" : "US Constitution",
    "tags" : [
        "january",
        "mine",
        "modem",
        "literature",
        "saudi arabia",
        "rate",
        "package",
        "respect",
        "bike",
        "cheetah"
    ],
    "comments" : [
        {
            "body" : "empty_comment",
            "email" : "eAYtQPfz@kVZCJnev.com",
            "author" : "Kayce Kenyon"
        },.........

 

2) we need to count comments so we will unwind the comments first using

 {
        $unwind: "$comments"
    }

 

3) then we need to group comment as per author so we will add group query with it count using sum.

{
$group: {
"_id": "$comments.author",
"num_comments": {
$sum: 1
}
}
}

 4) then we will sort from max to min number of comments. so we will add 

{
        $sort: {
            "num_comments": 1
        }
    }


5) note: this is large data so we will add limit to 1 rows by using
{$limit: 1}

so your final query will be

db.posts.aggregate([
{
$project: {
"_id": 0,
"comments": 1
}
},
{
$unwind: "$comments"
},
{
$group: {
"_id": "$comments.author",
"num_comments": {
$sum: 1
}
}
},
{
$sort: {
"num_comments": 1
}
},
{
$limit: 1
}
]
};
AND ANSWER I GOT IS Gisela Levin

 

Monday, September 1, 2014

M101J: MongoDB for Java Developers Homework 4.4

M101J: MongoDB for Java Developers Homework 4.4

Step 1:
Download the handout

Step 2:
import the Sysprofile data using
mongoimport -d m101 -c profile < sysprofile.json

Step 3:
you need to look into data or write a query to find the maximum latency in milli second

so we will filter and apply max on "millis" key of document.


Answer was: 15820