Python Forum

Full Version: error when trying to update mongodb
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am working with flask trying to update a mongodb collection which works fine when I state the fieldname that needs updated, but I want to fieldname to be a variable depending on which needs updated. This works fine to update the field:

Scoresenglish6.objects(user_id=user_id).update(E6001=str(score))
But, the fieldname being updated can change (so in this case it is "E6001") so I have the fieldname needing updated as a variable called field_name:

score = "10"
field_name = "E6001"
Scoresenglish6.objects(user_id=user_id).update(field_name=str(score))
Then I get the error:

Error:
mongoengine.errors.InvalidQueryError: Cannot resolve field "field_name"
obviously because there is no field name called field_name. Is there a way to do this? Thanks in advance, I'm a home coder working on a home project so apologies if this seems vague and not well explained.
Ah, you seem to be using MongoEngine. Did you look at the docs? You can pass keyword arguments in a dict, e.g. here (search for "updating" to see the example). Since objects returns a QuerySet object when you access it, per the top of the page, you might want to look at the docs for that class too, since that will tell you fully how to use the update method.
(Jul-04-2020, 04:29 PM)ndc85430 Wrote: [ -> ]Ah, you seem to be using MongoEngine. Did you look at the docs? You can pass keyword arguments in a dict, e.g. here (search for "updating" to see the example). Since objects returns a QuerySet object when you access it, per the top of the page, you might want to look at the docs for that class too, since that will tell you fully how to use the update method.

Thank you for the help - I am indeed using mongoengine. I've read the info on the link you provided thoroughly but still can't get to the bottom of solving my obstacle - probably doesn't help I don't understand the majority of it! I tried your suggestion of passing the info in a dictionary with no success (if I have done it right that is)

field_name = quiz_id
params = {}
params[field_name] = score
Scoresenglish6.objects(user_id=user_id).update(params)
I get the error:

Error:
ValueError: update cannot be empty
Anyhow, thanks for the help, I'll keep trying Wall
try adding ** to unpack the dictionary
Scoresenglish6.objects(user_id=user_id).update(**params)
(Jul-04-2020, 05:34 PM)Yoriz Wrote: [ -> ]try adding ** to unpack the dictionary
Scoresenglish6.objects(user_id=user_id).update(**params)

Thank you so much, that did the trick! I can't say thanks enough, learning python is so challenging Think
I think there's enough to do with learning the language itself, that trying to do that in the context of a project just adds more complexity that then makes it more difficult.
(Jul-04-2020, 06:42 PM)ndc85430 Wrote: [ -> ]I think there's enough to do with learning the language itself, that trying to do that in the context of a project just adds more complexity that then makes it more difficult.

You're absolutely right, I'm trying to keep a project simple and followed a tutorial but then there are things that pop up that are so complicated to a beginner!