MongoFrames icon indicating copy to clipboard operation
MongoFrames copied to clipboard

How unset a filed

Open mohmagdy opened this issue 7 years ago • 4 comments

i am using mongo frames, but not sure how to unset existing field?

mohmagdy avatar Feb 17 '19 09:02 mohmagdy

@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

anthonyjb avatar Feb 17 '19 09:02 anthonyjb

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': ''}}
)

anthonyjb avatar Feb 17 '19 09:02 anthonyjb

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

sfwat avatar Feb 17 '19 10:02 sfwat

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': ''}}
)

anthonyjb avatar Feb 17 '19 14:02 anthonyjb

The unset and unset_many methods have been in place now since 2020 so I'm closing this issue.

anthonyjb avatar Jul 17 '24 14:07 anthonyjb