Reputation: 1103
Let's consider the following example:
import { firestore } from 'firebase-admin';
const userCollectionRef = firestore().collection('users');
const setUserData = (userId, data) => {
return firestore().runTransaction(async trx => {
const userDocRef = userCollectionRef.doc(userId);
return trx.set(userDocRef, data);
});
};
const doSomethingElse = (userId, doc, data) => {
return userCollectionRef
.doc(userId)
.collection('subCollection')
.doc(doc)
.set(data);
};
If I call setUserData(123, someData);
and while this function is in progress the doSomethingElse(123, 'someDoc', someOtherData)
function gets called - will the transaction fail because a document in a sub-collection got modified?
Based on the docs I would assume that transaction will fail only if the user/123
document is modified while setUserData
call is in progress, but modifications to user/123/subCollection/someDoc
document won't cause the transaction to fail. I'm not sure though because the transaction failure docs section is not very explicit about it. It just says:
The transaction read a document that was modified outside of the transaction. In this case, the transaction automatically runs again. The transaction is retried a finite number of times.
Upvotes: 0
Views: 104
Reputation: 3499
While I don't "know" the answer, I can say that documents and documents in their sub-collections are entirely separate - in your example of user/123/subCollection/someDoc
, the document user/123
is only used as part of the path/index to user/123/subCollection/someDoc
- there is no other relation between them. In fact, document user/123
could be REMOVED and the path to user/123/subCollection/someDoc
would remain valid (it shows in Firebase console as an italicized path)
Upvotes: 2