简体   繁体   English

Firebase Firestore,如何有效地更新对象数组中的多个集合

[英]Firebase Firestore, How to update multiple collection from array of objects efficiently

i have this firestore collection that needs to update according to the data within array of objects, at first this was not a problem.我有这个 firestore 集合需要根据对象数组中的数据进行更新,起初这不是问题。 but as the data grows.但随着数据的增长。 to update the data to firebase is we have to compare each id and then perform update to all of the data.要将数据更新为 firebase,我们必须比较每个 id,然后对所有数据执行更新。

here i have some array,这里我有一些数组,

let newCategoriesUpdate = [
{
    category_id: 100001,
    parent_category_id: 0,
    name: "Health",
    isActive: true,
    has_children: true,
  },
  {
    category_id: 100019,
    parent_category_id: 100001,
    name: "Medical Equipment",
    isActive: true,
    has_children: false,
  },
  {
    category_id: 100020,
    parent_category_id: 100001,
    name: "Laboratory",
    isActive: false,
    has_children: false,
  },
]

the list contains more than 200 objects which need to compare on each loop, which takes more time and memory.该列表包含 200 多个对象,需要在每个循环中进行比较,这需要更多时间和 memory。

Here's what i've implemented in firebase to update the collection from array of objects above这是我在 firebase 中实现的,用于从上面的对象数组更新集合


const handleUpdateCategories = () => {
    db.collection("category")
      .get()
      .then((snapshot) => {
        snapshot.forEach((docRef) => {
          let name = "My Category";
          if (docRef.data().name === name) {
            let categoryRef = docRef.id;
            db.collection("category")
              .doc(categoryRef)
              .collection("categoryList")
              .get()
              .then((snapshotCollection) => {

                // loop collection from firebase
                snapshotCollection.forEach((catListDocRef) => {
                  let categoryListRefId = catListDocRef.id;

                  // need to compare each loop in array
                  // loop array to update

                  newCategoriesUpdate.map((category) => {
                    if (
                      catListDocRef.data().categoryId === category.category_id
                    ) {
                      db.collection("category")
                        .doc(categoryRef)
                        .collection("categoryList")
                        .doc(categoryListRefId)
                        .set(
                          {
                            categoryId: category.category_id,
                            isActive: category.isActive,
                            categoryName: category.name,
                          },
                          { merge: true }
                        )
                        .then(() => {
                          console.log("UPDATE Success");
                        })
                        .catch((err) => {
                          console.log("ERR", err);
                        });
                    }
                  });
                });
              });
          }
        });
      });
  };

This method works, and in the console also shows the message "UPDATE Success" multiple times.此方法有效,并且在控制台中也多次显示消息“UPDATE Success”。

Is there a better alternative to update multiple collection from array of objects?有没有更好的选择来更新对象数组的多个集合?

This is wasteful:这是浪费:

db.collection("category")
  .get()
  .then((snapshot) => {
    snapshot.forEach((docRef) => {
      let name = "My Category";
      if (docRef.data().name === name) {
        ...

You're retrieving all documents from category and then only processing the ones that have "My Category" as their name.您从category检索所有文档,然后只处理名称为"My Category"的文档。 If there are documents with other names, you should use a query to retrieve only the ones you need to process:如果有其他名称的文档,您应该使用查询来仅检索您需要处理的文档:

db.collection("category")
  .where("name", "==", "My Category")
  .get()
  .then((snapshot) => {
    snapshot.forEach((docRef) => {
      ...

There may be more things, but this is the first that jumped out at me.可能还有更多事情,但这是第一个跳到我面前的。

This work can be done on the server side with a cloud function onWrite trigger listening to the Categories collection.这项工作可以在服务器端完成,使用云 function onWrite触发器侦听类别集合。

Also consider structuring your data more efficiently, can you store the child category in the parent so you know exactly which documents need to be updated?还要考虑更有效地构建数据,您能否将子类别存储在父类别中,以便您确切知道哪些文档需要更新?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firestore 集合到具有 Firebase v9 的对象数组 - Firestore collection to array of objects with Firebase v9 如何更新 flutter 中 firebase 火库中的所有集合 - How to update all collection in firebase firestore in flutter 如何在包含文档中对象的对象数组中搜索特定字段并在 firestore (firebase) (JS) 中更新它们 - How to search for specific fields in an array of objects containing objects in a document and update them in firestore (firebase) (JS) 如何有效地从 Firebase 数据库中获取数组? - How to get an array from Firebase database efficiently? React中如何获取Firebase 9中的多个Doc对象? 并使用其文档 ID 从 firestore 获取多个文档? - How to get multiple Doc objects in Firebase 9 in React? and get multiple docs from firestore using its doc id? 使用 admin sdk 和 Firebase 云函数从 Firestore 检索对象数组 - Retrieving an array of objects from Firestore with the admin sdk and Firebase cloud functions 如何从 Android 上的 Firebase Firestore 获取嵌套集合? - How to get nested collection from Firebase Firestore on Android? 如何从 Firebase 的集合中的 Firestore 文档中获取单个字段? - How to fetch a single Field from a Firestore document in a collection from Firebase? Firebase 'collection' 未从 'firebase/firestore' 导出 - Firebase 'collection' is not exported from 'firebase/firestore' 从 firebase firestore 检索后,如何根据字段对 firestore 集合文档进行排序 - how to sort firestore collection documents based on a field after retrieving from firebase firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM