Previously we’ve talked about SQL databases and how to access them using various methods of querying. But there are also databases that forgo SQL in favor of other way of organizing data. These are called NoSQL databases and there are many flavors, but today we’re going to talk about one called MongoDB. MongoDB organizes its data in format closer to json objects instead of the traditional row/column structure. Once extracted from the database it is easy to convert it back into a standard table, but the versatility of json allows for more options when store other kinds of data. Python has a package that streamlines the process of connecting to and interacting with MongoDB, called PyMongo. First lets look at how to use PyMongo to connect to a database.
First, of course, you’ll need to install PyMongo, but once you have it the connection is simple enough, just one line:
from pymongo import MongoClient
client = MongoClient([connection url/uri])
It’s difficult to get more specific about the parameters, because there are several formats the url can be in. You can use a host/port combination, which would appear like this:
client = MongoClient(‘localhost’,’5555’)
Assuming your database is running on the default local host, and ‘5555’ is whatever port it’s using. You can also use MongoDB’s URI format:
client = MongoClient(‘mongodb://localhost:5555/')
To achieve the same thing. A quick note, if your database is running on the default host and port you can just call:
client = MongoClient()
Since no parameters will run it with the defaults. If you are hosting your database with MongoDB Atlas your URL will be slightly more complicated. After creating your database you can use the ‘connect’ button to get a connection string, which will have a space for your password. It is highly recommended that you store your password in a separate file for security purposes, and only import it for use in the connection string.
Once you are connected, you can access your database as an attribute of the client:
db = client.myStore
Alternatively, if your database name is formatted such that it can’t work in this style, you can also access it using dictionary style:
db = client[‘my-Store’]
It is important to remember that if you call a database or collection that does not exist it will be created, so be careful not to re-create or overwrite your information. A collection is roughly MongoDB’s equivalent to a table in a normal database. Collections are created and called in much the same way as databases:
customers = db.customers
But in MongoDB databases and collections are created ‘lazily’, which means that nothing has actually been saved to the server yet, and no databases or collections will be created on the server until something is actually stored in them. So let’s look at how to do that. But what is an entry going to look like in MongoDB? Here’s an example of what an entry for our customers collection might look like:
new_customer = {“first_name” : “Eliot”,
“last_name” : “Spencer”
“phone” : “555-1234”,
“email” : “espencer@lev.org”
“address” : “1918 55th St., Boston, MA”}
As you can see this entry or document is basically a json object, so now we need to insert it into our collection. There are several ways to go about this, depending on how many documents we want to insert at one time. Right now we only have one document, so we’re going to use .insert_one()
, pretty simple, right?
customers.insert_one(new_customer)
.insert_one()
actually returns an instance of InsertOneResult
, which can be used to immediately obtain the id number of the newly inserted document:
c_id = customers.insert_one(new_customer).inserted_id
If an inserted document does not already contain the ‘_id’ field, it will be assigned an ‘_id’ upon being inserted into a collection, and the new ‘_id’ will contain a unique value within the collection. We can also insert many documents as once, with `.insert_many()’. All we have to do is put our documents in a list like so:
Python
customer_list = [{…},
{…},
{…}]
And then insert the list using `.insert_many()’:
customers.insert_many(customer_list)
Now that we have entries in our collection, we need to be able to retrieve them. Similarly, we can use .find_one()
. There are two ways to use this, this first is without any parameters, which will return the first entry in the collection, and the second is to use a query based on a key-value pair:
# Without parameters returns the first entry:
customers.find_one()
# Query based on matching key-value
customers.find_one({“first_name” : “Alec”})
You can also find object by id simply by querying based on {‘_id’ : ‘…’}
but there is a catch. Although an ObjectId can be represented as a string, it is not the same as a string. If you have the string version of an id it is necessary to convert it to it’s unicode representation with ObjectId(some_id)
.
You can also query for more than one document at a time, using .find()
. .find()
returns a cursor object which can iterate over all the results. Without parameters .find()
will simply return all items in the collection, but you can also supply a query to limit the results. Here we’ll query for customers with the last name Ford:
customers.find({“last_name” : “Ford”})
There are many other operations you can do with a MongoDB, but this should get you started. MongoDB may not be appropriate for all databases, but its alternate approach to database management can provide some exciting possibilities for the right project.
Top comments (0)