Querying a MongoDB Database
1. Querying a MongoDB Database
Welcome back! Now that we've connected to the server, it's time to explore what's inside: the databases, the collections, and the data we'll be working with.2. Databases and collections
A MongoDB server holds multiple databases, that each hold related collections. In this course, we use one called film, which has a single collection: movies. Each collection contains documents with a flexible schema, meaning they can each have a different structure. This offers more flexibility than rows in traditional databases.3. Databases and collections in Python
To verify this in Python, start by creating a MongoClient like before. Calling .list_database_names() shows that we indeed have a database film, alongside some databases that MongoDB uses internally. Then, client.film.list_collection_names() shows its collections — in this case, just one: movies.4. Getting all documents
That's all great, but we haven't seen any actual data! For starters, let's retrieve all documents in the collection. You can do this with client.film.movies.find(), like this. That's a lot of dots, so let's also store the collection instance like this, and then call find on this variable.5. From cursor to Python list
When you run find(), you may expect a Python list with all documents, but that's not the case. Instead, find() returns a so-called cursor object. A cursor is like a pointer to the results of your query. With this cursor, you can fetch the result of your query, one document at a time. Especially if you're dealing with massive collections, being able to control how you fetch the data is pretty useful. The simplest way to force the actual data fetching to happen is by wrapping the cursor in a list() call. We now get an actual Python list of dictionaries representing the documents.6. Let's have a look
We finally see some documents in Python! Notice how each document has an underscore id field that uniquely identifies it. We also see other fields, with field values in a variety of types: the title field is a string, the genre is an array, and won_oscar is a boolean, but it's not always there! We used find() to get all documents here, but watch out with this! In a production database, your collections may be huge, and fetching all documents may mean multiple gigabytes of data. This can lead to excessive memory usage and slow performance. Let's cover two ways to avoid this.7. Query filters
Let's call `find()` again, but this time with a so-called query filter: a dictionary with key won_oscar and value true. This will return only documents that have the won_oscar field set to true, drastically limiting how many documents we get back.8. Target specific records with .find_one()
Next, instead of using find(), we can use find_one(). This method gets only the first document that matches the query filter. For example, to find a movie titled "parasite" you do mov.find_one(), and specify a filter with the key title and the value parasite. This time we immediately get the document, not a Cursor like we did with find().9. Let's practice!
As you can see, querying records with MongoDB is pretty quick, and you don't need to learn SQL to do it. Head over to the exercises to get a feel for it yourself.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.