admin管理员组文章数量:1401143
I'm using FieldValue.delete() to remove a field from a map instide a firestore document, but the key is still on the map. I want to remove the key, not just set an empty object as the value.
// Get the FieldValue object
var FieldValue = require('firebase-admin').firestore.FieldValue;
// Create a document reference
var exampleDocRef = db.collection('myDocumentType').doc('exampleDoc');
// Remove the key value pair from the map
var removeKeyValuePairInMap = exampleDocRef.update({
myMap:{ myKey:FieldValue.delete()}
});
I'm using FieldValue.delete() to remove a field from a map instide a firestore document, but the key is still on the map. I want to remove the key, not just set an empty object as the value.
// Get the FieldValue object
var FieldValue = require('firebase-admin').firestore.FieldValue;
// Create a document reference
var exampleDocRef = db.collection('myDocumentType').doc('exampleDoc');
// Remove the key value pair from the map
var removeKeyValuePairInMap = exampleDocRef.update({
myMap:{ myKey:FieldValue.delete()}
});
Share
Improve this question
edited Dec 22, 2021 at 16:28
Super Kai - Kazuya Ito
1
asked Feb 17, 2018 at 6:48
TimCroweTimCrowe
1162 silver badges6 bronze badges
5
- Please supply some sample code, including the FieldValue.delete() line and the line where you get the FieldValue object – Jason Berryman Commented Feb 17, 2018 at 20:36
- Just updated the post – TimCrowe Commented Feb 17, 2018 at 23:44
-
I just created a document
/cities/BJ
with 2 fields (one of themcapital
), ran your code and thecapital
field disappeared from the console. What is the issue that you're having? What version offirebase-admin
are you running? I tested with 5.9.0 – Jason Berryman Commented Feb 18, 2018 at 12:04 - I just updated the issue, I wasn't accurately describing the issue before. – TimCrowe Commented Jul 30, 2018 at 20:04
- This worked for me: stackoverflow./a/47555348/5532513 – Yossi Commented Feb 5, 2019 at 13:04
4 Answers
Reset to default 3One way to delete a key/value pair is to run a get() on the exampleDocRef and then delete it like you would any regular object's key/value pair and then update the doc after the deletion.
exampleDocRef.get().then(doc => {
const data = doc.data().myMap //gets it into something you can traverse
with dot notation
delete data[myKey]; //deletes key/value pair
exampleDocRef.update({ myMap: data}) //now just updating myMap with
itself minus the key/value pair you deleted
});
Ok, an edit to my previous answer above, here's a better way using your variables:
exampleDocRef.update({
myMap.myKey: FieldValue.delete()
})
You just have to make sure you traverse down to the key you want to delete with dot notation and call the delete() method. This deletes both key and value from the object.
This is how you would do it in the client/browser
firebase.firestore().collection('yourCollection').doc('docID').update({
"objOne.ObjTwoIfItExists": firebase.firestore.FieldValue.delete()
})
This is how you would do it in a cloud function
admin.collection('yourCollection').doc('docID').update({
"objOne.ObjTwoIfItExists": admin.firestore.FieldValue.delete()
})
Using dot notation in the key allows you to access nested objects.
i was facing the same problem as this. In my case, the variable i used for the object key was not actually used. But somehow the data from that variable was still extracted. Honestly i'm not even sure what went on. But this works for me.
To use a variable as key inside an object just use the square brackets.
// Firestore document
exampleDoc ={
id001: someData,
id002: someOtherData,
id003: someOtherOtherData,
}
// Get the FieldValue object
var FieldValue = require('firebase-admin').firestore.FieldValue;
// Create a document reference
var exampleDocRef = db.collection('myDocumentType').doc('exampleDoc');
// Create a variable to use as object key for the field u want to delete
var fieldToDelete = 'id001'
// Remove field from firestore (key & value)
var removeKeyValuePairInMap = exampleDocRef.update({
[fieldToDelete]: FieldValue.delete()
});
For firebase v9+, this works for me
import { deleteField, updateDoc, doc } from 'firebase/firestore';
updateDoc(doc(db, 'my-collection', 'item-id'), {
myField: deleteField(),
});
本文标签: javascriptFieldValuedelete() does not remove field key in firestore documentStack Overflow
版权声明:本文标题:javascript - FieldValue.delete() does not remove field key in firestore document - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744201185a2594968.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论