A command line implementation of an application to query a MongoDB database.
I deployed a Free Tier Cluster, populated a database with The UK election data (add description) and installed PyMongo to communicate with the database from Python.
The application is developed in a functional programming style with each query representing a function.
Queries are written as MongoDB aggregation pipelines.
- I do not print the queries as a long string, but use f-strings with function docstrings.
- I manage the user input using the EAFP principle (Easier to ask for forgiveness than permission) with
try except
blocks instead ofif else
. - Notice how I pass the user input to functions in a uniform way and how I deal with functions requiring additional parameters.
- Notice how I deal with functions' return values in a uniform way: either iterating the cursor or printing a single row.
PyMongo
should be installed.
When you start the application, you will see a list of 6 possible queries to run and a prompt to choose a query:
Enter a query number (see above): .
Enter a single number and hit Enter.
N.B. You will see the same question again if the input is not valid (not a number or beyond the range of available queries).
For some queries you might be asked to enter additional parameters.
For example:
This query requires you to enter a party: .
This query requires you to enter a constituency area: .
Enter a single word and hit Enter.
N.B. For such queries, it is assumed you know short names for constituencies or parties ('con', 'lab', 'eco' etc) as they are presented in a db.
If the input does not match records, you will see a message No records found...
If everything is correct, you will see the result of your query on the command line.