Fetching multiple documents at the same time - firebase

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'])

Related

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.

Flutter Firebase - Search for documents which contain a string for

I have a question regarding a request to retrieve data from Google Cloud Firestore with specific parameters in my Flutter project.
I use the following code to check if a string in Firebase equals to a search query performed by a user:
var snapshot = await firestoreInstance.collection('categories/subcategory/items')
.where("parameter", isEqualTo: searchQuery).get()
This works if the user types exactly the name of the parameter stored in Firestore. But what I want is that it also works if only part of the searchQuery string is stored in the Firestore parameter.
I found a solution on https://medium.com/flutterdevs/implement-searching-with-firebase-firestore-flutter-de7ebd53c8c9 for this. In the article an array is created with all possibile searches for the parameter. But I think that is a bit complex and you have to generate a lot of new data in Firestore just to search for the article.
Is there a way to do this in an easier way so that you can use an operator as "contains" instead of "isEqualTo" in Flutter with Firebase for the request?
var snapshot = await firestoreInstance
.collection('categories/subcategory/items')
.where(
'parameter',
isGreaterThanOrEqualTo: searchQuery,
isLessThan: searchQuery + 'z'
)
.get();
Try this:
var snapshot = await firestoreInstance
.collection('categories/subcategory/items')
.where(
'parameter',
isGreaterThanOrEqualTo: searchQuery,
isLessThan: searchQuery.substring(0, searchQuery.length - 1) +
String.fromCharCode(searchQuery.codeUnitAt(searchQuery.length - 1) + 1),
)
.get();
I based the variable names on your example.

How to handle nested collection query in cloud flutter firestore?

If have a collection like this
Shop(Collection):
-shopId(Document)
-name
-adresse
-itemSold (Nested Collection)
-itemId
-....
-....
how can i write a query in flutter that will return me only the shop that have a specific itemId in itemSold collection.
Its this gonna cost many read?
How are you handling that kind of tree on your side i am pretty new in noSQL database. Im use to traditionnal database.
You can use the .where method.
In your case something like this:
DocumentReference shopInstance = Firestore.instance
.collection('shops')
.document('shopsID'):
await shopInstance
.collection('itemSold')
.where('itemID', isEqualTo: 'someId')
.getDocuments()
.then()...

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 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().

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