top of page

Step by Step Guide to Connecting Python with MongoDB Using PyMongo for CRUD Operation

Step 1: Install Required Tools

  1. Install Python

  2. Install MongoDB (Local)

    OR use MongoDB Atlas (Cloud) (free cluster).

  3. Install VS Code

    • Download VS Code

    • Install the Python extension (by Microsoft).

  4. Install PyMongo Driver Open VS Code Terminal and run:

    pip install pymongo dnspython


Create Project in VS Code

  1. Create a new folder → mongodb_python_app.

    ree

  2. Open it in VS Code (File > Open Folder).


  3. Create a new file → app.py.


Step 3: Write Python Code

Paste this into app.py:

from pymongo import MongoClient
# 1. Connect to local MongoDB (make sure mongod is running)
client = MongoClient("mongodb://localhost:27017/")
# 2. Create or switch to database
db = client["school_db"]
# 3. Create or switch to collection
students = db["students"]
# 4. Insert a document
student1 = {"name": "Rohan", "age": 23, "course": "Computer Science"}
students.insert_one(student1)
# 5. Insert multiple documents
student_list = [
    {"name": "Raj", "age": 21, "course": "Mathematics"},
    {"name": "Priya", "age": 22, "course": "Physics"}
]
students.insert_many(student_list)
# 6. Fetch all documents
print("\nAll Students:")
for doc in students.find():
    print(doc)
# 7. Fetch with condition
print("\nStudents enrolled in Physics:")
for doc in students.find({"course": "Physics"}):
    print(doc)
# 8. Update a document
students.update_one({"name": "Sharon"}, {"$set": {"course": "Data Science"}})
print("\nAfter Update:")
for doc in students.find({"name": "Sharon"}):
    print(doc)
# 9. Delete a document
students.delete_one({"name": "Raj"})
print("\nAfter Deletion:")
for doc in students.find():
    print(doc)

Step 4: Run in VS Code

  1. Open terminal in VS Code (Ctrl + ~).

  2. Run:

    python app.py


Step 5: Verify in MongoDB Compass

  • Open MongoDB Compass.

  • Connect to the same URI you used in Python.

  • Browse school → students collection.

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

Recommended Products For This Post
 
 
 

Recent Posts

See All

Comments


© 2023 by newittrendzzz.com 

  • Facebook
  • Twitter
  • Instagram
bottom of page