云火恢复: 使用动态键更新嵌套对象中的字段

以下是正式文件:

{
name: "Frank",
favorites: { food: "Pizza", color: "Blue", subject: "recess" },
age: 12
}


// To update favorite color:
db.collection("users").doc("frank").update({
"favorites.color": "Red"
})

我想使用动态键代替颜色。

db.collection("users").doc("frank").update({
"favorites[" + KEY + "].color": true
});

这当然是不可能的,并将抛出一个错误。

我一直想这么做:

db.collection("users").doc("frank").update({
favorites: {
[key]: {
color": true
}
}
});

它实际上是用正确的键更新,但不幸的是,它覆盖了其他键(它们正在被删除)。

49306 次浏览

I found the solution inspired by a firebase solution (replacing "/" by ".").

var usersUpdate = {};
usersUpdate[`favorites.${key}.color`] = true;


db.collection("users").doc("frank").update(usersUpdate);

This solution worked for me:

db.collection('users').doc('frank').update({
[`favorites.${key}.color`]: true
});

You can update specific fields of variable(ly) named nested objects like below.

ref.set({
name: "Frank",
favorites: { food: "Pizza", quantity: 2 }
});


//now the relevant update code
var name = "favorites";
var qty = 111;
var update = {};
update[name+".quantity"] = qty;
ref.update(update);

https://jsbin.com/hihifedizu/edit?js,console

Just a note about a potential pitfall: After discovering that you can update nested fields using a dot syntax, I tried using set() the same way, since I needed it to work whether the object already exists or not:

var updateObj = {['some.nested.property']: 9000};
docRef.set(updateOb, {merge: true});

Unfortunately, this doesn't work – it sets a property whose key is some.nested.property instead. Inconsistent, but okay.

Fortunately, it appears that set(updateObj, {merge: true}) does a deep merge, so if you construct your update object as a fully nested object, your nested objects will also be properly merged:

// create the object
db.doc('testCollection/doc').set({p1: {['p2']: {p3: true}}}, {merge: true})
// update the existing object
db.doc('testCollection/doc').set({p1: {['p2']: {p4: true}}}, {merge: true})


// result is now this:
{ p1: { p2: { p4: true, p3: true } } }

I felt this is the simplest solutions of all the above :)

db.collection("users").doc("frank").update({
[`favorites.${KEY}.color`]: true
});

you can use

var auxKey = "history." + someVar;
var dataToUpdate = {
[auxKey]: "new data generated"
};
db.collection("users").doc("frank").update(dataToUpdate);

2022 Update with Webversion 9 (modular):

const docRef = doc(db, 'users', frank);


var usersUpdate = {};
usersUpdate[`favorites.${key}.color`] = true;


updateDoc(docRef, userUpdate);