Showing posts with label week4. Show all posts
Showing posts with label week4. Show all posts

Monday, April 13, 2015

M102: MongoDB for DBAs - Homework 4.2


Step 1:
You need to run all the replica Set use below command
mongod --dbpath "c://1" --replSet abc --port 27002
mongod --dbpath "c://2" --replSet abc --port 27002
mongod --dbpath "c://3" --replSet abc --port 27003

Step 2:
Note: do close mongo shell, if you have close then reload the data as per step given in homework 4.1

Step 3:
Run on mongo shell
rs.initiate()
then
use replication
then db.foo.find() to check data

then run

homework.b()
you will get your answer as 5002



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

M101J: MongoDB for Java Developers Homework 4.3

M101J: MongoDB for Java Developers Homework 4.3:

Step 1: Download Handout 

Step 2:
Problem Statement: we need to make blog fast by adding index to post collection.

Before that you need to import post.json

Steps to import:
from mongo shell 
>use blog
>db.posts.drop()
from the terminal window, you can go to uncompress directory of homework
and try below query to import.
mongoimport -d blog -c posts < posts.json


now, we need to improve performance for
1) Blogs Home page.
2)The page that displays blog posts by tag (http://localhost:8082/tag/whatever)
3) The page that displays a blog entry by permalink (http://localhost:8082/post/permalink)


1) For Query DBCursor cursor = postsCollection.find().sort(new BasicDBObject().append("date", -1)).limit(limit);
 

You can add
db.posts.ensureIndex({ date: -1})

2) for         
DBObject post = postsCollection.findOne(new BasicDBObject("permalink", permalink));

you can add index on 
db.posts.ensureIndex({ permalink: 1}, {unique: true})
 BasicDBObject query = new BasicDBObject("tags", tag);
     
   System.out.println("/tag query: " + query.toString());
        

DBCursor cursor = postsCollection.find(query).sort(new BasicDBObject().append("date", -1)).limit(10);


you can add index on 

 db.posts.ensureIndex({ tags: 1})

and then submit you answer in mongo proc.

Thursday, August 28, 2014

M101J: MongoDB for Java Developers Homework 4.2

M101J: MongoDB for Java Developers Homework 4.2
Question:
Suppose you have a collection called tweets whose documents contain information about the created_at time of the tweet and the user's followers_count at the time they issued the tweet. What can you infer from the following explain output?
db.tweets.find({"user.followers_count":{$gt:1000}}).sort({"created_at" : 1 })
.limit(10).skip(5000).explain()
{
        "cursor" : "BtreeCursor created_at_-1 reverse",
        "isMultiKey" : false,
        "n" : 10,
        "nscannedObjects" : 46462,
        "nscanned" : 46462,
        "nscannedObjectsAllPlans" : 49763,
        "nscannedAllPlans" : 49763,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 205,
        "indexBounds" : {
                "created_at" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ]
        },
        "server" : "localhost.localdomain:27017"
}
 
Option:
 
1) This query performs a collection scan.  
True, it perform a collection scan.
 
2)The query uses an index to determine the order in which to return result documents. 
True, it uses an index to determine result
 
3)The query uses an index to determine which documents match.
false, for matches there is no index, index are use only for ordering.
 
4) The query returns 46462 documents. 
false, You can see the query it has limit to 10 , so it return only 10 result.
 
5)The query visits 46462 documents. 
true,  yes it has scanned 46462 document.
 
6)The query is a "covered index query". 
false, query is colvered index query.
 
So answer for week 4 - Homework 4.2 are 
 
 
  
   
  

M101J: MongoDB for Java Developers Homework 4.1

M101J: MongoDB for Java Developers Homework 4.1

This HomeWork is related to find which query will be using index to get result.

you can use query to find list of index


db.system.indexes.find()

Document:
{
  "v" : 1,
  "key" : {
   "sku" : 1
  },
                "unique" : true,
  "ns" : "store.products",
  "name" : "sku_1"
 }
 
Key Indicates there exist an index sku on collection products with index name as
 sku_1.[at the time of index creation if we give {sku:-1} name of index will be
 sku_-1]
 
db.collectionname.getIndexes() can be used to get index in individual collection


Now lets complete the homework.
Which of the following queries can utilize an index. Check all that apply.
1) db.products.find({'brand':"GE"}) 
if you check in document , there is no index with brand, you will find brand
index as multi index with category. but in query we are only using brand so above
query cannot utilize and index.
 
2)db.products.find({'brand':"GE"}).sort({price:1}) 
if you check in document, you will see brand cannot use an index, but in document
if you see price is present as single index. so the above query use an index.
 
3)db.products.find({$and:[{price:{$gt:30}},{price:{$lt:50}}]}).sort({brand:1}) 
 price is present, therefore query is using index.
 
4) db.products.find({brand:'GE'}).sort({category:1, brand:-1}).explain() 
 category and brand are present in document as multi - index  but if you check
index is created with {category:1, brand:1} but while using in query it is using 
{category:1, brand:-1}, so for sorting mongo will perform complete scan and
 it will not use index . if index was created with {category:1, brand:-1} then
the above query will be using an index.
 
So your homework 4.1 answer is 
 
 


  
    
 

M101J: MongoDB for Java Developers - Week 4

M101J: MongoDB for Java Developers - Week 4

Mongodb week 4 is related with peformance, you will learn
1) what is index in mongodb and how will you  create it?
2) Identify slow query.
3) Overview of Sharding.