user3399180
user3399180

Reputation: 572

How to update existing document in firestore using set method

I have a firestore document that I want to update. Here's my code -

admin.firestore().collection('testResult').doc(`${testId}`).set(
            {
                name: userName,
                email: email,
            },
            { merge: true },
        );

I am using "set" method with "merge: true" object. As I want to create a new document if it doesn't exists and update the data if it exists.

The document that I am trying to update is getting rewritten by new property that I sent, rather than appending the data in the existing document. I read that using {merge: true} will merge the incoming data with current data, but that's not working. My whole document is being replaced by new data.

Here's my firestore collection when I first create the document -

enter image description here

And here's the same document when I update it -

enter image description here

Upvotes: 1

Views: 4817

Answers (3)

user3399180
user3399180

Reputation: 572

I got it working by doing what Frank van Puffelen suggested. Here's the correct code if anyone needs this in future -

admin
    .firestore()
    .collection('testResult')
    .doc(`${testId}`)
    .get()
    .then(doc => {
        if (doc.exists) {
            admin
                .firestore()
                .collection('testResult')
                .doc(`${testId}`)
                .update({
                    data: admin.firestore.FieldValue.arrayUnion(
                        { userName, email }
                    ),
                });
        } else {
            admin
                .firestore()
                .collection('testResult')
                .doc(`${testId}`)
                .set({
                    data: [{ userName, email }],
                });
            }
        return;
    })
    .catch(error => console.log(error));

First I checked if the document exists. If it doesn't exists then I am creating a new document and saving a object containing an array with my values. If it exists then I am updating the array.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

The update() method (or set() with a merge flag) creates a document that:

  1. contains the value for fields that you specify.
  2. any other fields that are already in the document.

So for any field you specify, any existing value for that field in the document will be overwritten.

Given that knowledge, the result is working as expected.


If you want to append the values you specify to the current value of that field, you'll need to use an atomic transaction to read-and-write the document.

If you want to store multiple username+email combinations, you'll need to do so in an Array field. You can then either add the new combination with a transaction again, or (if the combination of username+email has to be unique) you can use the atomic array-union operation

Upvotes: 1

LeadDreamer
LeadDreamer

Reputation: 3499

the merge doesn't create new FIELDS, it allows you to either ADD new fields that don't already exist or to selectively update specific fields. Fieldnames are unique; in your statement:

admin.firestore().collection('testResult').doc(`${testId}`).set(
            {
                name: userName,
                email: email,
            },
            { merge: true },
        );

you are specifying the fields name and email, and EXACTLY AS THE COMMAND SAYS it is setting those fields to the new values. If you had done:

admin.firestore().collection('testResult').doc(`${testId}`).set(
            {
                anotherName: userName,
                anotherEmail: email,
            },
            { merge: true },
        );

...it would have ADDED those fields (as in "merged new fields into the document") and left the existing fields name and email in-place

IF you had done:

admin.firestore().collection('testResult').doc(`${testId}`).set(
            {
                anotherName: userName,
                anotherEmail: email,
            },
            { merge: false },
        );

...(note merge: false) it would have set the DOCUMENT to

            {
                anotherName: userName,
                anotherEmail: email,
            },

...ignoring the existing fields.

Remember, fieldnames are UNIQUE in a document - you WILL NOT SEE a document like so:

            {
                name: userName1,
                email: email1,
                name: userName2,
                email: email2,
                name: userName3,
                email: email3,
                name: userName4,
                email: email4,
            }

I strongly suggest you study the documentation quite a bit more and understand exactly how Firestore documents work.

Upvotes: 3

Related Questions