I am attempting to use the "$where" operator inside of a MongoEngine query using execjs. This question builds from a successful answer to a MongoDB query here: MongoDB find in collection with unknown key. I've tested the function inside of MongoDB and it works, I just need to port it over to MongoEngine.
The error I receive when running the following is:
pymongo.errors.OperationFailure: database error: Can't canonicalize query: BadValue $where got bad type
The code follows below:
my_js_function = 'function mongo_query(){ for( var c in this ){ if( c == "machines" ){ for(var i in this[c]){ for( var j in this[c][i]){ if(j == "process" && this[c][i][j] == "543ef1f380da5b0c476373c7"){ return true; } } } }; } return false; }'
compiled_function = execjs.compile(my_js_function)
And then the MongoEngine query:
companies = Company._get_collection().find( {"$where" : compiled_function.eval('mongo_query()') })
Thank you for your thoughts!
(p.s. I realize this might be taken care of by reworking my schema, but a lot has already been built on top of what we already have worked out.)
Alright I'll answer my own question. Instead of changing the schema or figuring out how to execute the javascript I called a subset of the collection into an object and iterated through it using Python. It's not the best answer but it was better than changing the schema or getting that ugly JS to execute.