Posts

Showing posts with the label Google Cloud Firestore

Auto Generate Id For Document And Not Collection In Firestore

Answer : If you are using CollectionReference's add() method, it means that it: Adds a new document to this collection with the specified POJO as contents, assigning it a document ID automatically. If you want to get the document id that is generated and use it in your reference, then use DocumentReference's set() method: Overwrites the document referred to by this DocumentRefere Like in following lines of code: String id = db.collection("collection_name").document().getId(); db.collection("collection_name").document(id).set(object); Since you already know the id of the document, just call set() instead of add() . It will create the document if it doesn't already exist. This answer might be a little late but you can look at this code here which will generate a new document name: // Add a new document with a generated id. db.collection("cities").add({ name: "Tokyo", country: "Japan" }) .then...

Cloud Firestore: How Is Read Calculated?

Answer : Since you're reading 10 documents, so you'll be charged for 10 document reads. The number of read API calls you use is not relevant here. Also see: Firestore read/write pricing; does .limit(25) counts as 25 reads or one? Understanding Firestore Pricing Firestore Pricing - Does The Amount Of Documents In A Collection Matters? QuizApp - Firebase/ FireStore Reads I did a bunch of testing of this and can confirm that the Firebase Database tab uses an insane amount of reads. For me it incurred a 600+ read count every single time I opened it. If I clicked off that tab and back onto it, I would get another 600+ hit on read count. I monitored the usage of this using the GCP Usage for Firestore so I could avoid having that window open. This is an absurd cost, it has taken me into 100k+ reads by scrolling through it without realizing what was happening. You could even hit a million pretty easy if you were spending a lot of time in there doing something. In addi...

Can Firestore Update Multiple Documents Matching A Condition, Using One Query?

Answer : Updating a document in Cloud Firestore requires knowings its ID. Cloud Firestore does not support the equivalent of SQL's update queries. You will always have to do this in two steps: Run a query with your conditions to determine the document IDs Update the documents with individual updates, or with one or more batched writes. Note that you only need the document ID from step 1. So you could run a query that only returns the IDs. This is not possible in the client-side SDKs, but can be done through the REST API and Admin SDKs as shown here: How to get a list of document IDs in a collection Cloud Firestore? Frank's answer is actually a great one and does solve the issue. But for those in a hurry maybe this snippet might help you: const updateAllFromCollection = async (collectionName) => { const firebase = require('firebase-admin') const collection = firebase.firestore().collection(collectionName) const newDocumentBody = { ...

Add Field Separately To Firestore Document

Answer : Build a DocumentReference to the document you want to update, then use the update() method on the DocumentReference to indicate only the fields to be added or changed. Pass it an object with only properties that match the fields to add or change.

Cloud Firestore Rules On Subcollection

Answer : Honestly, I think you're okay with your structure and get call as-is. Here's why: If you're fetching a bunch of documents in a subcollection, Cloud Firestore is usually smart enough to cache values as needed. For example, if you were to ask to fetch all 200 items in "conversions/chat_abc/messages", Cloud Firestore would only perform that get operation once and re-use it for the entire batch operation. So you'll end up with 201 reads, and not 400. As a general philosophy, I'm not a fan of optimizing for pricing in your security rules. Yes, you can end up with one or two extra reads per operation, but it's probably not going to cause you trouble the same way, say, a poorly written Cloud Function might. Those are the areas where you're better off optimizing.

Angular 6 - Getting Download URL For Firebase Storage File After Uploading

Answer : You should add a finalize() to the pipe, something like: this.task.snapshotChanges().pipe( finalize(() => { this.downloadURL = this.ref.getDownloadURL(); // <-- Here the downloadURL is available. }) ).subscribe(); In the finalize() step, the downloadURL is available, so u can grab him from the ref asynchronously. --UPDATE You said you are using Angular 6, so I assume you are using the last version of firebase. They change getDownloadURL() to Observable from Task, So to get the actual URL you just have to subscribe. this.task.snapshotChanges().pipe( finalize(() => { this.ref.getDownloadURL().subscribe(url => { console.log(url); // <-- do what ever you want with the url.. }); }) ).subscribe();

Cloud Function To Export Firestore Backup Data. Using Firebase-admin Or @google-cloud/firestore?

Answer : The way you're accessing the admin client is correct as far as I can tell. const client = new admin.firestore.v1.FirestoreAdminClient({}); However, you probably won't get any TypeScript/intellisense help beyond this point since the Firestore library does not actually define detailed typings for v1 RPCs. Notice how they are declared with any types: https://github.com/googleapis/nodejs-firestore/blob/425bf3d3f5ecab66fcecf5373e8dd03b73bb46ad/types/firestore.d.ts#L1354-L1364 Here is an implementation I'm using that allows you to do whatever operations you need to do, based on the template provided by firebase here https://firebase.google.com/docs/firestore/solutions/schedule-export In my case I'm filtering out collections from firestore I don't want the scheduler to automatically backup const { Firestore } = require('@google-cloud/firestore') const firestore = new Firestore() const client = new Firestore.v1.FirestoreAdminClient() const bu...

Angular 5, NullInjectorError: No Provider For Service

Answer : You need to add TesteventService under providers under imports in your app.module.ts providers: [ TesteventService ] Annotate your service class with - @Injectable({ providedIn: 'root' }) The service itself is a class that the CLI generated and that's decorated with @Injectable() . By default, this decorator has a providedIn property, which creates a provider for the service. In this case, providedIn: 'root' specifies that Angular should provide the service in the root injector.