This is my Firestore structure:
Products(root) -> product(document) -> prices(subCollection)
The prices collection is in each product document.
According to Algolia Firebase extension instructions, this is how to define the field for indexing. But there is no example for sub-collection.
The current field that I have set for indexing are in the product(document) but I also want to select certain fields inside the prices(subCollection).
Is there a way to tell the Algolia Firebase extension to index certain field inside the prices sub-collection as well?
Related
[![I want a booking system like as collection->appoinment_user_id->user_id(Who want to book)->date and time.][2]
There is no API to get all subcollections under a document in the client-side SDKs for Firestore. The common pattern is to give collections known names, which you can type in your code, and only use generated IDs for documents.
So in your scenario you'd add a users collection under the 0Sj3n.... document, and then under that you'd add documents for the user IDs again.
Also see:
How to list subcollections in a Cloud Firestore document
How to get names list of collection in firestore?
My firebase database has a user profile collection, where all the documents are UIDs, each document has a field called subscriptions which is an array of strings, each string being other UIDs.
I have a second collection for host content, where each document contains fields such as imageurl and caption, essentially posts that user has made.
Is there any way I can use the streamprovider on the host content collection to only show the posts by the UID the current user has a subscription for?
There are no SQL-like joins in Firestore, so your only option here (without changing any data) is to add a new listener on each document in hostconent that matches what's in the profile. There is no way to do this with a single query.
If you want to do this with a single query, you should have a field for each document in hostcontent that identifies which profile it's associated with. Then you can make a single query using that field as a filter to find only the documents for a profile.
I just watched the playlist of Get to know Cloud Firestore, and I just learned that every field of the document is indexed.
My question is, is there way for a certain fields to be excluded on by Firestore indexing? Something like fields that I am pretty sure that I will not be using as a query lookup.
Thanks.
As you correctly found, Firestore automatically indexes all individual fields of the documents in the collection. You can exclude certain fields from the single field indexes panel in the Firebase console.
From there:
Cloud Firestore creates the indexes defined by your automatic index settings for each field you add, enabling most simple queries by default. You can add exemptions to manually set how a specific field is indexed.
From there, you can enter the collection (or collection group), and the field name, and then select which indexes (ascending, descending, arrays) get auto-created or not.
So few day ago I moved my apps posts to cloud firestore from realtime database because of the more flexable querying that firestore apparently has. I have been modifying my code to work with firestore as the new database. Now I ran into a problem. How can I retrieve every document within "Posts" collection that has "Likes" collection that contains a specifically named document? I don't know if that makes sense so here's an image showcasing the problem:
I had a working query for firebase realtime database but can't figure out how to do it with firestore. Is it possible or do I have to change the structure of my database? If I do have to change the structure, what would be the most convenient way of getting the same result?
This is currently not possible with collection group queries. You might be tempted to put a filter on the query for FieldPath.documentId() == 'your-doucment-id', but that just isn't supported. FieldPath.documentId() is actually a token that refers to to fully unique document id, which includes the entire path of the document.
The workaround is to store the document ID as a field in the document, and filter on the contents of that field instead. So, if the document had a field called id with the ID of the document, you could query for collectionGroup("Likes").whereEquals('id', 'your-document-id').
I'm using Firebase Cloud Firestore and can't figure out how to access documents without knowing the specific path. The database structure is users/{user id}/favourites/{favourite id}. There are no fields in users/{user id} only subcollections. Knowing the user id, i can get the favourites for the user, but I can't get a list of users to get everyone's favourites. Here is the code I am trying (Java admin SDK):
db.collection("users").get().get()
which results in an empty Iterable with no DocumentSnapshots.
How can I get a list of the most popular favourites?
EDIT: I've discovered I can get a list of users with no fields if I add a field. Even if I delete it later, it still appears as a document in the collection.
EDIT2: I've discovered that I can create an empty document, so I'm just doing that for now. As a one-off, I can get a list of all users from firebase auth and look up which ones have a favourites collection and just set those to empty documents.