Reputation: 15
Is it a good practice to put id field inside document or is it just a waste of resources? For example uid field inside User document.
upsertUserData(user) {
const userRef: AngularFirestoreDocument<any> = this.afs.doc(`users/${user.uid}`);
const data = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL
}
return userRef.set(data, { merge: true });
}
Is there any case when I could need that field in the future?
Upvotes: 0
Views: 1018
Reputation: 317487
In the most strict sense, it is a waste of space. Using the Firestore SDK APIs, you should always be able to use the ID of a document for a query, and access the ID of document results, without having to refer to it in a field of the same document.
But there may be some cases (specifically java POJO object mapping) when you don't want to write some extra code to pull the id from the document and put it in a field of your model object. In that case, maybe it's more convenient to have the id in a field. You can decide what's best for your case.
Upvotes: 2