更新Firestore中对象的字段? [英] Update a field in an object in Firestore?

查看:68
本文介绍了更新Firestore中对象的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是文档中提供的示例,用于更新Firebase中嵌套对象内的字段.

This is the example provided in the documentation to update a field within a nested object in firebase.

var frankDocRef = db.collection("users").doc("frank");
frankDocRef.set({
name: "Frank",
favorites: { food: "Pizza", color: "Blue", subject: "recess" },
age: 12
});

// To update age and favorite color:
db.collection("users").doc("frank").update({
"age": 13,
"favorites.color": "Red"
})
.then(function() {
console.log("Document successfully updated!");
});

我不想添加收藏夹,而是想添加到收藏夹中,以便有人向我指出如何执行此操作的正确方向.

Instead of updating the favourites, I want to add to favourites would someone point me in the right direction on how to do this.

假设我要

firebase: "Help"
the the resulting favourites object should be
favorites: { food: "Pizza", color: "Blue", subject: "recess", firebase: "Help" },

我将set与点操作一起使用,但它会覆盖所有内容.

I used set with the dot operation but it overrides everything instead.

推荐答案

要将条目添加到集合(JavaScript对象)中,请使用DocumentReference.update:

To add an entry to a set (a Javascript Object), use DocumentReference.update:

db.collection("users").doc("frank").update({
  "favorites.firebase": "Help")}
})

将导致

favorites: { food: "Pizza", color: "Blue", subject: "recess", firebase: "Help" }

这篇关于更新Firestore中对象的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