Deleting documents

Introduction to MongoDB in Python

Filip Schouwenaars

Machine Learning Researcher

Deleting a single document

# Setup
from pymongo import MongoClient
client = MongoClient()
mov = client.film.movies

# Delete document that matches filter, similar to find_one() res = mov.delete_one({ "title": "gladiator" })
# How many documents were deleted? res.deleted_count
1
Introduction to MongoDB in Python

Removing multiple documents at the same time

# Delete all really bad movies
res = mov.delete_many({ "rating": { "$lt": 5.0 } })

# How many documents deleted? res.deleted_count
4
  • Deleting documents is permanent and cannot be undone!
  • Double check your filters before deleting!
Introduction to MongoDB in Python

Let's practice!

Introduction to MongoDB in Python

Preparing Video For Download...