这是从 Firebase 文档中复制的:- https://firebase.google.com/docs/firestore/extend-with-functions
评论说监听器将监听“用户”和所有子集合中的所有文档。
如何只监听子集合的所有文档而不监听父集合? (即,在此示例中,请勿收听“用户”中的文档。)
// Listen for changes in all documents in the 'users' collection and all subcollections
exports.useMultipleWildcards = functions.firestore
.document('users/{userId}/{messageCollectionId}/{messageId}')
.onWrite((change, context) => {
// If we set `/users/marie/incoming_messages/134` to {body: "Hello"} then
// context.params.userId == "marie";
// context.params.messageCollectionId == "incoming_messages";
// context.params.messageId == "134";
// ... and ...
// change.after.data() == {body: "Hello"}
});index.js
最佳答案
根据文档Specify a group of documents using wildcards :
If you want to attach a trigger to a group of documents, such as any document in a certain collection, then use a {wildcard} in place of the document ID
另外,我在这里搜索了一个similar question :
A change to a subcollection organized under a document do not count as changes to the document itself. They will not trigger a snapshot event on that document.
Listening to a subcollection is not any different than listening to a top-level collection. In both cases, the listener will only be triggered by changes to documents within that collection or subcollection.
那么我可以得出结论,这是不可能的。
关于google-cloud-firestore - Firestore云函数监听子集合但不监听父集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64971603/