Keep tracking some changes in field of a doc

Hello, I have a collection will store two status of a product, the payment_status is about the status of “paid” or “unpaid”, the deliver_status is status about “delivered” or “undelivered”. Some of the user with role “seller” can change the value of payment_status, some users with role “customer” can change the value of delivered_status. For every changes on every status, I want to record the value change and who made such change. Is there a way to do that? To make sure the payment_status can only be changed by role “seller”, is there way to do that, like in schema validation?

Thanks,
James

Hey @Zhihong_GUO

It sounds like you want a history of all the changes made to a product document. Correct me if I am wrong.
However, I want to ask one thing. Is the history data accessed regularly? I ask because my initial thought would be to store the history of a product change in a new collection and reference the history document to the product document. This history collection would be of all changes made. Therefore the collection grows instead of an field array growing.
Or you could have a field on the product document as an array which stores the history. Do this if you are fairly confident that the array will not grow infinitely.
As for making sure only authorized people can perform certain action; I would get the user data and use server code to check that the current user has the correct permissions to perform the action being requested.

{
  _id: 5437t3952305t,
  username: natac13,
  userType: 'seller',
}

Therefore if

if (user.userType === 'seller') {
  // do action
}

Let me know if this helps.

Hello Sean,

Many thanks for the answer. I think the history and the query on the history will not be very frequently, so I will try your second solution, the array based solution in my app. I will keep you updated for any progress.

Regards,

James

Glad to be able to help. Just lastly I want to clarify. If the history is accessed infrequently, then I would store it in a different collection entirely. That way you are keeping the product documents smaller in size. Then reference the history collection on the product document

OK, quite clear. Thanks for the information.

1 Like