简体   繁体   中英

how to drop data in mongo db using python

In my mongodb I have around 20K records, now From the same mongodb I am importing only 500 records in python and now I need to delete those 500 records which I have imported , is it possible to delete those 500 records using python?

my code for importing 500 records

from pymongo import MongoClient
con = MongoClient(local host, 27017)
db = con.rssfeeds_db
data = pd.DataFrame(list(db.restdata.find().limit(500)))

You would need to use the _id for each record so that you could tell pymongo which records to delete. For example:

from pymongo import MongoClient
con = MongoClient(localhost, 27017)
db = con.rssfeeds_db

// create a list of the records. each one will be a dict.
records = [x for x in db.restdata.find().limit(500)]

// create a list of the 500 ids so we only need to call the db once
record_ids = [record['_id'] for record in records]

//create your dataframe
data = pd.DataFrame(records)

// delete all 500 records at once
db.restdata.delete_many(
    {'_id': 
        {'$in': record_ids}
    },
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM