Guest User

Untitled

a guest
Sep 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. import time
  2. import pymongo
  3. import datetime
  4.  
  5. import bson
  6. assert bson.has_c()
  7.  
  8. conn = pymongo.Connection()
  9. coll = conn.testdb.test
  10.  
  11. print 'Collection size is: %d' % (coll.count())
  12.  
  13. year = 2010
  14. count = 0
  15.  
  16. t1 = time.time()
  17.  
  18. #
  19. # Query a year's worth of data, one month at a time.
  20. #
  21. def foo():
  22. global count
  23. for ii in range(1, 13):
  24. start = datetime.datetime(year, ii, 1)
  25. if ii == 12:
  26. end = datetime.datetime(year+1, 1, 1)
  27. else:
  28. end = datetime.datetime(year, ii+1, 1)
  29.  
  30. query = {'date': {'$gte': start, '$lt': end}}
  31. curs = coll.find(query).sort([('date', 1)])
  32.  
  33. for doc in curs:
  34. if doc['val'] < 2000000:
  35. count += 1
  36. else:
  37. return
  38.  
  39. foo()
  40. t2 = time.time()
  41. print 'Found %d docs in %s seconds' % (count, t2 - t1)
Add Comment
Please, Sign In to add comment