firebase/snippets-node

Nested Objects with ArrayUnion

puppybits opened this issue · 1 comments

The documentation and examples do not mention if updating nested objects works with arrayUnion

This is the document's example:

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

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

does this work or not?

// does this work?
db.collection("users").doc("frank").update({
    "favorites.subjects": FieldValue.arrayUnion("history")
})

@puppybits good question! Yes that should work. The field favorites.subjects is an array field like any other and therefore you can use arrayUnion with it.

If you try it and I'm wrong, that's a bug!