Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

271
Vistas
How to drop a collection with pymongo?

I use scarpy to crawl data and save it to cloud hosting mLab successfully with MongoDB.

My collection name is recently and data's count is 5. enter image description here

I want to crawl data again and update my collection recently, so i try to drop the collection and then insert.

Here is my code 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

But when I run my spider, I see my collection recently count is 10 (It should be 5 that is what I want) enter image description here

I looking for some code that how to drop collection. It's just say db.[collection Name].drop()

But its no working in my case when i try self.collection.drop() before self.collection.insert(dict(item))

Anyone can give me some suggestions what is wrong with my code ?

That would be appreciated. Thanks in advance.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to use drop. Suppose foo is a collection

db.foo.drop()

Or you can use drop_collection

db.drop_collection(collection_name)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda