Can I Change In Firestore All Documents In Who Correspond To A Certain Condition Without Querying For All Of Them First?
I am building some sort of a social network. To save read operations, for every post, I store the details of the author's details with it (image and name). I am trying to create a cloud function that when a user edits their profile details, the function will change all the posts the user has wrote and update them accordingly (with the new name/image).
The problem I am facing (not much of a problem, just trying to be efficient), is how do I edit all the posts by that user? Do I have to query for all the posts by that user first and then loop through the results to change them? Or is there some method that would allow me to edit all documents that follow a certain condition?
This is my function right now, and I placed inside the hypothetical function I hope exists.
exports.updateUser = functions.firestore.document('communities_data/{community}/profiles/{profileID}').onUpdate((change, context) => {
// Retrieve the current and previous value
const data = change.after.data();
const previousData = change.before.data();
// We'll only update if the name has changed.
// This is crucial to prevent infinite loops.
if (data !== undefined && previousData !== undefined) {
if (data.name !== previousData.name || data.image !== previousData.image) {
//I am looking for something like this
return db.doc('communities_data/' + context.params.community + '/questions').where('author_ID', '==', context.params.profileID).set
} else {
return null;
}
} else {
return null;
}
});
Answer
With Cloud Firestore, there is no bulk update mechanism like SQL "update where". You have to query for the documents you want to update, iterate the results, and update them each individually.
Related Questions
- → .tsx webpack compile fails: Unexpected token <
- → Angular 2 bootstrap function gives error "Argument type AppComponent is not assignable to parameter type Type"
- → AngularJS Two binding in directive
- → How to fix "TS2349: Cannot invoke an expression whose type lacks a call signature"
- → Typescript point to function
- → Manage 301 Redirect when moving oldsite to AngularJS
- → Why is "this" null, in the link function within an Angular Directive?
- → ES6 equivalent of this Angular 2 typescript
- → Where to find example Visual Studio 2015 project(s) using typescript and React?
- → typescript definition for react-router v2.0 - error `has no exported member 'browserHistory'`
- → what version of javascript does typescript compile to?
- → How to configure Visual Studio 2015 to write JSX in ASP.NET 5 (RC) with intellisense
- → what's the different between ///<reference path="..."/> and import statement