M101n Homework 2.3

[Solution] Week 2 : CRUD : M101P: MongoDB for Developers

Homework 2.1 : 

In this problem, you will be using a collection of student scores that is similar to what we used in the lessons. Please download grades.json from the Download Handout link and import it into your local mongo database as follows:

mongoimport --drop -d students -c grades grades.json

The dataset contains 4 scores for 200 students.

First, let's confirm your data is intact; the number of documents should be 800.

use students db.grades.count() You should get 800.

This next query, which uses the aggregation framework that we have not taught yet, will tell you the student_id with the highest average score:

db.grades.aggregate({'$group':{'_id':'$student_id', 'average':{$avg:'$score'}}}, {'$sort':{'average':-1}}, {'$limit':1})

The answer should be student_id 164 with an average of approximately 89.3.

Now it's your turn to analyze the data set. Find all exam scores greater than or equal to 65, and sort those scores from lowest to highest.

What is the student_id of the lowest exam score above 65?

Solution :

    Query : db.grades.find( { score : { $gte : 65 } } ).sort( { score : 1 } )

    Answer : 22

Homework 2.2 : 

Write a program in the language of your choice that will remove the grade of type "homework" with the lowest score for each student from the dataset in the handout. Since each document is one grade, it should remove one document per student. This will use the same data set as the last problem, but if you don't have it, you can download and re-import.

The dataset contains 4 scores each for 200 students.

First, let's confirm your data is intact; the number of documents should be 800.

use students db.grades.count() Hint/spoiler: If you select homework grade-documents, sort by student and then by score, you can iterate through and find the lowest score for each student by noticing a change in student id. As you notice that change of student_id, remove the document.

To confirm you are on the right track, here are some queries to run after you process the data and put it into the grades collection:

Let us count the number of grades we have:

db.grades.count() The result should be 600. Now let us find the student who holds the 101st best grade across all grades:

db.grades.find().sort( { 'score' : -1 } ).skip( 100 ).limit( 1 )
The correct result will be:
{ "_id" : ObjectId("50906d7fa3c412bb040eb709"), "student_id" : 100, "type" : "homework", "score" : 88.50425479139126 }

Now let us sort the students by student_id , and score, while also displaying the type to then see what the top five docs are:

db.grades.find( { }, { 'student_id' : 1, 'type' : 1, 'score' : 1, '_id' : 0 } ).sort( { 'student_id' : 1, 'score' : 1, } ).limit( 5 )

The result set should be:

