Querying "like" in pymongo

Viewed 11103

I have researched this but can not find why what I am trying is not working, and will warn that I am somewhat new to python and very new to mongodb. I have a mongo database of tweets in JSON which I am trying to query through Python and pymongo. I want returned the 'text' and 'created_at' fields for all tweets that contain "IP".

I have tried the following, which works perfectly when I do this through the terminal:

db.tweets.find({text:/IP/},{text:1,created_at:1})

In Python, after experimenting I have found that I need to put the field names between quotes. I have gotten the following similar query to work:

cursor = db.tweets.find({'created_at':"Thu Apr 28 09:55:57 +0000 2016"},{'text':1,'created_at':1})

But when I try:

db.tweets.find({"text": /.*IP.*/},{'text':1,'created_at':1})

or

cursor = db.tweets.find({'text':/IP/},{'text':1,'created_at':1})

I get a

'SyntaxError: invalid syntax' at the "/IP/" part of the code.

I am using mongo 3.4.6 and python 3.5.2

1 Answers
Related