Can you create a sub-collection from flutter? - flutter

I was wondering if you could create a sub-collection from within flutter to firestore. I've heard you need to add manually add a document or something like that before you can access a sub-collection from flutter, but I wanted to know if there was a work-around.
Something like this-> Firebase.Firestore.instance.collection('groups').doc(groupID).collection('posts').add('')
without having to manually add a document first. Thanks

In Firestore (all client platforms, not just Flutter), both collections and subcollections don't "exist" in the console unless there is at least one document in it. If you want a subcollection to "exist", simply write a document to it.
When the last document is removed from a collection or subcollection, it ceases to "exist" in the console (after you reload it).
Collections are not really an "thing" in firestore. Documents are a thing, and they happen to be organized under collections. Your app should not depend on collections or subcollections existing - it should depends on documents being returned from queries on those collections.
See also:
Is possible to check if a collection or sub collection exists?
How to check if Cloud Firestore collection exists? ( querysnapshot)
In Dart/Flutter, how can I find out if there is no Collection in the Firestore database?

Related

Get all documents from firestore which contains a particular value in its subcollection

I have a collection say users and it has a subcollection vehicles(type,vehicleNo etc). I want to get all users who have a two-wheeler(type==two-wheeler). Is this possible with a firestore single query? How can I get those documents?
It's not possible. A query can consider only the documents within a single collection at a time. It can't reach into subcollections. What you will have to do is either copy the required data from the subcollection in the main collection that you need for the query, or compose a whole new collection that you can query. Duplicating data like this is common for NoSQL type databases like Firestore.

Check the subcollection exists before query in firestore

I'm implementing a social media app, where I put a subcollection of "following" under each user. I want to check if the subcollection exists before I query the subcollection, or the app will crash for querying a nonexistent collection. Is there a way to check this?
Collections don't really "exist" in the way that you're thinking. They simply appear when the first document is created, and they disappear when the last document is removed. There is no operation to simply create or remove a collection like a folder in a filesystem, and there is not operation to check to see if a collection "exists". A query against a collection with no documents will not fail (unless it was rejected by a security rule).
The only thing you can really do is query the collection to see if it has any documents at all. You can limit the query to 1 document if you want to minimize costs.

Adding document and subcollection to a collection - firestore python

We have a collection in Google Firestore.
Can we add a document with data and a subcollection under this new document in one statement?
In one statement, no. Strictly speaking, most programming languages are going to require at least one statement for each document to write using the provided APIs.
In one batch, yes. You can simply do a batch write to create any two documents simultaneously. Parent documents and subcollections do not need to exist in order to create a nested document, so there is not even any requirement to create the parent document, if that's what you were wondering.
Also, you can't create a subcollection that doesn't "exist". It will exist when the first document is written, and disappear when the last document is deleted.

Is it good practice to use subcollections in Firestore underneath non-existent documents?

Subcollections in Firestore must reference a document within a collection. If this document is ever deleted, its subcollections remain. However, is it good practice to create subcollections underneath documents that never exist (never have existed, never will exist)? Is there any vulnerability in this pattern?
It doesn't really make any difference - it is neither "good" nor "bad". Do whatever suits your application. The data in subcollections can be queried regardless if the parent document exists or not, and creating or deleting that parent document doesn't affect the subcollection in any way.

Skip specific documents when querying them from Firestore database

In my application user gets to pick specific documents out of the list, for example: 1,5,8 from the list containing documents 1,2,3,4,5,6,7,8,9. When logged into the application next time, I want to first fetch all of the chosen documents (considering pagination, because the number of documents user picked could be very high), and then start fetching the remaining documents as the user finishes viewing picked documents by scrolling down the list.
As it turns out, available Firestore querying methods are not capable of skipping the specific documents.
My current idea:
Make single document references for the user-specific documents and fetch them.
Make single document references for the documents between the range of user-specific documents (From the example that would be documents number: 2,3,4,6,7).
After that start making 'big queries' for the remaining documents.
This looks like a working solution, but I'm sure that there is a better way to accomplish the goal, since what I've done is not asynchronous and very slow. Help is appreciated!
Firestore doesn't have any way to exclude specific documents from queries. You may only include them using some existing field values. If you already know the documents to fetch, you can just get() them individually.
It sounds like you are already able to work around these requirements. I don't believe you have any alternatives.