Silas
Silas

Reputation: 89

querying mongodb using pymongo

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

Upvotes: 2

Views: 368

Answers (1)

chridam
chridam

Reputation: 103445

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

Upvotes: 2

Related Questions