취미와 밥줄사이

[Python] MongoDB Delete Document 본문

DB

[Python] MongoDB Delete Document

취미와 밥줄사이 2021. 11. 3. 15:03

Delete Document

  • 하나의 document를 삭제하려면 delete_one() 메서드를 사용합니다.
  • delete_one() 메서드의 첫 번째 매개변수는 삭제할 document를 정의하는 객체입니다.
  • query에서 두 개이상의 documents를 찾으면 첫 번째 항목만 삭제됩니다.
# Delete the document with the address "Mountain 21":

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb['custmers"]

myquery = { "address":"Mountain 21" }

mycol.delete_one(myquery)

 

Delete Many Documents

  • 하나 이상의 문서를 삭제하려면 delete_many() 메서드를 사용하십시오.
  • delete_many() 메서드의 첫 번째 매개변수는 삭제할 문서를 정의하는 쿼리 객체입니다.
# Delete all documents were the address start with the letter S:

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

myquery = {"address":{"$regex":"^S"} }

x = mycol.delete_many(myquery)

print(x.deleted_count, " docunments deleted.")

Delete All Documents in a Collection

collection의 모든 documents를 삭제하려면 빈 쿼리 객체를 delete_many() 메서드에 전달합니다.

# Delete all documents in the "customers" collection:

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

x = mycol.delete_many({})

print(x.deleted_count, " documents deleted.")

 

REFERENCE

https://www.w3schools.com/python/python_mongodb_delete.asp

 

Python MongoDB Delete Document

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

'DB' 카테고리의 다른 글

[Python] MongoDB Sort  (0) 2021.11.03
[SQLAlchemy] SQLAlchemy이란?  (0) 2021.11.01
[SQL] EXISTS 연산자  (0) 2021.11.01
[SQL] HAVING 절  (0) 2021.11.01
[SQL] GROUP BY 문  (0) 2021.11.01