How to interact with MongoDB with Python


Users need a programming language to interact with mongoDB.

Python is a most widely used programming language, has an official driver called Pymongo using which a user can interact with MongoDB easily.

Using PyMongo users can perform various CRUD operations.

1. Create 2. Read 3. Update 4. Delete

PyMongo can be installed using pip.

pip install pymongo

By using the below 2 lines a user can easily connect to mongoDB

import pymongoclient = pymongo.MongoClient("mongodb://localhost:27017/")

A user can also pass a username and password while connecting to mongoDB.

import pymongoclient = pymongo.MongoClient("mongodb://[username:password@]localhost:2701

Getting a Database A single instance of MongoDB can support multiple independent databases.

db = client.test_database

Getting a Collection A collection is a group of documents stored in MongoDB, and can be thought of as roughly the equivalent of a table in a relational database.

collection = db.test_collection

Thank You