0

I am currently trying to build a query in a string then after that use eval (ast_eval) to transform it to a dict in order to pass up to pymongo update.

for item in default_properties:
    query = '{"_id": {ObjectId:"%s"}}' % (oid)
if (doc["diff_id"] == diff_id):
                default_val_pos = [line[0], line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8]]
                update_val = '{ "$set":{"properties.%s": default_val_pos[%d] } }' % (item,idx)
                db.routes.update(eval(query), eval(update_val))

I thought that it should be okay now since in mongoshell itself accepts the same query, the only difference is that I used from bson.objectid import ObjectId to format it well, however, this error occured:

InvalidDocument: documents must have only string keys, key was <class 'bson.objectid.ObjectId'>

As far as I see it, I don't have any "field key" that is not a string other than the ObjectId and hmmm I don't think the I should turn ObjectId in a hardcoded string lol. So I was wondering if someone can explain what's the problem?

Thanks!

1 Answer 1

2

It should just be:

query = '{"_id": ObjectId("%s")}' % (oid)

You're getting confused with MongoDB Extended JSON, which you can evaluate like:

query = '{"_id": {"$oid": "%s"}}' % (oid)
from bson import json_util
query_dict = json_util.loads(query)

But the simplest way to build your query is with Python literals:

query_dict = {"_id": ObjectId(oid)}
Sign up to request clarification or add additional context in comments.

1 Comment

ah! so that's it. Thank for the clear and concise explanation where I'm wrong. And also for suggesting the last one! I forgot that "I COULD" do that! thanks again!

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.