簡體   English   中英

如何使用pymongo刪除一個集合?

[英]How to drop a collection with pymongo?

我使用scarpy來抓取數據並使用MongoDB將其保存到雲托管mLab

我的收藏名稱是recently ,數據的數量是5。 在此輸入圖像描述

我想再次抓取數據並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()時,我的工作沒有用。

任何人都可以給我一些建議我的代碼有什么問題?

那將是值得贊賞的。 提前致謝。

你需要使用drop 假設foo是一個集合

db.foo.drop()

或者你可以使用drop_collection

db.drop_collection(collection_name)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM