<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 ISQA 8080 Homework 2.2
Brian Detweiler
Step1:  Tasks
The fourth task is to upload a dataset and to write and run different queries on this dataset.
The fifth task starts with Redis. First, installation and running Redis either on your own computer or running it on
The sixth task demonstrates three examples of using Redis.
The seventh tasks asks you to describe a solution to use Redis for storing user ratings.
The eighth tasks asks you to write a reflection on the discussion boards.
Deliverables (Summary, Detailed description in the tasks)
Step2:  1. Display all occupations
Step3:  2. Chose an occupation and select all users with this occupation. Only show user information and hide the users' movie ratings.
Step4:  3. Count the number of men in the database.
Step5:  4. Select all users whose zipcode starts with a 5. Only show the user ID and zipcode.
Step6:  5. Select all movies from the year 1998 and category comedy.
Step7:  6. Count the number of movies from the year 1990 and 1995.
Step8:  7. Display all movies published before the year 1992.
Step9:  8. Imagine that you registered for MovieLens. Create a new user with your user data. Do not include any ratings.
Note
Step10:  9. Update the user record you created in the previous query and insert a new rating for a movie of your choice. You will need to use $addToSet (https
Step11:  10. A query of your choice.
Here, we'll find users near Omaha
<ASSISTANT_TASK:>
Python Code:
import pymongo
from pymongo import MongoClient
import datetime
import re
from pymongo import InsertOne, DeleteOne, ReplaceOne
import datetime
client = MongoClient()
client = MongoClient('mongodb://localhost:27017/')
db = client.homework2
users = db.users
movies = db.movies
Explanation: ISQA 8080 Homework 2.2
Brian Detweiler
End of explanation
movieList = movies.find({"_id": 1})
for movie in movieList:
    print movie
Explanation: Tasks
The fourth task is to upload a dataset and to write and run different queries on this dataset.
The fifth task starts with Redis. First, installation and running Redis either on your own computer or running it on
The sixth task demonstrates three examples of using Redis.
The seventh tasks asks you to describe a solution to use Redis for storing user ratings.
The eighth tasks asks you to write a reflection on the discussion boards.
Deliverables (Summary, Detailed description in the tasks):
See task four: In the answer sheet "Homework 2 Answer Sheet.docx", write the 10 queries including a screenshot of the command and the first lines of the result.
See task seven: In the answer sheet "Homework 2 Answer Sheet. Docx", describe how Redis can be used to store user ratings. You need to include screenshots to show how data would be stored as well as retrieved in Redis.
Submit the Answer Sheet on Blackboard using the link "Homework 2 Part 2: MongoDB and Redis" (where you downloaded the instructions and the text files).
See task eight: Write a reflection by replying to the thread "Homework Reflection 2" in the discussion board
End of explanation
occupations = sorted(users.distinct(u'occupation'))
for occupation in occupations:
    print occupation
Explanation: 1. Display all occupations
End of explanation
# We will choose 'technician/engineer' and hide movie reviews - limit to first 10 results
engineers = users.find({u'occupation': 'technician/engineer'},
                      {u'movies':0}).limit(10)
for engineer in engineers:
    print engineer
Explanation: 2. Chose an occupation and select all users with this occupation. Only show user information and hide the users' movie ratings.
End of explanation
men = users.find({u'gender': 'M'}).count()
print "There are " + str(men) + " men in the database."
Explanation: 3. Count the number of men in the database.
End of explanation
regex = re.compile("^5.*")
by_zipcodes = users.find({u'zipcode': regex},
                      {u'_id':1, u'zipcode': 1}).limit(10)
for zipcode in by_zipcodes:
    print zipcode
Explanation: 4. Select all users whose zipcode starts with a 5. Only show the user ID and zipcode.
End of explanation
comedies_from_1998 = movies.find({u'year': 1998, u'category': u'Comedy'}).limit(10)
for comedy in comedies_from_1998:
    print comedy
Explanation: 5. Select all movies from the year 1998 and category comedy.
End of explanation
movies_between_1990_and_1995 = movies.find({u'year': {'$gte': 1990, '$lte': 1995}}).count()
print 'There are ' + str(movies_between_1990_and_1995) + ' movies between 1990 and 1995'
Explanation: 6. Count the number of movies from the year 1990 and 1995.
End of explanation
movies_before_1992 = movies.find({u'year': {'$lt': 1992}})\
                           .limit(10)\
                           .sort('year', pymongo.ASCENDING)
for movie in movies_before_1992:
    print movie
Explanation: 7. Display all movies published before the year 1992.
End of explanation
# users.delete_one({u'zipcode': u'68102'})
me = users.find_one({u'zipcode': u'68102'})
if me is not None:
    print me
else:
    max_id = users.find_one({u'zipcode': u'68102'})
    myself = {u'gender': u'M', 
              u'age': u'25-34', 
              u'zipcode': u'68102', 
              u'occupation': u'technician/engineer'}
    result = db.users.insert_one(myself)
    my_id = result.inserted_id
    print 'created my profile with id ' + str(my_id)
Explanation: 8. Imagine that you registered for MovieLens. Create a new user with your user data. Do not include any ratings.
Note: The _id here is auto-generated, as per MongoDB conventions
I have seen ways to grab the max ID and increment it by one, but this is not safe in general, so I am not implementing it here.
End of explanation
movie_review = {u'moveID': 3272, # Bad Lieutenant 
                u'rating': 5, 
                u'timestamp': datetime.datetime.utcnow()}
users.update_one(
  { u'zipcode': u'68102'}, 
  { "$addToSet":{"movies": movie_review} }, 
  upsert=True)
me = users.find_one({u'zipcode': u'68102'})
print me
Explanation: 9. Update the user record you created in the previous query and insert a new rating for a movie of your choice. You will need to use $addToSet (https://docs.mongodb.com/v3.0/reference/operator/update/addToSet/) to add a value that does not exist in an array to the end of the array. You can use Math.round(new Date().getTime()/1000) to calculate the time in Unix format (seconds since start from epoch).
End of explanation
regex = re.compile("^681.*")
by_zipcodes = users.find({u'zipcode': regex},
                      {u'_id':1, u'zipcode': 1}).limit(10)
for zipcode in by_zipcodes:
    print zipcode
Explanation: 10. A query of your choice.
Here, we'll find users near Omaha
End of explanation
<END_TASK>