using two queries to get 'data1' OR 'data2' in flutter firebase - 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.

Related

how to query (filter) documents from firebase to flutter app based on a value nested in a List of Maps?

im trying to get documents from my 'Patients' Collection which contain an array of maps 'Appointments' based on the value of 'translator' key as shown here:
here is how im trying to query the documents but i guess im having a syntax error
static Stream<QuerySnapshot> getSpecificUserAppointments(String translatorName) {
return FirebaseFirestore.instance
.collection('Patients')
.where('appointments', arrayContains: translatorName)
.snapshots();
}
i also tried to filter this way
.where('appointments.translator', arrayContains: translatorName),
.where('appointments.translator', isEqualto: translatorName)
still not getting results
any help would be appreciated
The arrayContains operator checks whether the exact, complete item you specify exists in the array. So your check would only work if the appointments array contained an entry that has a single string as its value with translatorName.
If you want to check whether a certain name exists, add an additional array field to your document with translatorNames, where you keep just the (unique) names of the translators in the document. Then you can use that field to query:
return FirebaseFirestore.instance
.collection('Patients')
.where('translatorNames', arrayContains: translatorName)
.snapshots();

How to filter sub collection in firebase (flutter)?

I have a collection called Channels and inside it I have a sub collection called messages, so I want to filter the channel collection then I want to fetch the messages from it ?
FirebaseFirestore.instance
.collection('Channels') >> where(id = user.uid) <<<<< I want to filter this
.doc()
.collection("messages")
.orderBy("createdAt")
.snapshots(),
*any help will be appreciated ^^
error :
The method 'doc' isn't defined for the type 'Query'.
Try correcting the name to the name of an existing method, or defining a method named 'doc'.
According to this link you should use the where method. Combine that with firebase.flutter.dev docs (heading with relevant example here), you should be able to accomplish this using something like:
FirebaseFirestore.instance
.collection('Channels')
.where('id', isEqualTo: user.uid)
Also is channelID same with senderID? If the answer is yes, do you need both (and are you sure that is the structure you want)? :)
A Firestore query can only access documents from a single collection, or from a group of collections all with the same name. So you can't filter documents from messages based on fields in their parent Channels document.
What you can do is add the relevant field to each messages document, e.g. in a field named channelUID. Then you can use a collection group query to query all messages collections:
FirebaseFirestore.instance
.collectionGroup("messages")
.orderBy("createdAt")
.where("channelUID", isEqualTo: user.uid)
.snapshots(),
The problem is that the doc() is empty, so FB does not know which doc to go into.
Just add the id of the channel inside the doc(), like so:
FirebaseFirestore.instance
.collection('Channels')
.doc(recieverID)
.collection('messages')
.orderBy('createdAt')

Retrieve data from Flutter Firebase database

I am trying to retrieve data of all the users of the users collection and compare it with some other data. I am able to retrieve data of a particular user from its uid but want to iterate through all the collections and documents.
If you don't specify a specific document ID and use the get() method on a CollectionReference, it'll return a QuerySnapshot (containing all documents in that collection) which essentially is an array of QueryDocumentSnapshot.
FirebaseFirestore.instance
.collection('users')
.get()
.then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) {
print(doc["field_name"]);
});
});
You can use "QuerySnapshot" to retrieve data of all the users of the users collection. Please see an example of same at https://firebase.flutter.dev/docs/firestore/usage/#document--query-snapshots
I expect you are currently using "DocumentSnapshot" to retrieve data of a particular user.

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.

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