【发布时间】:2019-05-25 17:08:13
【问题描述】:
我有以下:
from pymongo import MongoClient
client = MongoClient()
db=client.localhost
collection=db['accounts']
db.collection.remove({})
cursor = collection.find({})
for document in cursor:
print(document)
第二部分只是打印集合中的所有文档。但是,每次我重新运行程序时,该集合都不会被清除。有谁知道为什么?
【问题讨论】:
-
问题是您使用
db.collection(指向名为“collection”的集合)而不是collection变量(指向“accounts”),因此您正在清理错误的集合