Question

I am working on the analysis of a mongo database. For analyzing the data, all the documents which were created in the past week have to be found. Currently, the database has a creation time field associated with each document, which is stored in mongo's default BSON format. Since the collection would be large, I require a fast method to do the same.

The database is still a work in progress, so more fields/indexes can be added to it, if it helps in making fast queries.

What is the fastest method to do this?

Was it helpful?

Solution

You could add an index on the creation time field (probably descending).

If the data is always going to be analyzed for a full week (Sunday-Saturday) rather than for the last seven days, you might consider adding another field that gets filled in with the week number when the document is inserted. It may be a more effective strategy if you have to have another field indexed to create a compound index on week,otherField - only by analyzing the queries you will be running on this data will you be able to determine if that would work better than an index on just creation time.

Reading up on how indexes work in MongoDB may help you formulate more questions you will need to determine answers to to get best performance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top