0

I have a collection of 290,000 documents with different timestamps, is there a way to specify a list of timestamps (lets say 200) using $in ['1506952790995', '1506953025981' ... ] and to have mongoDB find 200 documents from the full collection which are nearest to the specified timestamps in the list, if exact matches cannot be found (assuming that the full collection may not have documents precisely matching the $in list all the time)?

here is how all my documents in the collection look like: { _id : ObjectId source: String price: Double timestamp: Date }

What is the fastest query to achieve this?

By the way, this question still cannot be considered dupplicate since, it also asks about --a list-- of timestamps, not just a single one.

Odragon
  • 1
  • 2
  • The "fastest" to "closest' would be using `$near` and an appropriate index. It requires a bit of a hack in that you would need to store the data as "arrays" ( i.e `[1506952790995, 0]` ) in order to use such an operation. Considering that you appear to have stored the timestamp values as "strings" ( which is next to useless ) then you might consider it since you have stored the data incorrectly and you need to change that anyway. – Neil Lunn Oct 27 '17 at 10:20
  • Sorry, I am making a correction in the question to reflect the schema of all the documents in the collection - I am storing the timestamp as Date! – Odragon Oct 27 '17 at 10:25
  • Correct all you want. The same question has been asked many times before and you have been pointed to the only approaches there are. See big coloured box above your question title. – Neil Lunn Oct 27 '17 at 10:26
  • Thanks, looking at the already answered question lets assume I go the hack way and introduce an array as suggested, what would be the query for multiple points and not just one? ie db.places.find( { ratio : { $near : [50,0] } } ).limit(1) would return just one result, I want to specify more than one - say 200. – Odragon Oct 27 '17 at 10:39

0 Answers0