0

We are designin User and Order collection.

In our User collection, we already has DBRef referencing to Order collection but our manager asked us adding an extra field in the order collection.

  Existing collections
  Users:
  {
    "_id": "user001",
    "orders": [DBref('order', 'order001'),DBref('order', 'order002')],
    ...
  }

  Orders:
  {
    "_id": "order001",
    "amount": 123,
    "user": DBref('users', 'user001'), <--- new requirment from manager
    ...
  }

My concern is the id maybe not match for some human mistake(i.e someone updated the users field in the order collection but forgot to update the array). Will it be a "single source of truth" problem or another foreseeable problem? Manager would like to view the owner of the Order clearly in Mongo Compass. For my understand, we can use $lookup for checking the order's owner.

1 Answer 1

0

My concern is the id maybe not match for some human mistake(i.e someone updated the users field in the order collection but forgot to update the array). Will it be a "single source of truth" problem...

Sure, that's certainly a potential problem with the design choice your manager wants you to make. It's good you're thinking ahead on those kinds of things.

This is where normalized tables, typically in a RDBMS (SQL database), with proper constraints, prevents you from such data integrity issues. And a view could be used to unify the the Users and Orders tables so anyone could "view the owner of the Order clearly" without compromising on that data integrity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.