Last active
March 5, 2018 13:01
-
-
Save fmasanori/5923160 to your computer and use it in GitHub Desktop.
reddit query mongo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymongo | |
import sys | |
connection = pymongo.MongoClient("mongodb://localhost") | |
db = connection.reddit | |
stories = db.stories | |
def find(): | |
query = {'title':{'$regex': 'Python'}} | |
selector = {'title': 1, 'num_comments': 1, '_id': 0} | |
try: | |
cursor = stories.find(query, selector) | |
except: | |
print ('Unexpected error:', sys.exc_info()[0]) | |
sanity = 0 | |
for doc in cursor: | |
print (doc) | |
sanity += 1 | |
if sanity > 10: | |
break | |
find() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment