[英]can I get the path or reference of the document fetched from Firestore collection group query?
所以我想像这样执行集合组查询,以获取收件箱中的所有过期消息文档
const oneMonthAgo = moment().subtract(1, "month").toDate();
db.collectionGroup("inbox")
.where("createdAt", "<", oneMonthAgo)
.get();
inbox
实际上是users
集合中的一个子集合,所以路径会是这样的:
users/{userID}/inbox/{messageID}
在我使用上面的集合组查询代码获取所有过期消息后,我需要删除所有这些过期消息。 要删除消息文档,我需要知道文档的路径/引用
我可以从消息文档中的字段中获取 messageID。 但我不知道用户 ID 是,所以我不知道删除该消息的完整路径/参考
users/ ?????? /inbox/{messageID}
那么我可以仅从上面的集合组查询代码的结果中获取用户 ID 吗? 因为我需要使用此代码删除消息文档
db.doc(`users/${??????}/inbox/${messageID}`).delete()
上面的代码将返回FirebaseFirestore.DocumentData
的 promise。 我需要获取从集合组查询中获得的文档的路径或引用。
我可以这样做吗?
给定一个消息文档,您可以通过遍历其引用的parent
链来确定用户:
const messages = await db.collectionGroup("inbox")
.where("createdAt", "<", oneMonthAgo)
.get();
messages.forEach((messageSnapshot) => {
const messageRef = messageSnapshot.ref;
const inboxRef = messageRef.parent;
const userRef = inboxRef.parent;
console.log(userRef.id); // logs the id of the user document this message is for
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.