Reputation: 1247
This might be a somewhat trivial question, but I'm trying to minimize my write operations in a Firebase app and wanted to check on my current code. Let's say I have a Firebase collection that looks like this:
conversations
- someConversationId (<--document)
- someMessageId1 (<-- object within document)
- text: "Hey, what's up?",
- dateSent: new Date(2020, 1, 1),
- sentBy: "someonesUserId"
- someMessageId2 (<-- object within document)
- text: "Not much",
- dateSent: new Date(2020, 1, 2),
- sentBy: "someoneElsesUserId"
In this case, the messageId
is the key
, and the value
is an object
with a number of properties.
There are a couple ways I could update the above document. Consider the following two:
firebase.firestore().collection("conversations").doc("conversationId").update({
someMessageId1: {
text: "New text",
dateSent: new Date(2020, 1, 2),
sentBy: "someNewUserId"
});
firebase.firestore().collection("conversations").doc("conversationId").update({
someMessageId1.text: "New text",
someMessageId1.dateSent: new Date(2020, 1, 2),
someMessageId1.sentBy: "someNewUserId"
});
My assumption is both methods would result in 1 write operation. Is this right?
Why I'm asking this
I have a document with an object within it. In that object are, say, 5-10 properties. I would like to be able to overwrite some of those properties in the object without overwriting the entire object, because on the front end I will only know 2-3 of the properties I'll want to update and I want to keep the rest of the properties. The first method above would overwrite the entire object and leave it with only 3 properties and the others would be erased (I think?). The second method would only overwrite the 3 properties specified and any other properties on the object would remain. But I want to know how many write operations I'll be performing in this instance, and may rearrange my database structure if the second method above counts as 3 writes. Thanks!
Upvotes: 0
Views: 73
Reputation: 317467
It doesn't matter how many fields you change per update, or the way you express that those fields should be updated. It's still just one document written per call to update()
, which costs one write.
Upvotes: 2