is there any way to get documents from firebase by list of IDs?
I have this list:
val IDs= listOf("id1","id2","id3","id4")
and I want to get all those documents without looping through them.
something like this, if possible:
Firebase.firestore.collection("users").documents(IDs).get()
The Firestore client libraries don't have the notion of batch reads which would allow to retrieve a set of documents given it's ids. Nonetheless, you can still make queries that filter on the document id by using the FieldPath.documentId() object. This value translates to a special sentinel that allows queries on the document id so you can write a query like below:
Firebase.firestore.collection("users")
.whereIn(FieldPath.documentId(), listOf("id1", "id2", [...], "id10"))
There's one restriction to note with this approach. The whereIn filter accepts at most 10 values. If you need to retrieve more it would be necessary to make several queries.
Alternatively, the approach suggested by Brettski of making a bunch of single document reads would work too.
That isn't possible in Firestore. Id's can only be accessed directly through a collection/document request.
I don't know Kotlin, though in JavaScript I would perhaps approach it by creating an array of functions for each of the documents, then using a Promise.all() to retrieve them at once. It is, imo a downside of Firestore where you can't query for multiple Id's.
Related
I have an App where there are Documents for each car. I somehow need to query the Documents in a Collection to give me the ones where a User has worked on some Tasks.
The Problem is however, the Fieldpath in each Document is different so I cant query with a static Fieldpath.
Here is the Example Structure of an Document, and yes I know it is one of the worst ways to do it but I already made it a long time ago.
So what would be the best way to get the worker Array Queried if the FieldPath is not static?
The Problem is however, the Fieldpath in each Document is different so I cant query with a static Fieldpath.
If you don't know the full path of a field value to query, you simply can't query it. There are no wildcards for document fields in a query. It sounds like you will have to change your document structure if you want to perform the query, or at least just duplicate some data into another field that lets you make the query you want. Duplicating data to satisfy queries is common in NoSQL type databases.
My Android app has documents saved in Firestore following the path val documentName = "pets/$userid/$animal/$animalId". Animal can be cat, dog, bird, etc. I want to get all the pets owned by a specific user and then I want to iterate through the list of pets. How do I do that? I haven't been able to find an example. So basically I have
firestore.document("pets/$userId").get().addOnSuccessListener {
// now what?
}
There is no way to loop over all collections in the client-side SDKs, and also no way to query across collections with different names.
So the only real option is to perform a separate query for each subcollection:
firestore.document("pets/$userId/cat").get()...
firestore.document("pets/$userId/dog").get()...
firestore.document("pets/$userId/bird").get()...
And then you'd merge them client-side.
I'd also consider whether you really need these subcollections though. You can also store all of them in a single subcollection, and use a field to differentiate between animal types. That is a simpler model, which probably will work fine for this use-case.
You can't query across multiple subcollections in a single query. A query has to target a single collection or subcollection (or a collection group query that targets all collections with the exact same name). Also Firestore client SDKs can't list subcollections under a document.
You should probably restructure your data to support the queries you intend to make, which means putting the entire list of things in a single subcollection with a predictable name.
Are db.document("a").collection("b") and some_var = db.document("a"); some_var_2 = some_var.collection("b") equivalent?
In particular: in both cases, are "a"'s fields retrieved from database and loaded in RAM (especially for the first way)? (in other words: does the first way just retrieve the subcollection without retrieving/loading "a"'s fields?)
In both cases, you will only generate document or collection references, so Firestore won't fetch any data until you call a method (such as .get()) on it.
So, there is no difference, but you should use the first way in the case you will need to make operations on the entire collection in addition to the single document.
They are equivalent.
Neither of them actually retrieve any data. They are just creating references to documents. You have to call get() or attach a listener to actually get the data from the documents. You should probably familiarize yourself with the documentation for reading data from Firestore.
I have a simple Database structure, I want to do a query on it :
get the article wherein motcle contains.(key), and from the articles found we order them by the highest note.
I know that to do the "AND" I have to make a nested query. but how can I get the first node the article when I am in the second node motcle.
the Database
Did you consider changing the way data is structured, e.g. storing "html" is your case as value istead of key? (or migrate to Firestore, which provides better querying capabilities)
Then you could do query similar to
ref("article").child(id)
.orderByChild("motocle/someKey")
.equalTo("Html")
This is typical approach with Firebase, when data is structured based on query you need.
All of the Firestore data retrieval examples show a full document getting returned. Examples like this:
// Create a reference to the cities collection
CollectionReference citiesRef = db.collection("cities");
// Create a query against the collection.
Query query = citiesRef.whereEqualTo("state", "CA");
My database has 3,000 city objects in it, and I need to get a list of all the unique states in my database. Can someone show me an example of how I can get this information from Firestore? I'm hoping I don't need to download all 3000 documents just to collate this list myself.
You could create a Cloud Function which is triggered by an onWrite event to your cities collection. This function could see if the state already exists in a states collection and, if not, add it.
This way, you'll end up with a states collection which only has one of each state in it. As states are generally a 2 letter code, you could use this code as the document index, to ensure uniqueness in your cloud function writes.
As Frank says, the only way in which you can achieve this is by duplicationg data. This tehnique is named denormalization and for that I'll share you another resourse, which is a tutorial that I personally recomend you to see for a better understanding, Denormalization is normal with the Firebase Database. This tutorial was made for Firebase Realtime database but the same principles are also in Cloud Firestore.
So to solve your problem, you need to create another top level collection in which you need to store only the unique states. But you need to be aware of the fact that every time you add or delete a document, you need to do it twice, once for the cities collection that you already have and second for the newly created collection uniqueStates.