We've got a database with historical stock prices from Starbucks (SBUX), Apple (AAPL), Tesla (TSLA) and Microsoft (MSFT). The data is stored in a MongoDB where each entry/document is in the following format:
{
"symbol" : "SBUX",
"month" : "Feb",
"year" : 2023,
"price" : 102.09
}
Your challenge will be to query the MongoDB to produce a single result or multiple results according to the descriptions below.
Note: if you're running this from Replit, you can skip this section.
We're going to continue working in your Cloud Shell environment, so clone this repository into your environment as you've done before.
You may need to install PyMongo and dnspython in the Cloud Shell IDE to help connect to the MongoDB database:
sudo pip3 install pymongo
sudo pip3 install dnspython
You can also echo these installations to the startup script if you want to install them permanently - ask an Upperline Code team member for support on this if you want to do it.
Be aware that you'll need to start by specifying the name of the database you want to connect to. This database for this lab is called 'stock-prices'
.
To run the stocks.py
file, type python3 stocks.py
in the Terminal in your IDE. Anything you print()
will be shown in the Terminal, too. Bear in mind that while our intro to MongoDB included being able to SEE the entries on the MongoDB dashboard GUI (graphical user interface), this lab will be done entirely with queries, so you'll ONLY be able to see the content you print to the terminal.
Note: if you're running this from Replit, you can go through these steps in the Shell tab, or you can simply press the Run button moving forward.
You'll notice we've given you a bit help on this one to start out.
Any query that may return multiple documents is returned as a
Cursor
, and this is a data structure we haven't seen before. To convert theCursor
into something you're more familiar with, we can wrap the result in thelist()
method, in a similar way to how we useddict()
before:data = list(collection.find({...}))You can also iterate over the
Cursor
using afor
loop:for entry in data: print(entry)So we've given you the search query solution. What's missing?
You can, of course use standard
len()
functionality to count items in a list, but if you want to try something a bit more MongoDB specific, here's a bit of help:
- MongoDB .count() Method - It's worth knowing that MongoDB documentation is written in JavaScript, and we're writing in Python, so you can also cross-reference these with the PyMongo Documentation for the same .count() method.
While balancing TWO different pieces of documentation is difficult, we've found that the official documentation (written in JavaScript) is a lot easier to understand, so we usually recommend reading there first to see WHAT you want to do, and then checking the PyMongo documentation (written in Python) to make sure that the syntax is the same (it often is), or adjust the syntax a little if it happens to be different.
Here's a bit of help:
This is a situation where the official (JavaScript) documentation doesn't work exactly the same in Python, but the fix is actually super easy. While JavaScript objects can take symbols like
$gt
as keys in key-value pairs, Python is going to need a string version of those same terms, for example"$gt"
instead of just$gt
.This is an example of a situation where the PyMongo documentation doesn't even have a comparable page, because they expect you to look at the JavaScript documentation and infer that it's probably the same. They do, however, include an example of how to use one operator in the tutorial, so if you're feeling stuck, looking at the JavaScript example, and want to see a Python example, that's a good place to start.