Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

javascript - Firestore transaction, max documents

It will come at some point that perhaps I will have to update more than 500 documents, but first I have to read and update all the data to be fine. How would you do this with transactions?

I did something similar with _.chunk with batch. But this time I need a transaction but I wouldn't know how to do.

transaction:

    if (previousValue.Name !== newValue.Name || previousValue.Image !== newValue.Image) {

        const chatRoomQuery = db.collection(chatsCollection).where(userIdsProperty, 'array-contains', userId);

        const transactions = _.chunk(chatRoomQuery, maxSize) => {
                return db.runTransaction(transaction => {
                    return transaction.getAll(chatRoomQuery).then(docs => {
                        docs.forEach(doc => {
                            let chatRoom = doc.data();

                            let oldUser = {
                                Id: previousValue.Id,
                                Name: previousValue.Name,
                                Image: previousValue.Image
                            };

                            let newUser = {
                                Id: newValue.Id,
                                Name: newValue.Name,
                                Image: newValue.Image
                            };

                            let index = chatRoom.Users.indexOf(oldUser);
                            if (index > -1) {
                                chatRoom.Users.splice(index, 1, newUser);
                                transaction.update(doc.ref, chatRoom)
                            }
                        })
                    })
                })
            });

        await Promise.all(transactions);
    }

I think I have a syntax error not getting it right. I leave a screenshot.

enter image description here

question from:https://stackoverflow.com/questions/65644239/firestore-transaction-max-documents

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...