Firestore: find document in nested collections - firebase

I have these collections:
users/portfolios/rents/events
How could I find events with title Lunch?
I need to search for all users, portfolios, rents and events.

If you want to query across all events collections in your entire database, you'll want to use a so-called Collection Group Query.

Related

How do I create a recent feed view, using a collection group filtered firebase firestore query?

I have a collection of user subscriptions to books that contain the userId and the bookId. I have a collection of Books, which each have a collection of chapters. Those chapters have timestamp fields. I need a list of all of the chapters that a user is subscribed to, in reverse chronological order, and I am really only going to be needing the most recent 10 most of the time, since it will be shown on a scrolling listview in Flutter.
Can this even be done on Firebase? I know that I can do a collectionGroup and order that, for the chapters, and I can filter that by up to ten values if I include the bookId in the chapter document. But is it really a 10 subscription limit for my users? More than that would need some sort of bizarre parallel queries, or something.
Here's what the database looks like:

Firestore how to do a multiple where clause

I have a question about querying with Firestore.
Indeed, I am developing a mobile application of the social network type with a feed of publications from its subscribers.
However, I can't find any solution using Firestore to view posts from all my subscriptions.
Currently, I have a collection "Users" (which contains a uid and my user information), "Publications" (which lists all the publications of the application) and a document with my list of uid to who I am subscriber.
I was thinking of doing a query like "Show all publications in the collection 'Publications' where uid = one of the uid in my array of uid and order by date.
However, I haven't found a solution to do it because the solution I found is to use the IN operator which is limited to 10 items, while I can have thousands of subscriptions.
ref.orderBy('date', 'desc').where('uid', 'in', arrayOfUid).get();
Do you have an idea ?
You can't exceed the limits of the "in" query. If you need more, you will have to perform multiple queries, and merge their results in the app. There are no workarounds to this.

Firestore: Is there a way to get all collection documents with their sub collection documents [duplicate]

This question already has answers here:
Firestore: Does querying a collection also includes their sub collection?
(2 answers)
Closed 3 years ago.
I have collection users which have a sub collection called "notes about". "Notes about" have security rules that only users with role - "manager" can access them. Now everything works nice and so on, but my problem starts when manager decides to update his name, surname or imageUrl.
Since all notes have also property called "addedBy" which contains user name, surname, id and imageUrl, I need to go through all users and all their notes about sub collection documents, so I can check each document and update it with cloud function.
So far looks like I will have to refactor my db and store "notes about" in a separate collection. But maybe there is a way to get all collection documents with all their sub collection documents in another (reasonable) way.
I would prefer not to refactor my DB but maybe that's the only solution at the moment.
What are your thoughts?
DB model:
But maybe there is a way to get all collection documents with all their sub collection documents in another (reasonable) way.
Unfortunately there isn't. Queries in Firestore are shallow, they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and other subcollections in a single query. Firestore doesn't support queries across different collections in one go. A single query may only use properties of documents in a single collection. IMO, you can go ahead by querying the database multiple times to get the desired values. Is not so slow as might think.
Another possible solution in your case might be to use collection group query. In order to use this new feature, all the subcollections shoould have the same name.
Now it's up to you to decide if you'll use more than one query to get the data, or use collection group query or even refactor the database to fit your needs.

Firestore/Angularfire get collection of docs and subcollection of those docs

Is there a way with Firebase or Angularfire2 to get all the docs in a collection, as well as, one or all of the immediate subcollections.
For example, if I have a collection of categories where each category has a name, as well as a subcollection of subcategories, is there a way to get all the categories with their subcategory collection?
In my head, I'd want to do something like this:
afs.collection("categories").withSubcollection("subcategories");
Another idea I was thinking of that would work but isn't possible, would be to use a wildcard in the path like this:
afs.collection("categories/{categoryId}/subcategories");
I know neither of these are possible, just wanted to see if there was a way to do what I am showing.
At the moment, I store the subcategories in an array on the category document, but documents have a maximum size, a category could theoretically have an unlimited number of subcategories.
Read operations in Firestore are always shallow, and only return document(s) from a single collection. To read documents from multiple collections, you'll need multiple read operations.
The only exception to this is when the collections you want to query have the same name, in which case you can use a collection group query.
I honestly doubt if storing the categories in a subcollection is worth the hassle. While a document has a maximum size, it's 1MB. I'd highly recommend doing the path on how many categories you can store in that, and whether that is a reasonable limit for your app.

Create a collection linked to user ID in Firestore with flutter

Type of App to give context: An app that enables wedding bands to create a profile. Part of this profile will be to enter different dates with locations (Events)and return them in a list view. I have a Firestore collection called userData. The collection is made up of documents IDs(Firebase User ids) generated during sign in. These documents contain fields, mainly Strings and arraysfor things like a header image.I'm thinking of creating a class called Events with member variables date and venue. I'm wondering how I should structure Firestore to allow the Events be queried and returned in a listView. I've looked at sub collections where the UserData would be a parent but I'm not sure is this possible? Or maybe creating a collection at the root called Events but not sure how'd I'd connect the different bands to their Events. Here is a screen shot of Firestore. Each document is a band.
You can either:
Use a new top-level collection to store all events, with a field for the id of the band to help with querying for events for only that bad.
Use a subcollection under each band for their events.
It's totally up to you to choose. It will depend on the kinds of queries you want to make. Note that if you go with #2, you will not be able to query for events across bands (at least not until Firestore supports collection group queries which it does not today).

Resources