0

Mongo allows queries like this:

{_id: /^84042/}

i.e. no $regex keyword used.

How to pass the same regexes with pymongo?

If I do

my_collection.find('_id', '/^84042/')

it finds nothing.

UPDATE

The point is that I don't know whether I use exact value or regex. Hence, any solution involving $regex keyword or regex compiling is not applicable.

It should be the same as in mongo: just value spelling should differ.

Dims
  • 47,675
  • 117
  • 331
  • 600
  • Perhaps, `my_collection.find({"_id": {"$regex": "^84042"}})`? – Wiktor Stribiżew Dec 05 '17 at 20:51
  • You will *need* to know whether you're dealing with a regex or a string value. If you type it yourself you obviously know, because you're either typing a regex or a string literal. But even on the mongo console in Javascript you'd need to know the difference to programmatically produce the correct query. TL;DR: without knowing what it's supposed to be, you cannot treat it correctly. – deceze Dec 06 '17 at 10:02
  • The one who type the query NEED to know, but I NEED NOT to know and delegate this to mongo. This is the question. – Dims Dec 06 '17 at 15:40

0 Answers0