删除文档

Python 中的 MongoDB 入门

Filip Schouwenaars

Machine Learning Researcher

删除单个文档

# 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
Python 中的 MongoDB 入门

一次删除多个文档

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

# How many documents deleted? res.deleted_count
4
  • 删除是永久的,无法撤销!
  • 删除前请再次确认过滤条件!
Python 中的 MongoDB 入门

开始练习!

Python 中的 MongoDB 入门

Preparing Video For Download...