Buscar

domingo, 23 de noviembre de 2014

MongoDB for developers 1/8. Introduction. Homeworks

Homework 1.1


Install MongoDB on your computer and run it on the standard port. Download the HW1-1 from the Download Handout link and uncompress it.
Use mongorestore to restore the dump into your running mongod. Do this by opening a terminal window (mac) or cmd window (windows) and navigating to the directory so that you are in the parent directory of the dump directory (if you used the default extraction method, it should be hw1/). Now type
 
mongorestore --host localhost --port 27017 --db m101 --drop "dump\m101"

or  

mongorestore dump
 

Note you will need to have your path setup correctly to find mongorestore.

Next, go into the Mongo shell, perform a findOne on the collection called hw1 in the database m101. That will return one document. Please provide the value corresponding to the "answer" key from the document returned.

42



Homework 1.2

Get Pymongo installed on your computer. To prove its installed, run the program:


import pymongo
import sys


# Copyright 2013, 10gen, Inc.
# Author: Andrew Erlichson


# connnecto to the db on standard port
connection = pymongo.MongoClient("mongodb://localhost")



db = connection.m101                 # attach to db
collection = db.funnynumbers         # specify the colllection


magic = 0

try:
    iter = collection.find()
    for item in iter:
        if ((item['value'] % 3) == 0):
            magic = magic + item['value']

except:
    # print ("Error trying to read collection:" + sys.exc_info()[0])
        print ("Error trying to read collection:")


print ("The answer to Homework One, Problem 2 is " + str(int(magic)))



python hw1-2.py
 
This program will print a numeric answer. Please put just the number into the space below. Note that you will need to get MongoDB installed and the homework dataset imported from the previous homework before attempting this problem.

1815



Homework 1.3

We are now going to test that you have bottle installed correctly and can run a bottle-based project. Run the hw1-3.py download as follows:


import pymongo
import bottle
import sys


# Copyright 2012, 10gen, Inc.
# Author: Andrew Erlichson


@bottle.get("/hw1/")
def get_hw1(n):

    # connnecto to the db on standard port
    connection = pymongo.MongoClient("mongodb://localhost")

    n = int(n)

    db = connection.m101                 # attach to db
    collection = db.funnynumbers         # specify the colllection


    magic = 0

    try:
        iter = collection.find({},limit=1, skip=n).sort('value', direction=1)
        for item in iter:
            return str(int(item['value'])) + "\n"
    except:
        print ("Error trying to read collection:")


bottle.debug(True)
bottle.run(host='localhost', port=8082)

 
python hw1-3.py

It requires bottle to be installed correctly, your mongodb to be running, and that you have run mongorestore properly. From a different terminal window type the following from the command line: curl http://localhost:8080/hw1/50
Alternatively, you can put the url above into your web browser.
Type the answer into the box below

53

No hay comentarios:

Publicar un comentario