1

I'm trying to remove the keys from a mongoengine Document in python:

document.update(unset__name=True)
document.update(unset__surname=True)
document.update(unset__dob=True)
...

but instead of the above I would like to loop through them and use a variable, something like:

document.update(unset__<key>=True)

Is this possible?

1
  • 1
    you can create a dict dynamically and then pass it to document.update Commented Jul 12, 2022 at 12:55

2 Answers 2

1

with a map ? you can build it and put as function kwargs

unset_map = {
    "unset_key": True
}
document.update(**unset_map)
Sign up to request clarification or add additional context in comments.

1 Comment

unpacking is not necessary
0

yes, this is feasible by destructuring a dictionary. For example, like in there:

d = { "p1": 11, "p2": 12}
def f(p1,p2):
    print(f"p1={p1}, p2={p2}")

f(p2=100,p1=10)
f(**d)

In your case, you will simply have to build a dictionnary with all the keys you want to destroy, by building the correct string:

def delete_key(key_name: str):
    d = { f"unset__{key_name}": True }
    document.update(**d)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.