I am building an app which serves daily essentials like milk to customer. The milk subscription can be of 2 main types -
Regular Interval (say every 2 day)
Custom Interval (say every Tuesday and Thursday)
The customer should be able to edit their regular subscription for a period of time (say as usual I want 1 ltr milk but for 15th Feb to 30th Feb I want 2 ltrs)
User can also say pause my subscription for next 10 days as I am on vacation
Any suggestions/help with schema design will be helpful
As you can see each product has its default delivery details. It can be a dynamic object or array with specific fields per requirement. Those are the defaults if no exceptions are submitted for specific dates.
Now for the exceptions I recommend to have an exception collection with specific exceptions for specific dates. The idea is that for each delivery day the application will check if the defaults are not supposed to be overwritten by another value for that day from an exception. For example the exceptions collection will be :
Now if you index subscirptionId : 1, exceptionstartDate : 1, exceptionendDate : 1
Together , you can search what defaults needs to be overridden for each delivery day.
If there are multiple exceptions for a single day have the application merge and decide what is the end override …
To be honest mongoose is a third party framework that been adopted for schema enforcement as its main advantage. Now that MongoDB has built in easy to use schema validation we do not see benefits in using mongoose and using Node JS driver with the objects as is would be a prefared way.
Now the “or” is a conceptional idea, one object can have field x and the other field y and the application can read the object, look at the keys and define the specific patterns or workflows. Another option is to use generic names for the fields like :key and value.