在Mongoengine中使用execjs和$ where(Using execjs and $where inside of Mongoengine)

我试图使用execjs在MongoEngine查询中使用“$ where”运算符。 这个问题来自MongoDB查询的成功答案: MongoDB在集合中查找未知密钥 。 我已经测试了MongoDB中的函数并且它可以工作,我只需要将它移植到MongoEngine。

运行以下内容时收到的错误是:

pymongo.errors.OperationFailure: database error: Can't canonicalize query: BadValue $where got bad type

代码如下:

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)

然后是MongoEngine查询:

companies = Company._get_collection().find( {"$where" : compiled_function.eval('mongo_query()') })

谢谢你的想法!

(ps我意识到可以通过重新设计我的模式来处理这个问题,但是已经在我们已经解决的问题之上构建了很多。)

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.)

最满意答案

好吧,我会回答我自己的问题。 我没有改变模式或弄清楚如何执行javascript,而是将集合的一个子集调用到一个对象中,并使用Python进行迭代。 这不是最好的答案,但它比改变架构或让丑陋的JS执行更好。

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.

在Mongoengine中使用execjs和$ where(Using execjs and $where inside of Mongoengine)

我试图使用execjs在MongoEngine查询中使用“$ where”运算符。 这个问题来自MongoDB查询的成功答案: MongoDB在集合中查找未知密钥 。 我已经测试了MongoDB中的函数并且它可以工作,我只需要将它移植到MongoEngine。

运行以下内容时收到的错误是:

pymongo.errors.OperationFailure: database error: Can't canonicalize query: BadValue $where got bad type

代码如下:

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)

然后是MongoEngine查询:

companies = Company._get_collection().find( {"$where" : compiled_function.eval('mongo_query()') })

谢谢你的想法!

(ps我意识到可以通过重新设计我的模式来处理这个问题,但是已经在我们已经解决的问题之上构建了很多。)

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.)

最满意答案

好吧,我会回答我自己的问题。 我没有改变模式或弄清楚如何执行javascript,而是将集合的一个子集调用到一个对象中,并使用Python进行迭代。 这不是最好的答案,但它比改变架构或让丑陋的JS执行更好。

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.