How to get documents from Firestore without a single document id - firebase

So i have a collection named products in my Firestore and it contains all my products.
Since i am using Tag widget it gives me an error when there is more than one item sharing the same tag. So for my Products Builder i want to give a list that contains all my products without a specific product catched by his id, so stream property of my Stream Builder can be like this.
StreamBuilder(stream: Firestore.instance.collection('products').WITHOUT SPECIFIC PRODUCT ID .snapshot()
builder:...)
How can i do that?

It's not possible to construct a query for all documents except one. There are no inequality filters in Firestore.
Since it's just one document, it makes sense to simply do the query for all documents, the have the client code remove the one you don't want from the result set.

Related

Firestore: Wherein custom object array?

im trying to make a sort of chat app with the following architecture:
Its currently working as follows: in a root doc, there is an array of chat objects. Each chat object spawns a message doc, which contains an array of message objects. Same logic for comments.
Im thinking about a function to update all posts relative to the associated user, IE if a user changes their name all associated comments will be updated. Is this possible with the WhereIn() function? Or should i edit the architecture to something more like each document being its own message/comment? Thanks!
An in clause checks if a specific field is equal to one of a set of values, which doesn't apply here.
You might be thinking of array-contains, but that wouldn't work in your current structure either. The array-contains only matches exact an item in the array if it completely/exactly matches the value in the query.
The common way to allow such a query is to add a participants array field to each document where you store the UIDs of all participants in that doc. Then you can do an array-contains query against that.

Firestore - Batch querying nested documents in REST API

There is a way to batch get documents after you specified exactly all the file paths using
https://firestore.googleapis.com/v1/projects/projectName/databases/dbName/documents:batchGet.
And there is a way to get all documents and their fields under a collection by sending a GET to https://firestore.googleapis.com/v1/projects/projectName/databases/dbName/documents/collectionName
I ideally want to make a batch request to get document fields for all documents under an array of collections. Is there a way to do this without knowing the document names of every document I intend to get?
Example
I have a structure like projects/projectName/databases/dbName/documents/Inventory/productId/variantId/*location*
Each productId is a document, and under this it has a collection for each variant, and within that collection are documents for each location, that contains a field count.
For a basket, I want to get all inventory counts for all inventory locations, for each productId/variantId in that bas
It is not possible to get all documents based on an array of collection names.
You can use a collection group query and search all collections of a given name, but then you must know the path of each document you want to read.
Alternatively, you can get all documents under a specific path, but then you can't filter by ID anymore, and the collections have to be under a path - not an array.

How to query sub-collection of particular id only from particular parent collection?

I have collections named quests and quizzes, which have sub-collections inside their documents named published which will contain different published versions of the corresponding document.
quests/
published/
quizzes/
published/
quests and quizzes are also tagged and these tags are copied to published document. So all published documents have a tags field(an Array of Strings).
I want to query all the published quests which are related to given tag.
Something along this
db
.collectionGroup('published')
// This query is not correct, regex syntax doesn't work here for path.
// I have added it to show that I want to query only the published
// documents inside quests.
.where(
firebase.firestore.FieldPath.documentId(),
'==',
'/quests/*/published/*'
)
.where(
'tags',
'array-contains',
'kinematics'
)
--- EDIT ---
this is structure of quests, and quizzes also has the same structure.
and it also has tags field.
quest when version 1.0 was published
quest when version 2.0 was published
I want to fetch all the published quests that are related some particular tag, so here quests//published/2.0 should be one of the resulting documents, if I query with 'physics' tag, as version 1.0 it is not related only to 'kinematics'. Screenshot of only one quest is added here. there are multiple quests with multiple published version inside them relating to tag I want to query. I want to fetch all of them, excluding documents from quizzes//published/ with a single query.
This part of your query is not possible:
.where(
firebase.firestore.FieldPath.documentId(),
'==',
'/quests/*/published/*'
)
FieldPath.documentId() just can't be used as a filter in collection group queries. It's a known limitation. There are also no wildcard matches in Firestore, at all.
There's another problem with your requirement to "exclude documents from quizzes//published/". Firestore queries also cannot also exclude specific items based on inequality. Please read this other issue for more details. What you can do is store the name of the top-level collection (e.g. "quests") in a field in the documents of a subcollection, then filter to match documents with that field. But you can't exclude documents this way. If you want exclusion, you will need a boolean field that expresses this exclusion, such as "is_not_quiz", and set it appropriately for each document that is not a quiz.

Firestore database to accommodate multiple users independently arranging items

I need some help designing the Firestore database for my app. The app has three requirements:
Display documents in a recycler view;
The user to rearrange document items via drag and drop; and
Multiple users to access the documents and independently rearrange their items.
The first requirement is straight forward using a Firestore query.
The second requirement I’m planning to have a “position” field in the documents so the query can be “ordered by” the “position” field.
It’s the third requirement where I have difficulties. My first thought is to is to have multiple “position” fields associated with each user and order the query by the “user’s position” field. However, since Firestore requires an index for each query this approach does not work.
Any suggestions as to how best to design my Firestore database to accomplish the app’s three requirements would be appreciated.
You could store each user's ordering in a per-user document or collection (organized separately from the data itself). Simply map each document id to that user's chosen position for it. This means you would also need a separate query to get that ordering, but you wouldn't need a new index at all.
For anyone else that is interested, below describes my changes:
Excluded the “position” field from the Firestore item document. So, now when item documents are retrieved from Firestore they contain no position information. And they are downloaded in no particular order.
I’ve created a new Firestore “state” document that has a “positions” field that is a map with its key as the item’s uid and a position Int as its value. (I’ve also included an Int field that holds the recycler view’s firstVisiblePosition and another that holds its offsetTop so the recycler view can be restored to its last position.)
After the initial loading of item documents, the position values are applied to each item document. And the item documents are sorted by their position.
I handle ADDED, MODIFIED, and REMOVED document change events in my recycler view’s adapter. The change events “oldIndex” and “newIndex” values are not used.
Last, I save the “state” document under a user’s subCollection in the onStop lifecycle call back.
Hope this helps.

Way to store Tags for good Query options?

I have a Collection of Posts and a Collection of Users. Posts have certain attributes which are irrelevant for this, but they also have one attribute called tags right now its an array with certain words.
A User can follow certain Tags so he has a attribute followedTags which is also an Array right now that contains the Tags he follows.
Now one of the use cases for this is a User Feed to Show only Posts with Tags he follows, the Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays). Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags) I thought maybe I could make a smart change in the data modell itself, any suggestions?
Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays)
If you try to chain multiple whereArrayContains() methods, you're most likely getting the following error:
Caused by: java.lang.IllegalArgumentException: Invalid Query. Queries only support having a single array-contains filter.
So unfortunately Firestore can only allow a single call to whereArrayContains() method in query.
Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags)
If you have a reasonable number tags, you can create each tag as a separate property. In this case, it is allowed to call Firestore Query's whereEqualTo() multiple times.
If this is not the case, then you should consider augmenting your database structure to allow a reverse lookup by creating each tag as a seprate object (document) in a tagCollection. Under each document you can create a new collection named tagPost in which you should add all the posts that are labeled with a specific tag.

Resources