Python — MongoDB
Mongodb is a document-oriented NoSQL database used for high volume data storage. In this blog I have shared how to create database connection with MongoDB using python.
One can download a free MongoDB database at https://www.mongodb.com.
What is NoSQL?
NoSQL is not a relational database. It provides more flexibility since all records are not restricted by the same column names and types defined across the entire table.
What is MongoDB ?
- MongoDB is a document database. Each database contains collections which in turn contains documents. Each document can be different with varying number of fields. The size and content of each document can be different from each other.
- The document structure is more in line with how developers construct their classes and objects in their respective programming languages. Developers will often say that their classes are not rows and columns but have a clear structure with key-value pairs.
- As seen in the introduction with NoSQL databases, the rows (or documents as called in MongoDB) doesn’t need to have a schema defined beforehand. Instead, the fields can be created on the fly.
- The data model available within MongoDB allows you to represent hierarchical relationships, to store arrays, and other more complex structures more easily.
- Scalability — The MongoDB environments are very scalable. Companies across the world have defined clusters with some of them running 100+ nodes with around millions of documents within the database.
Why to Use MongoDB?
- MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time
- The document model maps to the objects in your application code, making data easy to work with
- Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data
- MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use
After installing MongoDB, next step is to install PyMongo, because Python needs a MongoDB driver to access the MongoDB database.
- To install PyMongo, type the following:
1. Creating a Database
To create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct IP address and the name of the database we want to create.
MongoDB will create the database if it does not exist, and make a connection to it.
- Create a database called my_database
In MongoDB, a database is not created until it gets content. MongoDB waits until we have created a collection (table), with at least one document (record) before it actually creates the database (and collection).
2. Creating a Collection
A collection in MongoDB is the same as a table in SQL databases. To create a collection in MongoDB, use database object and specify the name of the collection we want to create. MongoDB will create the collection if it does not exist.
Create a collection called “customers”:
MongoDB waits until you have inserted a document before it actually creates the collection.
3. Inserting Documents
In MongoDB terminology, a collection is a group of documents that are stored together within the database. Collections and documents are akin to SQL tables and rows, respectively. Retrieving a collection is as easy as getting a database.
- To insert a record, or document as it is called in MongoDB, into a collection, we use the insert_one() method.
- The first parameter of the insert_one() method is a dictionary containing the name(s) and value(s) of each field in the document we want to insert.
Return the _id Field
The insert_one() method returns a InsertOneResult object, which has a property, inserted_id, that holds the id of the inserted document.
- If we do not specify an _id field, then MongoDB will add one for us and assign a unique id for each document.
- In the example above no _id field was specified, so MongoDB assigned a unique _id for the record (document).
Insert Multiple Documents
- To insert multiple documents into a collection in MongoDB, we use the insert_many() method.
- The first parameter of the insert_many() method is a list containing dictionaries with the data we want to insert:
- The insert_many() method returns a InsertManyResult object, which has a property, inserted_id, that holds the ids of the inserted documents.
- If we do not want MongoDB to assign unique ids for you document, we can specify the _id field when we insert the document(s).
- Remember that the values has to be unique. Two documents cannot have the same _id.
4. Retrieving Documents
Let’s see some of the methods to perform operations on the MongoDB:
i) find() method:
- find_one() method select data from a collection in MongoDB. It returns the first occurrence in the selection.
- The find() method returns all occurrences in the selection.
ii) sort() method:
- sort()to sort the result in ascending or descending order.
- The sort() method takes one parameter for "fieldname" and one parameter for "direction" (ascending is the default direction).
- Use the value -1 as the second parameter to sort descending.
- sort(“name”, 1) #ascending
sort(“name”, -1) #descending
iii) Delete the documents:
- To delete one document, we use the delete_one() method. The first parameter of the delete_one() method is a query object defining which document to delete. If the query finds more than one document, only the first occurrence is deleted.
- To delete more than one document, use the delete_many() method. The first parameter of the delete_many() method is a query object defining which documents to delete.
- To delete all documents in a collection, pass an empty query object to the delete_many() method.
iv) Delete Collection:
We can delete a table, or collection as it is called in MongoDB, by using the drop() method.
v) Update Collection:
- We can update a record, or document as it is called in MongoDB, by using the update_one()method.
- The first parameter of the update_one() method is a query object defining which document to update.
- If the query finds more than one record, only the first occurrence is updated.
- To update all documents that meets the criteria of the query, use the update_many() method.
vi) Limit the Result
- To limit the result in MongoDB, we use the limit() method.
- The limit() method takes one parameter, a number defining how many documents to return.