无法在PyMongo中工作,但在mongo shell中工作

时间:2019-12-17 19:34:18

标签: mongodb pymongo

以下mongodb查询在mongo shell上运行良好

> db.mycoll.find( { $where: function() {return this.hash == '-8653408530678185564' || this._id == 'df8a2b91effa2143c0f7f663'} } )
{ "_id" : ObjectId("5df8a2b91effa2143c0f7f66"), "row_data" : [ [ "198.0.0.0", "1576571739279", "-8653408530678185564", "1", "31", "31", "9167de:912349:", "9.9.0" ] ], "hash" : "-8653678185564" }

但是,在pymongo上运行时,同一查询给出语法错误

  File "parse_v2.py", line 109
    coll.find({ '$where': function() {return this.hash == '-8653408530678185564' || this._id == 'df8a2b91effa2143c0f7f663'} } )
                                     ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:0)

Pymongo与mongo shell不同。它是mongo后端的软件驱动程序。您可以在pymongo中完成大多数在shell中可以完成的工作;但是方法,格式或语法可能有所不同。

尝试:

db.mycoll.find( {'$or': [{ hash: '-8653408530678185564'}, {'_id': 'df8a2b91effa2143c0f7f663'}] } )