Flutter Firebase get documents by array of IDs - firebase

How would I get a Query of documents from a collection using a list of IDs? Let's says I have the following:
List<String> someList = ['abc123', 'def456', 'hij789']; // Need to query for these documentIDs
I would normally do the following. But this obviously won't work since I need to query the documentIDs.
Query query = Firestore.instance
.collection(APIPath.products())
.where('someField', whereIn: someList);

Try using FieldPath.documentId().
Query query = Firestore.instance
.collection(APIPath.products())
.where(FieldPath.documentId(), whereIn: someList);
Note that you are limited to 10 items in the list, and this might actually be slower than just requesting each document individually with get().

Related

Fetching multiple documents at the same time

I'm trying to fetch multiple documents but I'm unable to do it. I wanted to fetch multiple documents containing my search criteria.
Here's what I tried.
final FirebaseFirestore db = FirebaseFirestore.instance;
QuerySnapshot<Map<String, dynamic>> querySnapshot = await db
.collection('MyCollection')
.where('FldName', 'in', ['123', '345', '111']).get();
Error on the syntax which is pointing in the 'in':
Too many positional arguments: 1 expected, but 3 found.
Here's what my firebase looks like.
You need to use whereIn like this:
.where('FldName', whereIn: ['123', '345', '111']).get();
Not, in as a String.
The where query might be incorrect, try using this one that I got from the docs.
.where('FldName', arrayContainsAny: ['123', '345', '111'])

how we can run multiple queries on Firebase/firestore to get filtered data in flutter?

how we can run multiple queries on Firebase/ firestore to get filtered data in flutter or any other possible solution? there is a picture storing dummy data in goal and title and wanna users according to the goals and titles filters.
Where() is a function provided by Firebase to query data
FirebaseFirestore.instance.collection('yourCollectionName').where('field1', isEqual:'value1').where('field2', isEqual: 'value2').get();
you can read more about it in the querying section at https://firebase.flutter.dev/docs/firestore/usage/
By looking at your Firestore database it is clear that you want to filter the documents based on two array fields goals and titles.
Firestore supports the arrayContains filter in the where clause by which you can filter data from an array inside Firestore. So you may think of doing something like this -
var filter1 = 'Hire Employees';
var filter2 = 'CEO';
FirebaseFirestore.instance
.collection('collectionId')
.where('goals', arrayContains: filter1)
.where('titles', arrayContains: filter2)
.get()
.then((QuerySnapshot docs) => {
for (var element in docs.docs) {print(element.data())}
});
But the above will throw an error saying that You cannot use 'array-contains' filters more than once. This is because Firestore has a limitation that you can use at most one array-contains clause per query which is documented here. So it is not possible to do something like the above.
I am not very sure of your use case, but as a workaround, you can restructure the database structure by making any one of the array field to a map, something like the following -
By making the structure like the above you can use multiple where clauses with one arrayContains filter and one isEqualTo filter which will look something like this -
var filter1 = 'Hire Employees';
var filter2 = 'CEO';
FirebaseFirestore.instance
.collection('collectionId')
.where('goals.$filter1', isEqualTo: true)
.where('titles', arrayContains: filter2)
.get()
.then((QuerySnapshot docs) => {
for (var element in docs.docs) {print(element.data())}
});
As the idea of allowing multiple arrayContains filters is currently not supported, but could be valid, I would recommend you file a feature request.

Unable to get the documents after creating a query index

Code:
var querySnapshot = await Firestore //
.instance
.collection('collection')
.where('name', isEqualTo: ['foo'])
.orderBy('time')
.limit(1)
.getDocuments();
print('${querySnapshot.documents}'); // prints []
It returns empty List.
Database structure:
Index built
Indexing isn't an issue here. Given the query and document you're showing, I'd always expect it to return no results. You're using an array-contains type query on a field that isn't an array. Your name field is a string, and strings can't be matched by array-contains queries.
If you intended for name to be an array, you'll need to modify the document so that it is actually an array with the string "foo" in it.

Flutter/Firebase get documents by list of IDs using .get()

This is a followup to Flutter Firebase get documents by array of IDs.
How would I get a Query of documents from a collection using a list of IDs? Let's says I have the following:
List<String> someList = ['abc123', 'def456', 'hij789']; // Need to query for these documentIDs
I'm using FieldPath.documentId() which works, but limits the query to 10.
Query query = Firestore.instance
.collection(APIPath.products())
.where(FieldPath.documentId(), whereIn: someList);
I would normally pass query to a function of mine that would return a Stream<List<Product>> as seen below.
return _service.queryStream(
query: query,
path: APIPath.products(),
builder: (data) => Product.fromMap(data),
);
I know get() is a better option, but not sure what the code would look like if I wanted to return a Stream<List<Product>>.

Flutter firestore compound query

i'd like to know if there's a way to query firebase firestore collection in Flutter adding more than one field to the query. I tried some ways but i didn't realize how to do that. For example:
CollectionReference col = Firestore.instance
.collection("mycollection");
col.where('nome', isEqualTo: 'Tyg');
col.where('valor', isLessThan: '39');
Someone, please, have some way to do that? i am new in flutter and not getting the way.
Building Firestore queries follows a "builder pattern". Every time you call where it returns a new query object. So the code you have constructs two queries, that you don't assign to anything.
CollectionReference col = Firestore.instance
.collection("mycollection");
Query nameQuery = col.where('nome', isEqualTo: 'Tyg');
Query nameValorQuery = nameQuery.where('valor', isLessThan: '39');

Resources