javascript - How to update existing document in firestore using set method - Stack Overflow

I have a firestore document that I want to update. Here's my code -admin.firestore().collection(&

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 ining 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 -

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

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 ining 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 -

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

Share Improve this question edited Apr 4, 2021 at 16:56 user3399180 asked Apr 4, 2021 at 16:16 user3399180user3399180 5722 gold badges9 silver badges22 bronze badges 4
  • The code looks fine at first glance. Can you edit your question to show screenshots of the document before and after this code runs? – Frank van Puffelen Commented Apr 4, 2021 at 16:25
  • Absolutely. I should I have done it before. – user3399180 Commented Apr 4, 2021 at 16:40
  • Why aren't you using update() to update the data ? – Kundan Commented Apr 4, 2021 at 17:58
  • 1 Because that would fail if the collections doesn't exists. I want to create a new collection if it has not been created. And update the collection if it already exists. – user3399180 Commented Apr 4, 2021 at 18:04
Add a ment  | 

3 Answers 3

Reset to default 3

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.

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 binations, you'll need to do so in an Array field. You can then either add the new bination with a transaction again, or (if the bination of username+email has to be unique) you can use the atomic array-union operation

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745560072a4633055.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信