mongodb - How to properly write this query -
using pymongo v2.6, proper way write find query these conditions:
select * tasks processed = 0 , (process_lock_date null or process_lock_date < - 10 minutes) order date_added asc limit 50
basically need write query select 50 records haven't been picked processor yet or haven't been processed , picked more 10 minutes ago (ie pickup unprocessed tasks picked longer time ago , didn't marked processed in case previous processor failed)
any appreciated. thank you.
you need import timedelta , datetime objects first create date object represents datetime 10 minutes ago, use object mongodb query follows:
from datetime import timedelta date = datetime.datetime.now() - datetime.timedelta(minutes=10) docs = db.collection.find( { 'processed': 0, '$or': [ {'process_lock_date': {'$lt': date}}, {'process_lock_date': null} ] }).sort({'date_added': 1}).limit(50) doc in docs: print(doc)
Comments
Post a Comment