Wednesday, August 13, 2014

M101J: MongoDB for Java Developers - Homework 2.1

Step 1:
Download Grades.js
 
Step 2:
Load grade.js using mongo import
 
mongoimport -d students -c grades < grades.js
 
Step 3:
use students
db.grades.count() 

Count Should be 800

Step 4:
Question? 
Find all exam scores greater than or equal to 65 
and sort those scores from lowest to highest. 
 
Understanding Question:
1) it says that we need to find score greater than or equal to 65.
so we will use $gte so our query criteria will be 
{"score":{"$gte":65}}
 
2) then it says to sort the score from lowest to highest.
for this we will use sort filter  and it is query criteria will be 
{"score":1}.

so our query will be 
 db.grades.find({"score":{"$gte":65.0}}).sort({"score":1})
 
Note: to sort in ascending order use  {"score":1}
to sort in descending order use  {"score":-1}
 
first document in the result you will find the answer 

3) optional : you can limit the number of rows, you will get only 1 document
by executing final query. so that you dont have to scroll. 
 
 
Final Query: 
 db.grades.find({"score":{"$gte":65.0}}).sort({"score":1}).limit(1).pretty();

Monday, August 11, 2014

M101J: Mongodb Homework 1.4

Step 1:
Download Handout
https://university.mongodb.com/static/10gen_2014_M101J_August/handouts/hw1-4.d1463929113a.zip

Step 2:
Uncompress the zip file

Step 3:
go to uncompress path in command prompt and run following command
mvn compile exec:java -Dexec.mainClass=com.tengen.Week1Homework4
 
if all is correct you will get message like
== Spark has ignited ...
>> Listening on 0.0.0.0:4567
 
Step 4:
Open http://localhost:4567. you will get your answer as 2805
 
enjoy your homework 

mongodb Homework: Homework 1.3

Step 1: Download handout
https://university.mongodb.com/static/10gen_2014_M101J_August/handouts/hw1-3.fac2c222ca2b.zip

Step 2:
uncompress it

Step 3:
in cmd go to uncompress path and run 
mvn compile exec:java -Dexec.mainClass=com.tengen.Week1Homework3
 
Step 4:
You will get 366 as answer. 

mongodb Homework: Homework 1.1

Step 1:
Download Link:
https://university.mongodb.com/static/10gen_2014_M101J_August/handouts/hw1-1.184820ec29b6.zip

Step 2:
unzip the Content and go to that path using command prompt

Step 3:
type mongorestore, it will load  json

connected to: 127.0.0.1
Mon Aug 11 14:59:29.188 dump\m101\funnynumbers.bson
Mon Aug 11 14:59:29.189         going into namespace [m101.funnynumbers]
100 objects found
Mon Aug 11 14:59:29.213         Creating index: { key: { _id: 1 }, ns: "m101.funnynumbers", name: "_id_" }
Mon Aug 11 14:59:29.459 dump\m101\hw1.bson
Mon Aug 11 14:59:29.459         going into namespace [m101.hw1]
1 objects found
Mon Aug 11 14:59:29.472         Creating index: { key: { _id: 1 }, ns: "m101.hw1", name: "_id_" }


Done. with your homework 1.1

M101J: MongoDB for Java Developers

Homework: Homework 1.2 

 Which of the following are valid JSON documents? Please choose all that apply.

1. {“name”:”Fred Flintstone”;”occupation”:”Miner”;”wife”:”Wilma”} 

2.{“title”:”Star Wars”, “quotes”:[“Use The Force”,”These are not the Droids you are looking for”],”director”:”George Lucas”}

3.{}

4.{“city”:”New York”, “population”, 7999034, boros:{“queens”, “manhattan”, “staten island”, “the bronx”, “brooklyn”}}

5.{“a”:1, “b”:{“b”:1, “c”:”foo”, “d”:”bar”, “e”:[1,2,4]}} 

 

Answer : 2,3,5

Invalid Json

1. {“name”:”Fred Flintstone”;”occupation”:”Miner”;”wife”:”Wilma”}  

4. {“city”:”New York”, “population”, 7999034, boros:{“queens”, “manhattan”, “staten island”, “the bronx”, “brooklyn”}}