2

Why does mongodb return results in shell but not in python.

Shell:

> db.posts.find({ Body: /html/ }).count()
5524

Python code:

query = {"Body": '/html/'}
r = mo_db.posts.find(query)
print r.count()
> 0

All other queries work fine, just find() works fine. Is there another way to handle slashes?!
I also tested r'/html/' and u'/html/'.

1 Answer 1

2

You're querying for the value as a string, not an actual regular expression object. The /.../ syntax is javascript syntax sugar for constructing the regex, but in python you need to do it with the re module.

Try this:

import re
pattern = re.compile("html")
query = {"Body": pattern}
r = mo_db.posts.find(query)
print r.count()
Sign up to request clarification or add additional context in comments.

1 Comment

Worked. Thanks man. I thought the /.../ was handled by mongo.

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.