How unset a filed
i am using mongo frames, but not sure how to unset existing field?
@mohmagdy Hey. So there isn't a way directly through the library - it's something I thought about adding though so now that some else has raised it I'll take a look at implementing this in the next release.
For now I simply make the change at the collection level, so:
Employee.get_collection().update_one(
(Q._id == employee._id).to_dict(),
{'$unset': {'company': ''}}
)
I realise this isn't ideal but the way I work most of the time I rarely use unset so this hasn't been a problem. I guess it might be nice to be able to do something like in the future:
employee.company = UNSET
employee.update()
Hope that's helpful and would appreciate any feedback you have on a nice interface for unsetting values.
Ant
I'm in the habit of using the Q object to generate queries, this is on of the few cases where actually it's shorter to use the raw query (so in preference to my above example):
Employee.get_collection().update_one(
{'_id': employee._id},
{'$unset': {'company': ''}}
)
i tried it and it's not working i want to remove the field when the value to update it is empty string but it remain in the document with empty string value
if you set the value to an empty string you'll need to also remove it from the internal document, so:
employee._document.pop('company')
Employee.get_collection().update_one(
{'_id': employee._id},
{'$unset': {'company': ''}}
)
The unset and unset_many methods have been in place now since 2020 so I'm closing this issue.