Get by DOC_ID only with Firestore REST API - firebase

I'm using Firebase REST API to get documents. Now the following url works fine:
https://firestore.googleapis.com/v1beta1/projects/{project}/databases/(default)/documents/{collection}/{doc_id}
However I'd like to know if it's possible to get a document only with {doc_id} without specifying the collection?
Thanks so much!

With the Firestore REST API, you will not be able to query the database for all documents with a specific ID under all possible collections. This will not be possible with the other SDKs as well.
What you could do with the REST API is listing all the (sub-)collections of a given document, see https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/listCollectionIds?authuser=0. You will receive collectionIds array of Collections IDs and you will be able to iterate on this array to search for all the documents with a specific ID within those collections.
Note that it doesn't seem to be possible to list the top-level collections for the database.

Related

How to list all non existent documents in a collection?

Say I have the following structure:
collectionA/documentA/collectionB/documentB
But I have set documentB directly to the above path (without explicitly creating collectionA, documentA or collectionB), so the document "documentA" is a non existent document that does not show in queries if I list all documents of collectionA even with admin sdk.
However the firebase web console somehow manages to list such documents. Not only that, I have also seen a third party app Firefoo list such documents and somehow even paginate the results. How can I achieve this?
Turns out this can be achieved with the
Firestore REST API v1
using the listDocuments endpoint and setting query param showMissing to true.
All Firestore queries are based on having some data about that document present in the index that is used for the query. There is no public API to show non-existing documents.
The Firebase console shows these documents based on getting a list of all collections, and then building a data model from that.

Filter Firestore Collection by Document Reference-Console

How does one filter a Firestore collection by a document reference in the web console? For example, I have a collection that references another collection document called challenges.
But no matter what combination I use to try to filter that collection by the challenge ID it returns no results. I need to be able to do this so I can look at all of the documents in this collection that reference back to that particular challenge reference.
I am assuming this is possible in the console, but maybe it is not?

How to print collection name under document in flutter firebase

[![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?

Correlating Firestore indexes to slugs in a webapp

I'd like my web app router slugs to correspond to my Firestore documents data.
For example:
www.mysite.com/restaurants/burger-king
/restaurants <- Firestore Collection
/restaurants/mcdonalds <- Firestore Document
/restaurants/burger-king <- Firestore Document
This is easy enough, as I can assign the name as a slug-friendly UID in Firestore. The difficulty arises with CRUD functionality. I need to be able to rename my item titles, but Firestore does not permit you to rename indexes, which is the issue I'm facing.
One SO solution I saw was to delete the old record and creates a new one at the updated index. That's problematic for me, because sub-collections would be hard to transfer from the client side.
Are there more elegant solutions?
You don't have to identify a document by its ID. If you're able to ensure uniqueness of a document field value, you could instead query a collection for an ID value in a known field, then use the results of that query to satisfy your REST API. Then, you can change the value of that document field as often as you want, in order to satisfy required changes to the public API.

Firestore: Get documents without parent document

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.

Resources