I’m new to MongoDB need some advice on how to design my database (with mongoose).
I want to have multiple documents that each have a user, a date, a type and a number.
Every few minutes the db should be check if any date is smaller than Date.now and if yes, my program do some things with type and user. The second thing that should be checked is if for every type, the number is bigger than another external value I am comparing it to, and if yes, it should do the same thing.
Since the database could get very big and these checks should run every couple of minutes, I want to make sure this thing is as efficient as possible. To achieve this, I thought about storing documents with different users and numbers as subdocuments with a parent that has a unique combination of type and date. These top-level documents should be sorted by date on saving so that only the first document has to be checked for an expired date every time the checks begin and for every other document, only the first subdocument (sorted by the number) has to be checked.
My question is if this is a good idea or if there is any better way to do it. If no, can you explain how I can sort the top-level documents so that I only have to read the first one’s date?