Last active
May 24, 2016 08:28
-
-
Save nonZero/6c8c634ea5a8af8f350ddf368a344fc9 to your computer and use it in GitHub Desktop.
insert many posts as documents and query by publish date
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 random | |
import datetime | |
import collections | |
import pymongo | |
client = pymongo.MongoClient() | |
db = client.get_database('udi123') | |
posts = db.get_collection('posts') | |
posts.create_index('id', unique=True) | |
posts.create_index('published_at') | |
def generate_random_post(): | |
return { | |
'id': str(random.randint(1000000, 9999999)), | |
'content': 'blah ' * random.randint(2, 10), | |
'published_at': datetime.datetime( | |
2000 + random.randint(0, 15), | |
random.randint(1, 12), | |
random.randint(1, 28), | |
random.randint(0, 23), | |
random.randint(0, 59), | |
) | |
} | |
c = collections.Counter() | |
for i in range(1000): | |
post = generate_random_post() | |
result = posts.update_one( | |
{'id': post['id']}, | |
{'$set': post}, | |
upsert=True | |
) | |
key = 'new' if result.upserted_id else "updated" | |
c[key] += 1 | |
print("OK", c.most_common()) | |
where = { | |
'published_at': { | |
'$gte': datetime.datetime(2009, 1, 1, 0, 0), | |
'$lt': datetime.datetime(2009, 2, 1, 0, 0), | |
}, | |
} | |
for i, p in enumerate(posts.find(where)): | |
print(i, p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment