Im working on a project using flutter and firebase, currently the database(firestore) has A collection named Projects, each project has an owner(userId) and a subcollection named Sections and each section has an Items collection. Each Item has a list of tags (strings). I wanted to add a search Items by tag feature, but just realized that the nested collections structure makes it hard. Changing the database structure now would be a lot of work. Is there a way to apply a query to multiple subcollections? basically I would need to query all projects owned by the user then query for all in those projects sections and then all todos inside them that contain a certain tag.
I don`t want to do multiple queries and join them with frontend code because I'm using real time functionality and having to deal with multiple streams isn't ideal. Cloud functions aren't an option right now because I'm using the free plan.
You're looking for a so-called collection group query, which searches across all collections with a certain name in one go.
Related
I have 2 different Firestore collections namely 'restaurants' and 'dishes'.
I currently have created 2 separate searches where user either searches for a dishes in search1 connected to 'dishes' collection or a dish in search2 connected to 'dishes' collection.
Please suggest on how to have just one search which searches in both collections in the backend and shows results from both 'restaurants' and 'dishes' at one place. Something like a universal search. So if user types 'burger' it should show 'chicken burger dish' as well as 'burger kind restaurant'.
Was able to make the searches work independently but need help with the strategy to combine the search in to one.
Thanks for your help.
Please suggest how to have just one search which searches in both collections in the backend and shows results from both 'restaurants' and 'dishes' in one place.
There is currently no way in which you can perform a search in two different collections at the same time. Why? Because the queries in Firestore are shallow, meaning that they only get documents from the collection that the query is run against. So to solve this, you have to perform two separate queries.
There is a workaround that might help, which would be to add all the necessary data on which you want to perform the search in a single document, most likely into a field of type array. In that way, you can perform a search using the "array contains" operator. Or for small datasets to search using contains.
If the above solution doesn't work for you, then you have to implement a third-party search service, as mentioned in the official documentation.
I have a collection structure in Firestore that looks like:
/teams/**/days/**/milestones/**
/teams/1WCraAZXLSNSoMvkZuSV/days/20210822/milestones/xyzdocid
So, I would have a set of documents in the milestones collection at the end. I'm trying to query milestones as a collectiongroup. How do I setup a collectionGroup index for this?
Is this too deep for the collectionGroup to work? What does the index look like for this? Is it possbile to create this collectionGroup index in the firebase emulator?
To anybody running into this same problem:
In the Emulator, collectionGroups and all index related operations automatically work. You don't need to setup anything special. This wasn't very well documented. I spent quite some time trying to find out how to create an index in the emulator. This also means, that you will need to do quite a bit of testing with a live firebase account, because most things will not automatically work there.
The collectionGroup format is based on the name of the collection only and not your indexes or any kind of paths. Meaning, if you have collections called "milestones" anywhere in your structure, the collectionGroup concept will combine them together. This is very powerful, but be careful about collections / subCollections with the same name but different purpose.
I am using Angular and Firestore to contain a set of user "projects", each of which may have multiple "budgets" associated with that project
What I want is a list of projects, with the list of budgets for each project
What I want to see on the page
Project A
budget 1
budget 2
Project B
budget c
budget d
budget e
Project C
...
Documents are stuctured with each project having its own a collection of budgets.
I can think of a variety of probably inefficient ways to do this, reading the list of projects first, and the iterating the projects to retrieve each ones budget collection, but somehow it seems like I ought to be able to do this in one go.
What is the usual way to do this, if its possible??
.
It's not possible. Queries in Firestore are shallow - you can only read documents from a single collection (or collection group) at once, neither of which is the case here. You will have to make multiple queries if your documents are distributed multiple collections.
If you must be able to do this in one query, all the documents should be in the same collection, or in subcollections all with the same string ID.
I want to manage multiple projects data in mongoDB. Each project contains multiple users from multiple departments with multiple role assigned to them. plus certain task is assigned to each user. Now I am confused about schema, not able to decide which entity should be kept as collection & which one as document ? What is the best efficient way to store ?
should I keep all under single collection as embedded documents or in separate collection ?
Thanks
First of all if you are using mongodb you should know why are you using it. MongoDB is not about normalize stuff. If you are able to create data structure is de-normalize way then and only then go for MongoDB.
I think you should maintain one single document containing all the mentioned things above. But the scenario which you have mentioned above is good for relational database. you need only 3 entities in relational database and your problem is solved.
Still if you want to go for mongodb you can go with one collection only. which contains project details number of users working there and their roles and department.
I just wanted to query everyone's best practice for doing this.
User has multiple notebooks within their account. Each of these is a record in the database.
There are multiple users.
The notebook has different sections to fill in. There are also sections which are lists. The user needs to be able to add extra items to these lists (almost as if it were it's own collection).
There may be a lot of users, and I want all their notebooks in the same collection.
How would you approach this? I'm using Simple Schema and Aldeed Collection. I imagine that each list within the notebook would be an array, but how would I make it that the user can set how many items / add new items to the list?
Interested to know people's thoughts!
This is a MongoDB data modeling question primarily (see also schema design), but there are a few things to keep in mind with Meteor:
read up on reactive joins at Discover Meteor and Gentle Node
for reactivity to work at its best, you want collections instead of arrays
that means you'll need to perform the equivalent of joins with MongoDB, so have a look at reactive join packages, this post about evaluating them, and Meteor.publish: publish collection which depends on other collection
Make sure to vote up this card on the Meteor roadmap to get native reactive joins.