I don't have enough reputation to comment and hence I have to ask this question again.
I have tried different ways to delete my dynamically changing date column as mentioned here but nothing worked for me : How to remove a field completely from a MongoDB document?
Environment_Details - OS : Windows10, pymongo : 3.10.1, MongoDB Compass App : 4.4, python: 3.6
I am trying to delete column "2020/08/24"(this date will be dynamic in my case). My data looks like this:
[{
"_id": {
"$oid": "5f4e4dda1031d5b55a3adc70"
},
"Site": "ABCD",
"2020/08/24": "1",
"2020/08/25": "1.0"
},{
"_id": {
"$oid": "5f4e4dda1031d5b55a3adc71"
},
"Site": "EFGH",
"2020/08/24": "1",
"2020/08/25": "0.0"
}]
Commands which don't throw me any error but also don't delete the column/field "2020/08/24":
col_name = "2020/08/24"
db.collection.update_many({}, {"$unset": {f"{col_name}":1}})
db.collection.update({}, {"$unset": {f"{col_name}":1}}, False, True)
db.collection.update_many({}, query =[{ '$unset': [col_name] }])
I am always running into error while trying to use multi:True with update option.
The exact code that I am using is:
import pymongo
def connect_mongo(host, port, db):
conn = pymongo.MongoClient(host, port)
return conn[db]
def close_mongo(host, port):
client = pymongo.MongoClient(host, port)
client.close()
def delete_mongo_field(db, collection, col_name, host, port):
"""Delete column/field from a collection"""
db = connect_mongo(host, port, db)
db.collection.update_many({}, {"$unset": {f"{col_name}":1}})
#db.collection.update_many({}, {'$unset': {f'{col_name}':''}})
close_mongo(host,port)
col_to_delete = "2020/08/30"
delete_mongo_field(mydb, mycollection, col_to_delete, 'localhost', 27017)