Flutter firestore compound query - firebase

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

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

using two queries to get 'data1' OR 'data2' in flutter firebase

I want to get data from firebase only if the user is not saved in the document. Because I need a or-query, which is not possible in Firebase, I decided to use RxDart's CombinedStreams with two steams:
var streamOne = FirebaseFirestore.instance
.collection('jobs')
.where('jobState', isEqualTo: 1)
.where('userOne', isNotEqualTo: FirebaseAuth.instance.currentUser!.uid)
.snapshots();
var streamTwo = FirebaseFirestore.instance
.collection('jobs')
.where('jobState', isEqualTo: 1)
.where('userTwo', isNotEqualTo: FirebaseAuth.instance.currentUser!.uid)
.snapshots();
But my app shows the data even if the current user is in 'userOne' OR 'userTwo'. Is it possible to avoid this and get the data just if the currentUser is not 'userOne' OR 'userTwo'?
Your logic is flawed: if the UID is in userOne, the second query will still return that document, and vice versa. What you want is actually an AND: the documents where the UID is not in userOne and not in userTwo.
Unfortunately though that query also isn't possible on Firestore, as all not-equal conditions in a query must be on the same field.
There is no way to capture your logic in a single query, and you will have to filter the documents fo userOne and userTwo in your application code instead.

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

Resources