I have a huge bucket that contains all user's notification data. like this:
┌────┬─────────┬─────────────────────────┐
│ id │ user_id │ data │
├────┼─────────┼─────────────────────────┤
│ 1 │ 1 │ {"somekey":"someValue"} │
│ 2 │ 2 │ {"somekey":"someValue"} │
│ 3 │ 1 │ {"somekey":"someValue"} │
│ 4 │ 1 │ {"somekey":"someValue"} │
│ 5 │ 1 │ {"somekey":"someValue"} │
│ 6 │ 2 │ {"somekey":"someValue"} │
│ 7 │ 2 │ {"somekey":"someValue"} │
│ 8 │ 1 │ {"somekey":"someValue"} │
│ 9 │ 2 │ {"somekey":"someValue"} │
│ 10 │ 2 │ {"somekey":"someValue"} │
└────┴─────────┴─────────────────────────┘
So, anytime I want to insert a new record, for example for user_id=2, I want to remove earliest record for user_id=2 to have only N record for each user (of course if total number of records is less than N, there will be no remove)
There might be a better data modeling approach. Does all of this data need to be in separate documents? If "N" is a relatively small number, you could fit all of these into an array within a single document. Like:
Then the process would be:
This approach has the benefits of simplicity and of not needing to update multiple pieces of data. The way you've modeled it could work, but you would need ACID transactions (which aren't available in Couchbase's Node SDK yet) or maybe an Eventing function to check to make sure there aren't too many notification documents for each user whenever a new notification is created.