Home  >  Article  >  Web Front-end  >  How can I efficiently update arrays within Firestore documents without overwriting the whole array?

How can I efficiently update arrays within Firestore documents without overwriting the whole array?

DDD
DDDOriginal
2024-10-25 06:56:29446browse

How can I efficiently update arrays within Firestore documents without overwriting the whole array?

Updating Arrays with Firestore

Firestore offers a straightforward method for updating arrays within documents, eliminating the need for overwriting the entire array.

The following functions enable array updates:

  • arrayUnion(): Adds elements to an array, ensuring that duplicates are not included.
  • arrayRemove(): Removes all instances of specified elements from an array.

Example code:

// Array union
firebase.firestore()
.collection('proprietary')
.doc(docID)
.update({ sharedWith: firebase.firestore.FieldValue.arrayUnion({ who: "[email protected]", when: new Date() }) })

// Array remove
firebase.firestore()
.collection('proprietary')
.doc(docID)
.update({ sharedWith: firebase.firestore.FieldValue.arrayRemove({ who: "[email protected]", when: new Date() }) })

These functions provide a simple and efficient way to manage arrays in Firestore, allowing developers to make targeted updates without affecting the entire array.

The above is the detailed content of How can I efficiently update arrays within Firestore documents without overwriting the whole array?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn