Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop a collection with pymongo?

Tags:

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.

like image 511
Morton Avatar asked Feb 22 '18 09:02

Morton


People also ask

How do I drop a collection in Pymongo?

Delete Collection You can delete a table, or collection as it is called in MongoDB, by using the drop() method.

How do I delete a collection in MongoDB?

To delete a MongoDB Collection, use db. collection. drop() command.

How do I delete a record in Pymongo?

To delete one document, we use the delete_one() method. The first parameter of the delete_one() method is a query object defining which document to delete. Note: If the query finds more than one document, only the first occurrence is deleted.

How do I delete a file in MongoDB using Pymongo?

To delete documents from a collection of MangoDB, you can delete documents from a collections using the methods delete_one() and delete_many() methods. These methods accept a query object specifying the condition for deleting documents. The detele_one() method deletes a single document, in case of a match.


1 Answers

You need to use drop. Suppose foo is a collection

db.foo.drop() 

Or you can use drop_collection

db.drop_collection(collection_name) 
like image 64
Arpit Solanki Avatar answered Oct 03 '22 21:10

Arpit Solanki