Retrieve every nth document in a collection

I have a 10 year history of the Dow Jones index. Is there a way I can retrieve every, say, 5th document? I want to do this to speed up graphing Closing data. So, instead of plotting over 1800 points, only 360+ points need to be plotted. Thank you.

Hi @David_Robinson,

Welcome to MongoDB community.

There are a few ways to work around it. You can add an aggregation stage with $sample to get random few documents :

Or you can rank each document and query the ones mod with 5 is 0:

You can group the data by lets say a day or a week and use $last to project one document of each group:

{ plotDoc: { $last : "$$CURRENT" } }

Thanks
Pavel