Python — MongoDB

What is NoSQL?

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


  • To install PyMongo, type the following:

1. Creating a Database

  • Create a database called my_database

2. Creating a Collection

3. Inserting Documents

  • 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

  • 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

  • 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.
  • 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
  • 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.
  • 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.
  • 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.





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

An Introduction to PyScript along with Code Examples

Upgrade your old Flutter projects to Flutter Version 2 with Null Safety.

Choose to Send an Email Now or at a Later Date using Laravel ,VueJS and CronJob (Task Scheduler)

My PhpStorm setup

How to use AWS Free Tier to deploy Magento2 for learning purpose.

Argos KYC API Ver 2.0 Released

How To Estimate Weather Forecasts with the OpenWeatherMap API in Python

Kafka Fundamentals

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


More from Medium

Getting Started with Python Language — 2

[OpenCV] Basics review notes (1) & PyCharm unboxing

where to install opencv pakage

Multiplayer Word Guessing Simple Python Game

Implementation of Django Signals