Reputation: 1700
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?
Upvotes: 2
Views: 128
Reputation: 5217
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:
Upvotes: 1
Reputation: 4138
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.
Upvotes: 0
Reputation: 927
A couple reasons why you would use User._id over a username
Upvotes: 1
Reputation: 64342
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.
Upvotes: 2