許容範囲を見つけて減算/追加し、範囲を使用して検索するのに時間がかかります。
from datetime import datetime, timedelta
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
# Test Data
client.test.times.remove()
client.test.times.insert({ 'date': datetime(2013,03,26,05,33,15)})
client.test.times.insert({ 'date': datetime(2013,03,26,16,48,39)})
def get_approx_datetime(dt):
start = dt-timedelta(minutes=10)
end = dt+timedelta(minutes=10)
return (start,end)
find_date = datetime(2013,03,26,05,28,15)
(start,end) = get_approx_datetime(find_date)
for time in client.test.times.find({ 'date': { '$gt': start, '$lt': end} }):
print time
出力:
{u'date': datetime.datetime(2013, 3, 26, 5, 33, 15), u'_id': ObjectId('517cb34f1d41c806aec7c4ad')}