Can we not query collections inside transactions?

对着背影说爱祢 提交于 2020-01-11 04:37:07

问题


Looking at https://firebase.google.com/docs/reference/js/firebase.firestore.Transaction I see four methods: delete, set, get, update.

I was about to construct a lovely little collection query and pass it to .get, but I see the docs say that .get "Reads the document referenced by the provided DocumentReference."

It appears this means we cannot get a collection, or query a collection, with a Transaction object.

I could query those with the query's .get() method instead of the transaction's .get() method, but if the collection changes out from under me, the transaction will end up in an inconsistent state without retrying.

It seems I am hitting a wall here. Is my understanding correct? Can we not access collections inside a transaction in a consistent way?


回答1:


Your understanding is correct. You have to identify the individual documents that you would like to ensure will not change before your transaction is complete. If those documents come from a collection query ahead of time, fine. But think for a moment about how not-scalable it would be if you had to track every document in a (very large) collection in order to complete your transaction.




回答2:


I don't understand. is this code ok or not? I don't see other way to do it :

admin.firestore().runTransaction(async t => {
const query = admin
    .firestore()
    .collection('coll')
    .where('x', '==', true);
    const results = await query.get(); //this line is crucial
results.forEach(r => t.update(r, ...)});

? It's said whenever anyone writes to collection the transaction runs again



来源:https://stackoverflow.com/questions/50071700/can-we-not-query-collections-inside-transactions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!