Firebase Batch Writes - firebase

I have a flutter app where I'm fetching a List of names based on a certain condition from a top-level collection called /students and displaying it one of my screens.
Now I want the user to select these names, with a CheckBoxListTile and then confirm to add these selected names into a deeply nested subcollection of /departments/cse/2017/6 B 17/students/, how do I accomplish this in an efficient way?
I basically want to write all the documents at once into the sub-collection from my List <Student> selectedStudents while maintaining their firebase userId as the docuemntId in that collection of students.

In Firestore writes are counted in a way that each document will count as one write.
therefor the objective of not hitting a limit will not happen.

Related

Filter Firestore Collection by Document Reference-Console

How does one filter a Firestore collection by a document reference in the web console? For example, I have a collection that references another collection document called challenges.
But no matter what combination I use to try to filter that collection by the challenge ID it returns no results. I need to be able to do this so I can look at all of the documents in this collection that reference back to that particular challenge reference.
I am assuming this is possible in the console, but maybe it is not?

How to turn off automatic indexes in firestore for subcollection

I don't want to have automatic indexes created by firestore because I need to remove and add every five minutes 50-100 documents (each doc has +/-60 fields) to my subcollection. This causes of big volume for "Cloud Firestore Index Write Ops" (300k / day for only one user) and Cloud Storage. I don't need to sort, filtering that documents so I suppose I can turn off automatic indexes, right?
I know that I can add exemptions for fields, but I don't know how can I use it for documents in subcollections. What should I pass in Collection ID and Field path if the path for documents is like:
mainCollectionName/{id}/subcollectionName/{document=**}
and when should I select a collection checkbox and when collection group checkbox?
Unfortunately, it's not possible to disable indexes or create exemptions for documents to be indexed. As clarified in this similar post here, this cannot be achieved and there is even a limit of 200 exemptions of fields that can be done - you can check the limits here.
For your case, indeed, you would have to exempt the fields individually and besides that, to create the exemption, to set the collection you use its id and not the path. So, you would only need to set in the Collection ID field the subcollectionName and then the field to be exempted.
In addition to this, feel free to raise a Feature Request in Google's Issue Tracker, so they can check about implementing an exemption of documents in the future.

Unable to delete an item from multiple collections in firebase

I have two collection in firebase, say A and B. When I add an item to one of the collections, say A, it automatically adds to another collection B. But when I delete an item from collection A, it remains in the other collection B.
Shot from firebase console
When I delete an item from collection A, it remains in the other collection B.
I am unable to figure out how to make the items deleted in both collections?
Please help.
This is no relational DB. Cascadate updates/deletes are possible in relational DBs like Sql, Postgresql, Firebird... In No-SQL You do not have any relations one document to another. What' more it is desirable to have some data cloned in two or more documents rather than creating a separate doc for it.
But maybe You can achieve what You want by using reference type? I sow it once or twice, it might be a good idea to check it out. But be the description it is only a field with path/reference, no magic here.
By the official doc:
If you want to delete documents in subcollections when deleting a parent document, you must do so manually, as shown in Delete Collections.
So You either want to rethink The DB model or have to manualy track each wlement You want to delete.
You have two ways to do that:
From client side use batch write (more here)
DocumentReference doc_1_reference = firestore.collection("messages").document(doc_1_uid);
DocumentReference doc_2_reference = firestore.collection("messages").document(doc_2_uid);
final batchWrite = firestore.batch();
batchWrite.delete(doc_1_reference);
batchWrite.delete(doc_2_reference);
await batchWrite.commit();
Using Cloud functions by adding a delete trigger to the documents, and perform the deletion of the other document when triggered. Given you have away to know which document you're supposed to delete which is not difficult to do (read about it here)

Firestore: Getting documents sorted by a numeric field contained in the two sub-levels documents

My firm asked me to get all the documents from the root collection, users. They must be sorted according to the value of a numeric field, titled amount, present in a first sub-level document and also in a second sub-level document.
I don't know how to fulfill this aim easily with Firestore NoSQL queries. What would you recommend to me?
Data structure
Here it is:
users
user_1 (document to get and sort)
user_2 (document to get and sort)
Each of these two users is structured like this:
collection_1
document_of_first_level
contains this field: amount
and contains also this collection: collection_2
document_of_second_level
contains this field: amount
So I should get a list containing user_1 and user_2. This is a sorted list. It's sorted according to the two fields both named amount, which are contained under user_1, but also under user_2. These both fields are nested.
They must be sorted according to the value of a numeric field, titled amount, present in a first sub-level document and also in a second sub-level document.
As also #DougStevenson mentioned in his comment, you cannot achieve this with Cloud Firestore. There is no way to get documents from a subcollection (level 1) and another subcollection (level 2) in a single query. Firestore doesn't support queries across different subcollections in one go. Queries in Firestore are shallow, which means they only get items from the collection that the query is run against. A single query may only use properties of documents in a single collection.
So the most simple solution I can think of, would be to query the database twice, once to get the documents within subcollection (level 1) and second to get documents within subcollection (level 2) and then compare them client side.
The idea from your comment is a solution since adding a property under the level 1 document will allow you to query the database just once. In Firestore, you can simply chain multiple where() functions in a single query.

Correlating Firestore indexes to slugs in a webapp

I'd like my web app router slugs to correspond to my Firestore documents data.
For example:
www.mysite.com/restaurants/burger-king
/restaurants <- Firestore Collection
/restaurants/mcdonalds <- Firestore Document
/restaurants/burger-king <- Firestore Document
This is easy enough, as I can assign the name as a slug-friendly UID in Firestore. The difficulty arises with CRUD functionality. I need to be able to rename my item titles, but Firestore does not permit you to rename indexes, which is the issue I'm facing.
One SO solution I saw was to delete the old record and creates a new one at the updated index. That's problematic for me, because sub-collections would be hard to transfer from the client side.
Are there more elegant solutions?
You don't have to identify a document by its ID. If you're able to ensure uniqueness of a document field value, you could instead query a collection for an ID value in a known field, then use the results of that query to satisfy your REST API. Then, you can change the value of that document field as often as you want, in order to satisfy required changes to the public API.

Resources