{ "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 } { "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 } { "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 } { "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 } { "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 }
To verify that you have completed this task correctly, provide the identity of the student with the highest average in the class with following query that uses the aggregation framework. The answer will appear in the _id field of the resulting document.

db.grades.aggregate( { '$group' : { '_id' : '$student_id', 'average' : { $avg : '$score' } } }, { '$sort' : { 'average' : -1 } }, { '$limit' : 1 } ) Enter the student ID below. Please enter just the number, with no spaces, commas or other characters.

Solution :    Answer : 54

Homework 2.3 : 

Blog User Sign-up and Login

Download the handout and unpack it.

You should see three files at the highest level: blog.py, userDAO.py and sessionDAO.py. There is also a views directory which contains the templates for the project.

The project roughly follows the model/view/controller paradigm. userDAO and sessionDAO.py comprise the model. blog.py is the controller. The templates comprise the view.

If everything is working properly, you should be able to start the blog by typing:

python blog.py Note that this project requires the following python modules be installed on your computer: cgi, hmac, datetime, json, sys, string, hashlib, urllib, urllib2, random, re, pymongo, and bottle. If you have python installed at all, you probably already have most of these installed except pymongo and bottle.

If you have python-setuptools installed, the command "pip" makes this simple. Any other missing packages will show up when validate.py is run, and can be installed in a similar fashion. As of this recording, we are still using the beta version of PyMongo so we will install directly from GitHub. Note that this directions are identical to what we taught you within the lessons and you should already have PyMongo 3.x and bottle installed. If not, use the following commands.s.

First, let's confirm your data is intact; the number of documents should be 800.

$ pip install https://github.com/mongodb/mongo-python-driver/archive/3.0b1.tar.gz $ pip install bottleIf you go to http://localhost:8082 you should see a message, "this is a placeholder for the blog"

Here are some URLs that must work when you are done.

http://localhost:8082/signup http://localhost:8082/login http://localhost:8082/logout

This next query, which uses the aggregation framework that we have not taught yet, will tell you the student_id with the highest average score:

Solution :  jkfds5834j98fnm39njf0920f02

Homework 2.4 :

Which of the choices below is the title of a movie from the year 2013 that is rated PG-13 and won no awards? Please query the video.movieDetails collection to find the answer.

NOTE: There is a dump of the video database included in the handouts for the "Creating Documents" lesson. Use that data set to answer this question.

Homework 2.5 :

Using the video.movieDetails collection, how many movies list "Sweden" second in the the list of countries.

NOTE: There is a dump of the video database included in the handouts for the "Creating Documents" lesson. Use that data set to answer this question.


Feel free to comment below your experience with above approach and If you still find any problem  with above steps Let me know I would love to help you to resolve your  problem.

m101n Mongodb for Net Developers

August 20, 2017 | Author: F.N. Benea | Category: Mongo Db, Database Index, Software Engineering, Computer Programming, Databases

Share Embed Donate

Report this link

Short Description

Cursul Mongodb for Net Developers de pe Youtube...


Sheet1 LECTURE WEEK 1: INTRODUCTION Welcome to the class Introduction to M101N What is MongoDB? MongoDB Relative to Relational Application Architecture Quick Introduction to the Mongo Shell Introduction to JSON Installing MongoDB (windows) MongoDB is Schemaless JSON Revisited JSON Spec Introduction to Our Class Project, the Blog Blog in Relational Tables Blog in Documents Introduction to Schema Design H1.1 WEEK 2: CRUD

https://youtu.be/B_MBXbP9LSE https://youtu.be/hVcGEb7HiDc https://youtu.be/Lfl8hdQOi6Y https://youtu.be/-KIC1LXxcGM https://youtu.be/RDEhRO_FwGE https://youtu.be/e18vCIdQKp4 https://youtu.be/PTATjNSjbJ0 https://youtu.be/sBdaRlgb4N8 https://youtu.be/uKB-Hoqs6zI https://youtu.be/CTffxoSSLqg https://youtu.be/kOrsT94-A28 https://youtu.be/NTDgDqgs9gk https://youtu.be/kqglm5c52oI https://youtu.be/JFkBoIxjLqo https://youtu.be/6XE3wZCPiZ8 https://youtu.be/TV7RWf6DBbc

Introduction to Week 2 https://youtu.be/H_OcFFkRYX0 CRUD and the Mongo Shell https://youtu.be/C7LinMC2o5o Secrets of the Mongo Shell https://youtu.be/IIIzjPp-IRE BSON Introduced https://youtu.be/K3J6WvDW-Hc Inserting Docs https://youtu.be/qqfVxGLIrLg Introduction to findOne https://youtu.be/w9V0fJsDwbQ Introduction to find https://youtu.be/8kKfFK6a0Ak Querying Using field Selection https://youtu.be/UIg86QjSoyY Querying Using $gt and $lt https://youtu.be/FHLrz4VGzkg Inequalities on Strings https://youtu.be/imCCKOevU3c Using regexes, $exists, $type https://youtu.be/lI-jhqYf1JY Using $or https://youtu.be/BW5ElNCRZps Using $and https://youtu.be/hYk7pjgjjzc Querying Inside Arrays https://youtu.be/jvEqwW75Bus Using $in and $all https://youtu.be/QU2NrkviORE Queries with Dot Notation https://youtu.be/NrjFECIfwqk Querying, Cursors https://youtu.be/3jA6iFSEJOI When can you change the behavior of a c Counting Results https://youtu.be/eKD5bVmNQMI Wholesale Updating of a Document https://youtu.be/g7Fi1xXsuvU Using the $set Command https://youtu.be/XyhNjs2pNVc Using the $unset Command https://youtu.be/LpErz8jLW0I Using $push, $pop, $pull, $pushAll, $pullAll, $addToSet https://youtu.be/GOn0EWKDQoY Upserts https://youtu.be/Dy2p8k3EZs4 Multi-update https://youtu.be/2GNNdUmDL-4 Removing Data https://youtu.be/5K0t1dU8IJY getLastError (MongoDB v2.4 and earlier) https://youtu.be/a6o0TQjUMug Getting Started with the .NET Driver https://youtu.be/ujuCwFiqfpA .NET driver, MongoClient, MongoDatabase, MongoCollectionhttps://youtu.be/3xaIrMiGzT0 .NET driver, document representation https://youtu.be/-cOlCv2ptCA .NET Driver, Poco Representation https://youtu.be/wYHn8sqBMuI .NET Driver, InsertOne https://youtu.be/k3J1tLnJSqc .NET Driver, Find https://youtu.be/vgAmMCxzmzo Which method name is NOT a way to

Page 1

Sheet1 .NET Driver, Find with Filters .NET Driver, Find with Skip, Limit and Sort .NET Driver, Find with Projections .NET Driver, UpdateOne and UpdateMany .NET Driver, DeleteOne and DeleteMany .NET Driver, FindOneAndUpdate, FindOneAndReplace, Fin .NET driver, BulkWrite WEEK 3: SCHEMA DESIGN Introduction to Week 3 MongoDB Schema Design Relational Normalization Mongo Design for Blog Alternative Schema for Blog Living Without Constraints Living Without Transactions One to One Relations One to Many Relations Many to Many Relations Multikeys Benefits of Embedding Trees When to Denormalize WEEK 4: PERFORMANCE Introduction to Week 4 Storage Engines: Introduction Storage Engines: MMAPv1 Storage Engines: WiredTiger Indexes Creating Indexes Discovering (and Deleting) Indexes Multikey Indexes Dot Notation and Multikey Index Creation Option, Unique Index Creation, Sparse Index Creation, Background Using Explain Explain: Verbosity Covered Queries When is an Index Used? How Large is Your Index? Number of Index Entries Geospatial Indexes Geospatial Spherical Text Indexes Efficiency of Index Use Efficiency of Index Use Example Logging Slow Queries Profiling Mongotop

https://youtu.be/yEiIBTMrN7E https://youtu.be/vvm1RX85yqo https://youtu.be/pZTlQgPSi_M https://youtu.be/XpHsKDcWlrM https://youtu.be/dDo891eZfe0 https://youtu.be/o0d6vYZLhqI https://youtu.be/Q2na8Ks_sLM Homework 2.1 Homework 2.2 Homework 2.3 https://youtu.be/9urnY4OcuNM https://youtu.be/AABYJM12qF4 https://youtu.be/GX__f2s4hd8 https://youtu.be/PRylEHH5t84 https://youtu.be/ZvqNWVWB2-o https://youtu.be/YFRMkDPaams https://youtu.be/FfRr3qjRfww https://youtu.be/cCsfon0vUlQ https://youtu.be/EIaP1KbVkUc https://youtu.be/fEYYjZ7zEHc https://youtu.be/KtIY4Q1tUao https://youtu.be/XIN0Dqht08Q https://youtu.be/lIjXyQklGWY https://youtu.be/jDZ-HFoJ0vg Homework 3.1 Homework 3.2 Homework 3.3 https://youtu.be/GGMfM3pYK_4 https://youtu.be/YvK7I9fYpK4 https://youtu.be/os3591KviNM https://youtu.be/aNsugW7r3mM https://youtu.be/U3iWPF5jP-g https://youtu.be/xi2gtzZez6Q https://youtu.be/dX49IcmTrGA https://youtu.be/_NGwn_X82Dw https://youtu.be/wT0_ktAZbBg https://youtu.be/D-Ra5TEaaL4 https://youtu.be/ZznHByqtTMA https://youtu.be/AchmKNj2qhw https://youtu.be/r5YeICVzDjQ https://youtu.be/WxXVun6bZ20 https://youtu.be/QyV79jsSJ9Y https://youtu.be/JyQlxDc549c https://youtu.be/wjA0eo_lihg https://youtu.be/xiujksUfzUA https://youtu.be/UKUDYqNVL6I https://youtu.be/pULU4DVsUWQ https://youtu.be/nLau5Fx9LC8 https://youtu.be/JJmIf0pn100 https://youtu.be/g032EW67SRA https://youtu.be/aWuvC-O7Qkk https://youtu.be/pN1Yhrup9-I https://youtu.be/D9YLXgy7NYo

Page 2

Sheet1 Mongostat Sharding Overview Homework 4.1 Homework 4.2 Homework 4.3 Homework 4.4

https://youtu.be/E2aDTSes3Wc https://youtu.be/BDxT-VZdYqc db.products.find( { 'brand' : "GE" } ).sort( { price251120 :1}) The query examines documents. The problem you'll notice is the db.profile.find({ns: "school2.students" }

WEEK 5: AGGREGATION FRAMEWORK Introduction to Week 5 Simple Aggregation Example The Aggregation Pipeline Simple Example Expanded Compound Grouping Using a document for _id Aggregation Expressions Using $sum Using $avg Using $addToSet Using $push Using $max and $min Double $group stages Using $project Using $match Using $sort Using $limit and $skip Revisiting $first and $last Using $unwind $unwind example Double $unwind Mapping between SQL and Aggregation Some Common SQL examples Limitations of the Aggregation Framework Aggregation in .NET

https://youtu.be/EaEIHm3CMQM https://youtu.be/DQBXCsjeO5c https://youtu.be/AuO8CEkTG6Y https://youtu.be/3lEpnMcfpCs https://youtu.be/qTbtax_cKcc https://youtu.be/zoN4cj_XQzY https://youtu.be/L4G14MTfTgQ https://youtu.be/93MSz3uDC1A https://youtu.be/baIDZ-M5j7w https://youtu.be/YzURaZnKI9s https://youtu.be/LQcBM-g0ACY https://youtu.be/BYoNX4trjOQ https://youtu.be/EIWF9Oxeb8M https://youtu.be/TbQ2PI5Fib0 https://youtu.be/7RtHG90Hrbw https://youtu.be/HUEtV7omSb8 https://youtu.be/o5hzYKXUyrU https://youtu.be/JOdAnxVAMwc https://youtu.be/E4aYOQPeQvI https://youtu.be/U_4Enh2TTp4 https://youtu.be/YXGL27217B8 https://youtu.be/auL2R0XKlyM https://youtu.be/ep2gLSR6C0U https://youtu.be/U_gRSxEq3c0 https://youtu.be/Lyysv6LpHDw

WEEK 6: APPLICATION ENGINEERING Introduction to Week 6 Write Concern Network Errors Introduction to Replication Replica Set Elections Write Consistency Creating a Replica Set Replica Set Internals Failover and Rollback Connecting to a Replica Set from .NET Driver Bad Things Happen to Good Nodes Write Concern Revisited Read Preferences Review of Implications of Replication Introduction to Sharding Building a Sharded Environment

https://youtu.be/BmQuvmpt_ZQ https://youtu.be/oRDYNWCYnGo https://youtu.be/xWNzCkTCN-M https://youtu.be/f1WTYGORU3w https://youtu.be/WFXSVHO78bQ https://youtu.be/Oqf_Eza-s1M https://youtu.be/flCFVFBRsKI https://youtu.be/6GbrJmxCEl0 https://youtu.be/IW1oW_Adlt0 https://youtu.be/pli6Gsi7Z-k https://youtu.be/7ZYqrmGOlpc https://youtu.be/5VyXyccjS3k https://youtu.be/mhHaS4ZWzZE https://youtu.be/K5ISnvYKQFQ https://youtu.be/_GfDqa1qRl0 https://youtu.be/dn45G2yw20A

Page 3

Sheet1 Implications of Sharding Sharding + Replication Choosing a Shard Key

https://youtu.be/ig278F60gRA https://youtu.be/gkUCUbM0oEg https://youtu.be/8q2GB3QSBSI

WEEK 7: CASE STUDIES Introduction to Week 7 Interview with Charity Majors, Parse Interview with Ryan Bubinski, Codecademy

https://youtu.be/eL054c6nkaY https://youtu.be/PTp0A8_IVAY https://youtu.be/RkPmVQNesZA


Page 4


Homework & Final Exam https://youtu.be/CAR42sMkXEo https://youtu.be/IAvnMgvHuLw https://youtu.be/YQTqdLe3W4M https://youtu.be/uWcOsdV4Iz4

{"fruit":["apple", "pear", "peach"]}

5 1 https://youtu.be/wiwOEG_6ojs https://youtu.be/pLW6hX-hvAA { "title" : "Star Wars", -1030 https://youtu.be/nWOt4VN6Y3g https://youtu.be/a6TFwpCoLoY https://youtu.be/4hsTQrMs-xY https://youtu.be/BRAVnBz3ogE https://youtu.be/uN_wf5a3BE4 https://youtu.be/ouHFZgtEAuU https://youtu.be/ouHFZgtEAuU https://youtu.be/tY7I1l8JdDI https://youtu.be/ZRWT0lcHsoU https://youtu.be/yMi7PwOoqXY https://youtu.be/diimYkvr374 https://youtu.be/oIkSajy8NLw https://youtu.be/gwECaa4N9Xc https://youtu.be/6O1UGHwuEww https://youtu.be/yiEjK_V008s This can be done at any point before the first document is called and before you've checked to see if it is empty. https://youtu.be/Uozp0diXOc8 https://youtu.be/pwnRUdtI8lY https://youtu.be/G2e5ZxqR6nw https://youtu.be/-CzLvQS12wY https://youtu.be/bGbiIGEM0FY https://youtu.be/Loo5XKQAryM https://youtu.be/puGW4t5wmOU https://youtu.be/AGvJs8xqZJg

MongoDB.Driver doc.Add(“field”, “value”);

42 {}

db.scores.find({type: "essay", score: 50},{_id:false, student:true})

2 db.users.find({name : {$regex : "q"}, email:{$exists:true}}) db.scores.find({$or:[{score:{$lt:50}},{score:{$gt:90}}]})

db.scores.count({type:"essay", score:{$gt:90}}) db.users.update({'username': 'splunker'}, {'$set': {'country': 'RU'}})

doc[“field”] = “value”;


Page 5


https://youtu.be/Wfsll8tt_ng 54


https://youtu.be/OVDnLJXpqNE https://youtu.be/s5E0T4caqj4 https://youtu.be/3VckBMGInic https://youtu.be/189zABW3HHk https://youtu.be/BjW64RkzlGg https://youtu.be/KQcspnr-Y2M


https://youtu.be/SR76BjJl-PQ This is the basic idea behind why we call it MMAPv1. https://youtu.be/dmnPb-hhVHM https://youtu.be/5Z6V8mJ8eS4 https://youtu.be/9FUOetT45AM https://youtu.be/caSP0dO02iU

https://youtu.be/ia6dzcj-ZMk https://youtu.be/yV_KEDEzOyc x https://docs.mongodb.org/manual/core/index-creation/?_ga=1.173842341.745792798.1450433650#index-creation-background https://youtu.be/Gpj_s49H_rQ https://youtu.be/qBFFnnoK1NA https://youtu.be/MxHsmUXjkKo db.foo.find( { a : 3 } ) & db.foo.find( { c : 1 } ).sort( { a : 1, b : 1 } ) https://youtu.be/mXHUIghDkFw https://youtu.be/4UW_oMSswTo db.places.find({location:{$near:[74,140]}}).limit(3) https://youtu.be/hVLa0H7Oe98 db.stores.find({ loc:{ $near: { $geometry: https://youtu.be/YtRnCyKS1VE { type: "Point", coordinates: [-130, 39]}, $maxDistance:1000000 } } }) https://youtu.be/7OK0e1gftjg complete the result set, without the need https://docs.mongodb.org/manual/reference/operator/query/regex/?_ga=1.188480174.74579 to scan a substantially larger number of Equality fields before range fields Sort index before keys (orrange documents) in orderfields to fields fields Equality before sort fields db.system.profile.find({millis: https://youtu.be/A4x4kE02pSc {$gt:1000}}).sort({ts:-1}) -

Page 6


The getmore column concerns the number of https://youtu.be/etOEgMTFKW0 requests per time interval to get additional data db.products.find( { $and : [ { price : { $gt from a cursor : 30 } },{ price : { $lt : The uses an index to the 50 } query } ] } ).sort( { brand : 1determine }) order in which to return result documents. 'db.posts.explain("executionStats").find({ "Tags" : "asd"}).sort({"CreatedAtUtc":15820

https://youtu.be/Nt0cQI86G40 https://youtu.be/mcnGfI69rRc https://youtu.be/nr1E1qTyIHU https://youtu.be/bIlwnlYBRA0

https://youtu.be/kJ0k2na4ukU https://youtu.be/ATO_s_Ah08o https://youtu.be/tT6Jx8s9dTc https://youtu.be/zGcTxUQuLGE https://youtu.be/hFqDvVXtm6E https://youtu.be/sHCdOiCispA https://youtu.be/J-asAAEHJ0Q https://youtu.be/IGN0lXg-kJ0 https://youtu.be/CHGiumXE-pU https://youtu.be/-cghyd6AHHA https://youtu.be/joRw-fqCIWA https://youtu.be/MVSoinykIJA https://youtu.be/Xfl3m7wz8ts https://youtu.be/jAWL-BJD0tI https://youtu.be/pziFq1oVRI4

https://youtu.be/6b5-Eg-vNMY https://youtu.be/xrruRJHSpzc https://youtu.be/XCssKzhlnZU https://youtu.be/vrUxTTD-XwA https://youtu.be/SjaJEYK1u3E https://youtu.be/ibZVf59aUj8 https://youtu.be/kGDhzwLZmXU https://youtu.be/t3rNxRg6WZE

https://youtu.be/hQrCwRplszo https://youtu.be/wWG5GUt76lw https://youtu.be/8wuWShep8Qs https://youtu.be/hy1MDmvRElQ

Page 7

Sheet1 https://youtu.be/vfl0hqdgB3Q https://youtu.be/hnuiYokdO7g https://youtu.be/91tO_iGga5w

Page 8


{ "a" : 1, "b" : { "b" : 1

e: 50},{_id:false, student:true})

"}, email:{$exists:true}}) }},{score:{$gt:90}}]})

Page 9


4,140]}}).limit(3) All 3

Page 10

Sheet1 the faults column appears only in the mmapv1 output

"$state", pop:{$max:"$pop"}}}])

0, city:{$toLower:"$city"}, pop:1, state:1, zip:"$_id"}}]) $gt:100000}}}])


Page 11


Page 12

View more...


0 thoughts on “M101n Homework 2.3”


Leave a Comment

Your email address will not be published. Required fields are marked *