someCollectionRef.add(/* data */)
is an alias for someCollectionRef.doc().set(/* data */)
.
So to specify your own ID, just use:
yourCollectionRef.doc(/* your id */).set(/* your data */);
firebase.firestore()
.collection("Users")
.doc(firebase.auth().currentUser.uid)
.collection("tasks")
.doc(/* your custom ID here */)
.set({
task: task.taskname,
category: task.category
})
.then(() => console.log('success!'))
.catch((err) => console.error('failed to set doc data!', err));
To place the document ID in the document, you can split this chain into two parts as below. Unless you plan on using Collection Group queries, you could use FieldPath.documentId()
to query documents by their ID instead without needing to store it in the document.
const myTaskDocRef = firebase.firestore()
.collection("Users")
.doc(firebase.auth().currentUser.uid)
.collection("tasks")
.doc(/* your custom ID here */);
myTaskDocRef
.set({
id: myTaskDocRef.id,
task: task.taskname,
category: task.category
})
.then(() => console.log('success!'))
.catch((err) => console.error('failed to set doc data!', err));