What is PyMongo?

The PyMongo library allows interaction with the MongoDB database through Python; it is a native Python driver for MongoDB.

PyMongo supports the following MongoDB versions:

  • 2.62.6
  • 3.03.0, 3.23.2, 3.43.4, 3.63.6
  • 4.04.0 and 4.24.2.
svg viewer

Installation

PyMongo can be installed using pip:

pip install pymongo

If you’re using pip with Python3, run:

pip3 install pymongo

Example

Have a look at a basic example below on how to get started with PyMongo:

import pymongo
# 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 you want to create.
client = pymongo.MongoClient("mongodb://localhost:27017/")
# Create a database called "MyDB"
mydb = myclient["MyDB"]

In MongoDB, a database is not created until it gets content.

MongoDB waits until you have created a collection with at least one record before it creates the database.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved