How to list all non existent documents in a collection? - firebase

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.

Related

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?

Check if a document exists on Firestore without get() the full document data

So this is possible:
const docSnapshot = await firebase.firestore().collection("SOME_COL").doc("SOME_DOC").get();
console.log(docSnapshot.exists);
But it "downloads" the whole document just to check if it exists. And I'm currently working with some havier documents and I have a script where I just need to know if they exist, but I don't need to download them at that time.
Is there a way to check if a document exist without .get() and avoid downloading the document data?
It seems you are using the JavaScript SDK. With this SDK there isn't any way to only get a subset of the fields of a document.
One of the possible solutions is to maintain another collection with documents that have the same IDs than the main collection documents but which only hold a very small dummy field. You could use a set of Cloud Functions to synchronise the two collections (Documents creation/deletion).
On the other hand, with the Firestore REST API, it is possible, with the get method, to define a DocumentMask which defines a "set of field paths on a document" and is "used to restrict a get operation on a document to a subset of its fields". Depending on your exact use case, this can be an interesting and easier solution.

Get by DOC_ID only with Firestore REST API

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.

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.

Firestore Search - LIKE

I want to search the "displayName" in documents nested within a collection, or more specifically the data is as follows:
users -> $idstring -> displayName
Ive come up with the following using AngularFire but its still not quite working for me.. I need to check displayName against the first 3 chars of val (user entered) and bring back results that start with that, I need a kind of LIKE search operation to occur, is this possible with firestore.. so far its just returning almost everything in my users collection
this.itemsCollection = this.aft.collection<iUser>('users', ref => ref.orderBy("displayName").startAt(val))
As mentioned on the Cloud Firestore Documentation:
Cloud Firestore doesn't support native indexing or search for text
fields in documents. Additionally, downloading an entire collection to
search for fields client-side isn't practical.
And to enable full text search of your data, you'd need to use a third-party search service like Algolia or ElasticSearch.
The documentation actually provides a guide on how to integrate Algolia with Firebase.

Resources