Introduction
If you're working with data stored in MongoDB, you'll probably need to access the database from Python, Luckily the MongoDB client library makes this task a simple one, allowing you to connect to MOngoDB with just a few line of code. IN this step-by-step tutorial, we'll explain how to connect Python to MongoDB using pymongo client library.
Install the pymongo library
# Python3 intstallation of Pymongo
pip3 install pymongo
# Python2 intstallation of Pymongo
pip install pymongo
Connect ot MongoDB Server
Next, we'll connect to the MongoDB server. ON macOS and Linux-based terminals, you can use the ps commeand to grep fro the MOngoDB server.
ps | grep mongo
On most Linux servers, you can use the service command to start and stop the mongod server
sudo service mongod status
On macOS, you can use the list command to confirm that MOngoDb was installed using Homebrew
brew services list
After you've confirmed that the MongoDb server is running, you cna use the mongo command to enter the mongo shell
mongo
Importing module
Create a file filename.py
import pymogo
# ... or
from pymongo import MongoClient
check pymongo install or not.
python3 fileaname.py
or
python filename.py