Connecting MongoDB With Python

Database Image
  1. What is MongoDB
  2. Using MongoDB with Python using pymongo.
  1. Whenever we speak about Database (DB) there is one common DB that comes to mind — MySQL. This is a Relational Database. These basically use tables to describe the relationship between data.
  2. On the other hand, are NoSQL database which doesn't use a table for describing the relationship, rather they can use document-based, key-value pairs, etc. Moreover, unlike SQL, they have a dynamic schema, while SQL has a well-defined schema.
pip install pymongo
pip install dnspython
# Install driver for python to connect to MongoDB
import pymongo
from pymongo import MongoClient
client = MongoClient("Enter your connection string")
# Print Name of all Databases
print("\n The Name of all Databases are : ")
print(client.list_database_names())
# Connect to a Database
db = client.database_name
# Print Name of all Connections in the Database
print("\n The Name of all Connections in the Database are : ")
print(db.list_collection_names())
collection = db.collection_namequery ={
"tag1":True,
"tag2":{"$ne":True},
"tag3":"Adnan",
"tag4":{"$exists":True}
}
for document in collection.find(query):
print(document)
{“tag1”: True, “tag2”: False, “tag3”:” Adnan”, “tag4”: 5}

--

--

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