【发布时间】:2022-01-13 07:40:14
【问题描述】:
注意:这不是重复的问题,请阅读到最后并查看包含的图片。
我在 Firestore 的集合/文档中有一个嵌套对象和一个数组字段。
主要类别
- 饮料
- 小吃
饮料项目
- (水、能量、牛奶……)
零食是
- (薯片、饼干、玉米、..)
用户可以为多个具有到期日期的项目订阅这两个类别:
- 饮料->能量
- 饮料->牛奶
- 小吃->薯片
我想更新 [expDate] 字段,其中 [name] 等于饮料,[type] 等于 [能量]
我更重要的是探索了 Firestore 文档 compound queries in Cloud Firestore 并阅读了很多关于 stackeoverflow 的文章和问题,但我找不到答案,下面是我的部分代码。
db.collection(this.dbName)
.where("name", "==", "drinks")
.where("subscriptions.data.type", "==", "energy")
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
let data = doc.data();
if (data.email == email) {
let subscriptions = data.subscriptions;
subscriptions.forEach((subscription) => {
if (subscription.name == productName) {
let prodTypes = subscription.data;
prodTypes.forEach((prodType) => {
if (prodType.type == itemType) {
let docRef = fb.db.collection(this.dbName).doc(email);
fb.db
.collection(this.dbName)
.doc(email)
.update(docRef, {
subscriptions: [
{
data: [
{
expDate: expiration,
type: itemType,
},
],
name: productName,
},
],
});
}
});
}
});
}
});
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
我没有得到上述查询的任何控制台日志结果。
【问题讨论】:
-
@mark-rotteveel,感谢您的编辑,但您从问题中删除了 firebase 关键字,我希望将其加粗,并且具有 firebase 专业知识的人会看到该问题。
-
这就是标签系统的用途。您不应该只在问题的标题中添加标签。
标签: javascript firebase google-cloud-firestore nested-object compound-query