Home > Web Front-end > JS Tutorial > body text

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

DDD
Release: 2024-10-25 06:56:29
Original
445 people have browsed it

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() }) })
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!