Python Forum
error when trying to update mongodb
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error when trying to update mongodb
#1
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.
Reply
#2
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.
Reply
#3
(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
Reply
#4
try adding ** to unpack the dictionary
Scoresenglish6.objects(user_id=user_id).update(**params)
Reply
#5
(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
Reply
#6
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.
Reply
#7
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Retrieve images base64 encoded MongoDB and Flask Nuwan16 2 3,291 Oct-13-2020, 06:25 PM
Last Post: Nuwan16
  Store Screenshot Selenium + MongoDB Nuwan16 9 3,600 Aug-18-2020, 03:57 AM
Last Post: ndc85430
  filtering by category flask+mongodb Leon79 3 9,029 Jul-19-2020, 04:25 AM
Last Post: ndc85430
  Extract json-ld schema markup data and store in MongoDB Nuwan16 0 2,454 Apr-05-2020, 04:06 PM
Last Post: Nuwan16
  storing images in mongodb using python richa828 2 9,369 Jun-06-2018, 08:08 AM
Last Post: richa828
  Begginer - How to choose a server to run my Python script with MongoDB? Mapl 1 3,744 Oct-24-2017, 01:02 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020