Ionic Increment Firestore Value - ionic-framework

I was wondering if there was a way to increment a value in Firestore with Ionic. For example, a like button and when clicked a field value 'likes' adds +1 onto the existing value.
I saw examples with 'transactions' where this is done but it was with the Realtime Database rather than the Cloud Firestore. I looked further and could not find any documentation regarding Firestore.
Is there anyway to pull this off without pulling the existing value from the snapshot first?
Thanks,
Troy

There are no server-side increment operators in Firestore. To increase an existing value, your app will first need to read that value.
For an example of using a transaction in Firestore, see updating data with transactions in the Firestore documentation. This literally shows how to add 1 to a field.

Related

request only required data from firebase realtime database flutter

Let's say I have millions of records like this. I am showing this information in GridView. It will take a lot of time to download the whole database and then display results. How do I download only 50 records at a time and when I am about to reach the end of list download another 50 and so on like Google image search results? Is it possible?
What you want is called pagination. I don't know about real-time databases but I do know firebase firestore provides you with tools to do that. You could check this link out for firebase firestore.
https://www.youtube.com/watch?v=poqTHxtDXwU&feature=emb_logo
You can write code to fetch specific data then add a scrollController to page and a code to fetch next data whenever your scrollController reaches bottom of page.
For it you have to learn using scrollController.
https://youtu.be/pGmHYXC5MeU

Pricing for Firestore when using StreamBuilder

When using StreamBuilder in Flutter to read changes in a document in Firestore, how am I charged?
Am I charged for each time I listen whether there is any change or not multiplied by number of users or any different pricing is used.
After doing some research and reading documentation, I am ready to answer my own question.
If we are using StreamBuilder to fetch Snapshot from Firestore then we are charged for read only when the data is updated and are NOT charged continuously.
Hope it helps someone else.

addSnapshotListener swift firestore behaviour

I have a document in Cloud firestore to which I listen for updates. It has 2 fields, it has a field description and a field for a picture. The picture is approximately 0.2 mb and description is a few words. I wanted to know what would happen if I made changes to the description in the document, I wanted to know if addSnapshotListener actually downloads a fresh new copy of the document or just the field that has been changed.
I indeed see, by looking at how much data is being downloaded in Xcode, a new fresh copy of the document is downloaded.
This is not efficient at all, since the picture field is rarely changed, only the description might change in my application.
Is there a way to optimize this?
Is there a way to optimize this?
Yes! Don't do that.
Firestore (and the realtime database) is not intended to store images or large datasets per field.
You should explore Storage and keep a reference (url) to the item stored in storage in your Firebase.
Cloud Storage is built for app developers who need to store and serve
user-generated content, such as photos or videos.
By leveraging storage if you need to update or change a field in Firestore, you're only working with a small amount of data instead of an entire image worth.
To answer the question; if you read a document from Firebase, it does read the Document and it's child data.
Here's a link to the Storage Docs which shows how to capture a reference to the item uploaded to storage.
https://firebase.google.com/docs/storage/ios/upload-files
If you want to automatically sync the images to all clients and have them available offline, just put them in a separate document.
// Store your small, frequently changing text here:
db.collection('users').doc(userId).set({email: vince#example.com})
// Store your image here:
db.collection('user_profile_pic').doc(userId).set({data: <imagedata>})

Google Cloud Firestore console reading of all documents and charges

I am new to Firestore so I have a Profiles and Users collections. In the Cloud Firestore Console when I click on Database > Firestore > Data tab > Profiles or > Users the console is reading ALL the documents in each collection. These reads are counted in the Usage tab. So my question is if I have lets say 500K documents in the Profiles collection and clicked on Data I will be charged for reading 500K docs just to view the first 25 docs only. Just clicking on Data tab the console reads all the docs of the first Collection.
I tried using the filter but to use it you will have to click on the Collection and read all the docs first then you can edit the filter.
Is this the way it works or is it my misunderstanding?
I faced the same confusion a while ago and upon digging down to the issue I learnt that all the data which gets loaded in the 'Data' tab of Firestore page does count towards the overall Firestore usage.
However, I was concerned with the same question as yours thus I contacted Firebase support. They reverted back confirming the first instinct of mine(Document reads in 'Data' tab does count) BUT initially it reads only the first 300 documents of ANY selected collection, so even if your collection has over 1 million docs, it will still load only the first 300 documents.
They suggested a way around it until the Firebase team finds a legit solution
Bookmarking the Usage tab of the Firestore page. (So you basically 'Skip' the Data Tab and the useless 300 reads)
Adding a dummy collection in a certain way that ensures it is the first collection(alphabetically) which gets loaded by default on the Firestore page.
So my question is if I have lets say 500K documents in the Profiles collection and clicked on Data I will be charged for reading 500K docs
Absolutely not! You are only charged with read operations for the documents that are apart of the query. I don't know the exact number at which the first query is limited but the data is loaded in smaller chunks, with other words a pagination system is implemented. So once you scroll down other elements are loaded and so on.
If you intend to create an app that uses Firestore as backend, please also note that, offline persistence:
For Android and iOS, offline persistence is enabled by default.
For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method.
For the web, offline persistence is an experimental feature that is supported only by the Chrome, Safari, and Firefox web browsers. Also, if a user opens multiple browser tabs that point to the same Cloud Firestore database, and offline persistence is enabled, Cloud Firestore will work correctly only in the first tab.
This means, that once you get a document(s) from the Firebase servers, you are not charched anymore since the results are coming from the local cache.
I just have a similar Question like and I find it redundant to post it as a Question so I'm posting it as asnswer,sorry for that,
Question:
this.db.collection(this.collectionName.usersCollection)
.doc(currentUserId).collection(this.collectionName.friendsCollection)
.where("friendID","==",id)
.get().then(snapshot =>{
if(snapshot.empty)
{
this.db.collection(this.collectionName.chatsCollection).add({
user1 : currentUserId,
user2 : id
}).then(docRef =>{
docId = docRef.id;
this.db.collection(this.collectionName.usersCollection)
.doc(currentUserId)
.collection(this.collectionName.friendsCollection).doc(docId)
.set({
friendID: id,
})
this.db.collection(this.collectionName.usersCollection).doc(id)
.collection(this.collectionName.friendsCollection).doc(docId)
.set({
friendID:currentUserId,
})
resolve(docRef.id);
})
}
else{
snapshot.forEach(document =>{
// console.log("friend id",document.data().friendID, " docid: ",document.data().docId);
resolve(document.id);
})
}
})
so here I'm writing and reading the docId ,so will it affect the counts? I cannot have a check properly because with this many other operations are happening.
Thanks .

MongoDB Stitch vs Firebase Firestore AND/OR operations

I mainly want to compare using firestore vs mongoDB Stitch queries.
I'm building an app (angular 5 + ios + android). The app is mainly of 2 views profile and feeds.
The profile contains an array of tags(all user post tags) and a timeline of his posts.
every post got its own array of tags and timestamp. so I can filter them.
filters are either AND / OR operation.
In firestore I plan to use object maps so the AND operation can work
In firestore in order to create OR operation, I need to loop on all the possibilities fetch them individually, aggregate the data and remove duplicates. This can be done on the frontend or inside a cloud function.
I also need to run analytics to know the trending tags every day.
If a user got 100s of posts. How will firestore compare to mongoDB Stitch regarding?
Speed
Cost