admin管理员组文章数量:1432399
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
3 Answers
Reset to default 3the 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:
- contains the value for fields that you specify.
- 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.
本文标签: javascriptHow to update existing document in firestore using set methodStack Overflow
版权声明:本文标题:javascript - How to update existing document in firestore using set method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745560072a2663421.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论