You will be producing a script that will define functions (named a(), b(), etc.) that will perform relevant tasks for the purpose of helping the company get information about which countries their customers reside in.
db = db.getSiblingDB("hw15")
Your script, when finished should define the following functions:
HINT: I have written this function below to introduce some notation.
_id
to match the id value in
the customer collection. The geocustomers collection will also have a
gender field and a name field that are copied from customers. Finally, add
a new field called “location” that is a GeoJSON object containing the
customer's location. To make this transformation happen you'll need to
rearrange the coordinate values into a new object and convert the coordinate
values from string to double.
HINT: I used an aggregate pipeline with $project
and $out
stages
to accomplish this.
HINT: I used the .forEach() method to iterate over each country and
did a $geoWithin
query to get the count of people within that
country's border. Then I used an update query to add the new field.
HINT: I used an aggregate pipeline because I wanted more control over the format of my output document (but you can also use find() if you want to). When calling .aggregate in a function the output is squelched, so I used this to get the output I wanted:
let result = db.countries.aggregate([ .... ]) result.forEach(doc => printjson(doc))
HINT: Remember that if you did b() correctly then entries in both
collections share the same _id
value. So, when you learn that a
customer in geocustomers is not found in any country, you can use the id
to delete from both collections. It took about 20 minutes for this function
to run to completion when I had it working. I put an output statement in
my loop to display the id of each customer I was deleting to be able to
see/track progress. At one point the script stopped making progress
(probably a network issue). So, I restarted the function and it finished
fine. Also, I used $geoIntersects
rather than $geoWithin
for this particular query.
load("hw15.js")
Then you can test a function by simply running it from the shell: a()
My solution to a() was this:
function a() { db = db.getSiblingDB("hw15") // switch to hw15 DB db.customers.drop() db.countries.drop() db.geocustomers.drop() }