Displaying Sum of Each Element in Firestore (Flutter) - flutter

I have this this collection in Firestore where each document has 'count' property, and it is int of 1. I would want to have a function that adds that count property of 1 to total property somewhere in Firebase, I presume it would be another collection that would just calculate the sum as they are added 1 by 1. I'm at a bit of a loss how I would go around doing that.
The collection is displayed in a regular Stream and each element is in a List Tile. So what I'm after is to get a function that would be called somewhere that would add that '1' to the let's say collection called 'Total' and it would calculate whenever that function is executed and add that 1 to total. Also, how would I display that 'Total' collection somewhere else on another page as a string perhaps?
I attached a general image how my Firestore looks like just to give you guys a general picture.
I don't think code is necessary here, since as I said, it is just a regular List Tile and Stream of the 'Names' collection, but let me know if you need more context. Thank you!

In Cloud Firestore in order to increase an integer property, you would need first to iterate through the documents, by reading stored data and subsequently perform an update to change the value of this specific field in the corresponding "Total" collection.
To display the collection using flutter, you can take a look at StreamBuilder which can listen to the document changes and display appropriately, I also found this Medium article which might help.

Related

There is a doc in firestore that has a ID_number field when created. If I want to create another doc, how do I make its ID equal to the previous ID+1?

I'm using Flutter and Firestore, and I want to be able to create documents with an assigned ID field inside them, but I don't know how to make that the new document IDs Field is equal to the last document ID field number + 1.
For Example, if I have this field inside a document, I want to make the next document's correlativeNumber equal to this one + 1 = 87
The best option that you have is to store the last correlativeNumber into a document. Each time a new document is added, increment that number by 1. In this way, you can always know which number was used previously.
But there is something that should take into consideration. When it comes to document IDs, according to the official documentation:
Do not use monotonically increasing document IDs such as:
Customer1, Customer2, Customer3, ...
Product 1, Product 2, Product 3, ...
Such sequential IDs can lead to hotspots that impact latency.
So it's best to use the Firestore's built-in identifiers, which are by definition completely unique.

Firestore check if array in specific document contains a value

I would like to check if the array "devices" which is in the document "SOjTxEjMwb71isiiPnR8" contains the value "AE0EBAFA-1560-404B-A3DA". I just want to know this specific array in the specific document. Because it can be that the same value in the same array occurs in another document. How do I have to do this in Swift? Thanks a lot for your help.
You're looking for the array-contains operations, which in Swift looks like this:
devicesRefRef
.whereField("devices", arrayContains: "AE0EBAFA-1560-404B-A3DA")
You can then get the results once or listen for realtime updates. You'll note that all links I provide are to the relevant sections of the documentation, so I recommend spending some time there.

How to increment individual element in an array in firestore for flutter

This is how my data look like.
I'm able to use FieldValue.increment(1) to update individual fields but is there a way to use FieldValue.increment(1) for specific individual elements in the array? Thanks in advance!
I tried using the following code:
firestore.collection('test').doc('I1raMaJArb1sWXWqQErE')
.update({
'rating.0': FieldValue.increment(1),
});
But the whole rating became empty as seen
You can't use FieldValue.increment() if the field is part of the array. The value of the field inside the array is fixed. The best way to update or edit the field that part of an array is to read the entire document, modify the data in memory and update the field back into the document.

Is there a method in Firestore that could fetch a document and its adjacent documents?

In Firestore, we need to use something like startAt or endAt with limit to get either previous or preceding data from a given point in the list of documents within a collection.
We need to fetch twice to get adjacent documents (once for the data before and once for the data after) then we need to combine them in the list which could be error prone
I was wondering if there is a way to get the corresponding document with its adjacent documents in Flutter Firestore? Something like .getDocumentWithAdjacent
Firestore.instance.collection('restaurants').document(docId)
.getDocumentWithAdjacent(previousCount: 3, afterCount: 6); // will fetch 10 documents
The command above will fetch 10 documents: 3 previous documents, the document itself, and 6 preceding documents.
Just further explanation & use cases:
Just like Instagram app for example, the user can tap on a post to see its detail. Then the user can scroll up and down to see the adjacent post details easily because when the user tap on a post, some of the data before and after that post also being loaded.
Another example: A restaurant search app that shows a list of restaurant in an area.
When a user tap somewhere in the middle of the list of restaurant, then the restaurant details is being displayed, but the user want to scroll (either horizontally or vertically) to see the adjacent restaurant in the list so we need to load the data for that also.
Thank you
Firestore does not have a concept of "adjacent" documents in the way that you describe.
What you can do instead is make two queries. One for documents greater than a given document, then another for document less than the given.

Mongo query for number of items in a sub collection

This seems like it should be very simple but I can't get it to work. I want to select all documents A where there are one or more B elements in a sub collection.
Like if a Store document had a collection of Employees. I just want to find Stores with 1 or more Employees in it.
I tried something like:
{Store.Employees:{$size:{$ne:0}}}
or
{Store.Employees:{$size:{$gt:0}}}
Just can't get it to work.
This isn't supported. You basically only can get documents in which array size is equal to the value. Range searches you can't do.
What people normally do is that they cache array length in a separate field in the same document. Then they index that field and make very efficient queries.
Of course, this requires a little bit more work from you (not forgetting to keep that length field current).