[英]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.