I have a question. I have an app that allows users to save posts by clicking an icon. Posts are saved in Firebase inside a collection called "saved", this collection is inside another collection called "posts". This is a saved sample post:
Here is an example of an unsaved post:
The question is, how do I retrieve only the saved posts?
Any sample code you write can help me, or even suggest an idea.
you can achieve your goal by multiple ways, but I think since you are trying to get all collections in your database that is names saved, consider trying Collection Group it would help get all documents inside any saved collection in your database, for more information you can check:
https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query
Related
I have a few music albums - basically just files in folders - that I want to upload to Firebase Storage.
One would usually run a function after a file has been uploaded to create a Document containing the metadata about the Song but that's where Im stuck.
I can get most infos I need by reading the Tracks ID3 Tags but in a NoSql Database I think im supposed to not only create a Document for the Track but also a Document for each album with an array of all tracks - or at least an array with all track ids.
But when or how do I create the Album Document? Another example is the Album Cover.. I want to save the Url inside the Track Document as well as in the corresponding Album but that means that the Artwork is the first thing I need to upload because I can't add an URL because it doesn't exist yet.
I feel like I have to get this right before I start because updating everything afterwards is a pain.
Is using upload functions really the way to go here or is there really a tool or another way im missing.
thank you very much
You mentioned Firebase Storage wich is a just a cover for Cloud Storage and it's a obejct managment system not a Database, however I think you are refering to Firebase Firestore.
On firestore since as you mentioned is a NoSQL DB and the schema structure your Db should have, There no correct way to do this and will defitetly depend on each specific use case. However you can take a look at this docs where it's expalined how to arquitecture your schema thinking from a SQL to a NoSQL format.
Among other information the main pointsa are:
In general, you can treat documents as lightweight JSON records
You have complete freedom over what fields you put in each document
After you create the first document in a collection, the collection exists. If you delete all of the documents in a collection, it no longer exists.
You can use sub collections inside of collections
Deleting a document does not delete its subcollections!
And finally to have an idea on how to structure the information, you can take a look at this repo where "NoSQL-Spotify by Luke Halley" explains a NoSQL schema based on spotify so I think it shoudl fit your need or at least give you a starting point.
I have read that it is recommended to store data in a store or a context due to the fact that data in the store can be used in any component.
For example in an e-commerce app. I have seen the cart icon which displays the number of items using the Redux Store or Hooks Context to store those numbers and also when click on the cart icon, the items display on the cart page is also using Redux Store or Hooks Context.
My question is using Amazon.com as an example, there are 12 million of products and when users logged out and log in again, they are still able to access to products in their carts. So in this case, is the Redux Store or Hooks Context relevant? And if so, which part? Of course, using Redux Store or Hooks Context to store user profile and authentication is understandable.
Thanks a lot
Your first line essentially answers the question. In order to have persistent cart data it must be stored in a database that has to be read from each time you need it. You save the number of reads by storing it locally.
Without redux you would have to read from the database each time you wanted to know what was in the cart.
With redux you read it once and access it from any component.
I have a Firestore data structure and a document where all my followers can see the recentPosts of mine by querying the collection of documents based on the users field of the document where querying users name is present just like below.
my question is how to share a post of others to my followers, currently i am duplicating the shared post to my recentPostsand my seperate Collection of posts documents, but what if a user deletes the post and the post was shared by million users? i have to delete all the shared posts, is there a better solution?
Given your choice in data model, having to delete the duplicated posts is pretty much the normal solution. I also don't see this as problematic, given that:
You've already written the duplicate post to all these followers to begin with, so the delete is just another write.
Deletes and other writes are relatively uncommon in most applications. If not, consider whether you should really be duplicating the data to all followers.
You could choose to implement this with a global list of deleted posts, that each client then reads. But at that point you're making the code that reads data more complex to prevent writes, which is typically not the best approach when using NoSQL databases.
I'm setting up a Firestore database and am playing around with structuring it. Is there a way to populate and change it quickly without having to add/change fields manually every single time?
Two example things I am looking to do are:
1) Populate collections with documents that have predetermined fields. Currently I have to add the fields manually every single time.
2) Edit the fields en masse for all documents within a collection (e.g. change the name of a field, delete a field entirely, add a new field)
The Firebase console doesn't seem to provide these tools, would my best bet be to write a separate app specifically for this purpose?
Since such bulk uploads and bulk edits are not part of the console, you'll have to build something yourself indeed.
A good place to start would be the Cloud Firestore API, which allows adding and updating documents in the database.
Is it possible to create multiple documents against a collection using Cloud Firestore in the same transaction?
I'm looking at the documentation on batched writes. Unless I'm mistaken (I'm new to Firebase, so could be the case) these examples are meant to demonstrate 'batched writes' but the examples only show a single field being updated.
Yes. Batched writes can work for both updating a single field on multiple documents, or creating a bunch of documents at once.
Follow this link to see a similar case i had which i solved. It talked about a collection with its documents being automatically created when a document is created. You will get an idea on how to solve your issue from it.
The link is thus: When collection is created, the documents are added to it at once
I hope it helps.