Student Name Tushar bonde
URN NO 2020-B-28072001
Course Name NoSQL Database
Specialization BCA-Data Science Sem-V
Academic Year 2022-2023
MOOC TITLE: connecting to mongoDB in python
Link: https://www.mongodb.com/languages/python
What is Python?
Python, the Swiss Army knife of today’s dynamically typed languages, has
comprehensive support for common data manipulation and processing
tasks. Python’s native dictionary and list data types make it second only
to JavaScript for manipulating JSON documents — and well-suited to
working with BSON. PyMongo, the standard MongoDB driver library for
Python, is easy to use and offers an intuitive API for accessing databases,
collections, and documents.
Objects retrieved from MongoDB through PyMongo are compatible with
dictionaries and lists, so we can easily manipulate, iterate, and print them.
How MongoDB stores data
MongoDB stores data in JSON-like documents
:
Connecting Python and MongoDB Atlas
PyMongo has a set of packages for Python MongoDB interaction. For
the following tutorial, start by creating a virtual environment, and
activate it.
Creating a MongoDB database in Python
The first step to connect Python to Atlas is to create a cluster. You can
follow the instructions from the documentation to learn how to create and
set up your cluster.
Next, create a file named pymongo_get_database.py in any folder to write
PyMongo code. You can use any simple text editor, like Visual Studio Code.
Create the mongodb client by adding the following:
o create a Mongo Client, you will need a connection string to your
database. If you are using Atlas, you can follow the steps from the
documentation to get that connection string. Use the connection string
to create the mongo client and get the MongoDB database connection.
Change the username, password, and cluster name.
In this python mongodb tutorial, we will create a shopping list and add a
few items. For this, we created a database user_shopping_list.
MongoDB doesn’t create a database until you have collections and
documents in it. So,
let’s create a collection next.