This question already has answers here:
Google Firestore - How to get several documents by multiple ids in one round-trip?
(17 answers)
Closed 8 months ago.
In Firestore, if I have as list of, say, 20 document IDs, what is the best way to fetch all those documents?
I'm thinking of, for example, using something like Angolia for search and getting a list of ids back from it.
Would I need to manually get or onSnaptshot each reference, or is there a more performant/simple way to fetch a list of documents just using their ID?
Thanks!
A lot of the options depends on how your firestore is structured, and what framework you're using to encorporate firebase.
Generally speaking, querying a list can be done using the in query operator.
In the case of id querying, the array is id's, and you query by firebase.firestore.FieldPath.documentId()
import { query, where } from "firebase/firestore";
const q = query(firebase.firestore.FieldPath.documentId(), where('id', 'in', ['SOME_ID_1', 'SOME_ID_2']));
Related
This question already has answers here:
Query based on multiple where clauses in Firebase
(8 answers)
Closed last year.
I'm using FirebaseRecyclerAdapter and pass FirebaseRecyclerOptions with some query into constructor. Each item has number of fields including timestamp a and another integer field b. So I want items with older a but less b to be on top of list. It's easy to implement with sqlite, but not so easy with nosql. It seems I need to add some extra field which keeps both fields:
String.valueOf(a) + String.valueOf(b) but how to achieve asc / desc properties?
Loading the whole list and sorting in Java is not an option.
Unfortunately, Firebase Realtime database does not support queries on multiple properties, supports only queries on a single child property. So, you're correct in guessing that you'll need to create an extra field to keep both fields. So to achieve this, you need to create a new field which in your database should look like this:
Firebase-root
|
--- itemId
|
--- a: valueOfA
|
--- b: valueOfB
|
--- a_b: valueOfA_valueOfB
So as you see, the a_b property combines the values that you want to filter on.
Unlike Firebase Realtime database, Cloud Firestore allows compound queries. You should take a look at this. So a query as the one below is allowed in Cloud Firestore without creating a combined property.
itemIdRef.whereEqualTo("a", "valueOfA").whereEqualTo("b", "valueOfB");
When you order strings this is the normal ordering method. A quick fix would be to add two zeros before each second element like this: "1516428687_001", "1516428687_002", "1516428687_012". Now your order will be fine. For more explanations, please see my answer from this post.
This question already has answers here:
Firestore: how to perform a query with inequality / not equals
(7 answers)
Closed 2 years ago.
I want to retrieve all the collections except one document id. I have following query it will get all data by validating collection field. however i want to get all data except one document.
await Firestore.instance.collection('tbl').where('id',isEqualTo:textId).getDocuments().then(
(data){
dataRowCount= data.documents.length;
}
);
Firestore doesn't offer any way to exclude documents from a query. You must can only filter for known values that you're looking for, or ranges of values. If you want to skip a document, you will have to check the query results in your code and omit the one you don't want
This question already has answers here:
collectionGroup within a certain path
(2 answers)
Closed 3 years ago.
I have a user collection, and within that collection there's a collection name groups, which then has groupItems. Is it possible to run a query for all groupItems for a specific user?
I could run a global version like:
db.collectionGroup('groupItems')
but that is overkill for me, I'm looking for something like:
db.collection('users').doc(uid).collection('groups').collectionGroup('groupIems').where('categoryOnly', '==', true)
Is this possible?
The architecture of the collections looks like this:
Users
---> User
------> Collections
---------> GroupA
------------> GroupAItems
---------------> GroupAItem1
---------------> GroupAItem2
---------> GroupB
------------> GroupBItems
---------------> GroupBItem1
---------------> GroupBItem2
Ideally I could call for all Group Items instead of first calling for groups and getting [GroupA, GroupB...], and then calling for GroupA's items, then GroupB's, etc.
Update: it turns out that querying a specific path may be possible after all, thanks to how FieldPath.documentId() is indexed for collection group indexes. Check #samthecodingman's answer here: https://stackoverflow.com/a/68049847
Happy answer above 👆
Old answer below 👇
There is currently no way to limit a collection group to just collections under a specific path. All collection group queries get documents from all collections with the specified name.
So the only way to currently query a subset of your groups is to use a naming scheme that allows you to uniquely address them.
Also see:
Does collection group queries get data from all collections with the same name?
collectionGroup within a certain path
This question already has answers here:
Google Firestore: Query on substring of a property value (text search)
(25 answers)
Closed 3 years ago.
When I search for 'stone', I want my query to return
{ first: 'Fred', last: 'Flintstone', }
However, the docs suggest I need to search for an exact match only.
usersRef.where("last", "==", "Flintstone")
And will not return a partial match of 'stone'.
Is there a way to get partial string search matches?
This is not supported by firebase as the link you shared mentioned, probably you want to use some tool like algolia to achieve that use case, and keep it in sync with firestore using cloud functions.
This question already has answers here:
Firebase (2016) Shallow Query
(2 answers)
Closed 3 years ago.
I have the next datas on real time firebase database:
example
As you see, there are more data in "animals" , "rocks" and "vegetables". I want get that names in a List of data, something like this:
List list = ["animals","rocks", "vegetables"];
I know how to do a get http request in flutter to get data doing this:
var response = await http.get('https://mydb.firebaseio.com/prueba.json');
pruebaFirebase = json.decode(response.body);
But it returns a List of collections, and I only need the name of each one names of collections.
Thanks in advance dear community.
Use the shallow query parameter of the Realtime Database REST API:
https://mydb.firebaseio.com/prueba.json?shallow=true