document creator as username or userId

风流意气都作罢 提交于 2019-12-24 00:55:39

问题


In a meteor collection for a document I have a creator attribute. Is it advisable to store the username (more intuitive) or userId (which is user._id) as creator attribute? What is the more standard practice?


回答1:


With document databases, the answer is commonly, "both".

Storing the userId is required as it is the only field that is guaranteed to not change (as others have pointed out).

It's often worth it to store "denormalized" data as well. In some cases, its actually required.

Take for example a line item on an invoice. It needs to be static, a snapshot in time. You'd need to store the product info at the time of the purchase even though later that product's name or other characteristic may be changed by the vendor.

In our app, there are times when we store user profile info (name, contact info, etc) in addition to the userId. In some cases its strictly for performance reasons, in others its a snapshot used in audit reporting and must not change.

In the performance case, you'll probably want to have processes in place to update the "denormalized" data if it is ever changed in the "source of authority". In the snapshot case, like invoice line-items, you'll want to explicitly not update them. Sometimes, its just ok for the data to be out-of-date and its not worth the complexity of keeping it updated.

For a good example of this kind of "document vs relational" design discussion, check out these two blogs posts:

  • http://ayende.com/blog/4465/that-no-sql-thing-the-relational-modeling-anti-pattern-in-document-databases

  • http://ayende.com/blog/4466/that-no-sql-thing-modeling-documents-in-a-document-database




回答2:


I always join collections by id, and I'd advise you to do the same. The id will never change, but the underlying data may be altered. In this example, if the user changes his/her username then the join would be broken.




回答3:


A couple reasons why you would use User._id over a username

  • User._id's are more URL friendly (no UTF-8, other languages etc.)
  • User._id is static
  • Mongo and Minimongo is good at finding and updating Collections with an _id



回答4:


The best reason to use the _id from the users collection is that all meteor methods and publish functions provide this.userId to identify the user who made the call. That userId is the only information you have about who is making the client request.

If you use username or some other unique field in Meteor.users you will always need to look up the user by id to get the other field.



来源:https://stackoverflow.com/questions/21798844/document-creator-as-username-or-userid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!