2

i just started using mongodb and setup a test database to handle web scraping results from a couple scripts i created. right now, the date_found is being loaded as a string. when i run this in mongohub:

{"date_found" : /.*2015-05-02.*/}

i get all the collections with '2015-05-02'. awesome!

however, when i run:

for item in collection.find({"date_found": "/.*2015-05-02.*/"}):
    print item

i get nothing.

also, this:

for item in collection.find():
    print item

gives me all the collections, so it seems everything works to the extent that i can query the database.

any chance someone can tell me what bonehead mistake i'm making (or what i'm missing)?

1 Answer 1

2

In pymongo, to include a regular expression you could try something like this:

import re
regx = re.compile(".*2015-05-02.*")
for item in collection.find({"date_found": regx})
    print item

Or using the $regex operator:

import re
regx = re.compile(".*2015-05-02.*")
for item in collection.find({"date_found": {"$regex": regx} })
    print item
Sign up to request clarification or add additional context in comments.

6 Comments

both of those are throwing an "sre_constants.error: nothing to repeat" at the regx line...
Looks like the leading . is missing in those regex strings.
I have updated the answer the include the leading .. Thanks to @JohnnyHK for the heads-up.
works like a charm. do you mind shedding some light on the difference? i'm assuming it has something to do with pymongo but i'm not really sure...just trying to understand. thanks again!
Basically the leading . matches any char except newline, best explained by this Python 2.7 Regular Expressions Cheat Sheet.
|

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.