[英]How to drop a collection with pymongo?
我使用scarpy
來抓取數據並使用MongoDB將其保存到雲托管mLab
。
我想再次抓取數據並recently
更新我的收藏,所以我嘗試刪除該集合然后插入。
這是我的代碼pipelines.py:
from pymongo import MongoClient
from scrapy.conf import settings
class MongoDBPipeline(object):
def __init__(self):
connection = MongoClient(
settings['MONGODB_SERVER'],
settings['MONGODB_PORT'])
db = connection[settings['MONGODB_DB']]
# here is my collection name recently setting
self.collection = db[settings['MONGODB_COLLECTION']]
def process_item(self, item, spider):
# try to drop my collection recently
self.collection.drop()
self.collection.insert(dict(item))
return item
但是當我運行我的蜘蛛時,我看到我recently
收藏數是10(應該是5,這就是我想要的)
我正在尋找一些如何刪除集合的代碼。 它只是說db。[集合名稱] .drop()
但是當我在self.collection.insert(dict(item))
之前嘗試self.collection.drop()
時,我的工作沒有用。
任何人都可以給我一些建議我的代碼有什么問題?
那將是值得贊賞的。 提前致謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.