0

I have a simple collection that contains two fields: timestamp and value (float). I need to get min and max values in the timestamp range.

The collection contains approx 10000000 documents.

I've add indexes like {"value" : 1, "timestamp" : 1} and {"value" : -1, "timestamp" : 1}.

Queries like

db.getCollection('Sample').find({'timestamp' : {'$gte': 1714284644, '$lte': 1745820644}}).sort({'value':-1}).limit(1)

took about 1 second. Count without limit shows 525600 documents.

"Explain" show that query covered by one of the idexes. IXSCAN takes most of time.

Is there any way to speed this query up?

3
  • Try it the other way around, i.e. { "timestamp" : 1, "value" : -1} – Wernfried Domscheit May 6 at 17:30
  • Tried to use {"value" : 2, "timestamp" : 1} and {"value" : 2, "timestamp" : -1}. It didn't help too. Changing the order of fields in the index didn't help too. – Сергей Дячук May 7 at 7:36
  • To find min and max values for a field use an aggregation query with $group stage with $min and $max operators. – prasad_ May 21 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.