You are on page 1of 1

CMP6221: Computing for AI

Lab Session 7: Introduction to MongoDB and Python

1. Objectives
a. Integrate MongoDB with your Python applications

Note: Although most of the instructions in this sheet are written for command line interpreter,
you are advised to save your python codes as scripts.

2. MongoDB
MongoDB stores data in JSON-like documents, which makes the database very flexible
and scalable.
To be able to experiment with the code examples, you will need access to a MongoDB
database.
You can download a free MongoDB database installer at https://www.mongodb.com

3. Pymongo
Python needs a MongoDB driver to access the MongoDB database.

If you are using Anaconda:


• Start Anaconda prompt

In Anaconda Prompt type the following command:


1. python -m pip install pymongo
2. conda install -c anaconda pymongo

4. Exercise

This exercise 8 from Lab 6. You have to do the same application using MongoDB to
store and retrieve the persons and their birthdays.

For this exercise, we will keep track of when our friend’s birthdays are, and be able to find
that information based on their name. Create a collection (in MongoDB) of persons (names
and birthdays). When you run your program it should ask the user to enter a name, and
return the birthday of that person back to them. The interaction should look something like
this:

>>> Welcome to the birthday dictionary. We know the birthdays of:


Albert Einstein
Benjamin Franklin
Ada Lovelace
>>> Who's birthday do you want to look up?
Benjamin Franklin
>>> Benjamin Franklin's birthday is 01/17/1706.

You might also like