$39
------- Part 1: Queries (40 points)
Download this file, and run it to create your Mongo collection:
** createGovtWorkers.py
Do not change anything in the data generation, including the random.seed(3) command. This will ensure you all are using the same dataset with 2,000 documents and the same values.
Write queries for the queries and partial answers in this file (I show the first part of the answer in each query.):
** queryGovtWorkers_partialAnswers
ONLY Q4, Q5. Q9. Q10, and Q11 are graded. The others are a logical progression to help you get the right answer for Q2, Q4, Q5, Q10, and Q11.
Note: The Midwest states (Q9) are:
{"state":"ND"},{"state":"SD"},{"state":"NE"},{"state":"KS"},{"state":"MN"},{"state":"IA"}, {"state":"MS"},{"state":"WI"},{"state":"IL"},{"state":"IN"},{"state":"MI"},{"state":"OH"}
A few hints:
• Do the queries in the order presented (Q1, then Q2, then Q3, etc.) because it helps you build upon simpler queries.
• Q5: You need to use $regex.
• Q8 and Q9: Use $match before the $group.
• Q10: Use $match after the $group (like the having clause in SQL group by).
• Q10: You will need to use $match before and after $group.
Note: You may run the queries in Mongo command line or using PyMongo. If using the command line, I strongly urge you to develop your queries in another file and then cut and paste them. Go ahead and copy and paste the answer from running them. If using PyMong, you need to put " " around field names and operators. For example, here is a PyMongo query (Notice the for loop after the query to print the results.):
print("\nComplete info about people whose weight is <= 41 Kilos")
count = 0
objs = peeps.find( {"weight": {"$lte":41}} )
for anObj in objs:
print(anObj)
count = count + 1
print(count)
------- Part 2: Updates/Deletes (10 points)
Write an example of an update for the collection you used in Part 1 that changes one document. Print the document before and after.
Write an example of an update for the collection you used in Part 1 that changes multiple documents. Print the documents before and after.
Write an example of a delete command for the collection you used in Part 1 that deletes more than one document. Print a subset of the documents before and after that shows the delete worked.
Here is an example .py file containing updates and deletes for another dataset created by this .py file:
File to create:
createEmps-1.py
File of updates and deletes:
pymongo_ex_update_delete.py
Example output from running this file on the data created by running the file createEmps-1.py:
tempUpdateDeleteOutput