Compare DocumentReference(s) in Firestore with .where() - firebase

How to compare DocumentReference references? In this example (with Dart) I only want to fetch tickets from a specific user.
late Query<Map<String, dynamic>> _query;
late DocumentReference<Map<String, dynamic>> _userDocumentReference;
_userDocumentReference = _firestore.collection('users').doc(userId);
_query = _firestore
.collection('tickets'),
//Does not work here
.where('user_id', isEqualTo: _userDocumentReference)
.orderBy('created_at', descending: isDesc)
.limit(7);

I've found the solution by creating an index in FireStore. Check your Debug Console because there it provides a link to create the index to your collection automatically.
It looks like this:
https://console.firebase.google.com/v1/r/project/{project}/firestore/indexes?create_composite=...,

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

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 and Firebase - Get Specific Documents From Firebase

When you go into posts collection there are documents based on userId. And inside a document there is a new collection named userPosts. Inside userposts you can find postId and the details about the post.
I can get specific user post by using this code
postRef = Firestore.instance.collection('posts');
QuerySnapshot snapshot = await postRef
.document(userId)
.collection('userPosts')
.getDocuments();
But I want to get all the user posts without naming a specific user. How could I achieve this?
You can use a collection group query to query documents among all collections with same name.
QuerySnapshot snapshot = await Firestore.instance
.collectionGroup('userPosts')
.getDocuments();

How to check if an array in a firestore document is empty?

I'm building out an app in flutter and I want to run a query to determine if the array in my firestore document is empty. Is there a way to do this?
Here is a picture of my data structured in firestore.
I know there is an arrayContains method but I'm not sure on how to check for an empty array. Here is my current code.
_postsStream = await _firestore
.collectionGroup('posts')
.where('timestamp', isGreaterThanOrEqualTo: _start)
.where('timestamp', isLessThanOrEqualTo: _end)
.where('problems', arrayContains: )
.snapshots();
I left the arrayContains: intentionally empty for now. Please advise on how I would go about implementing this feature. Thanks in advance!
An empty array defaults to [] So this is possible by doing the following:
Do not use ' ' for the square brackets
final _database = Firestore.instance;
return _database
.collection('arrays')
.document(arrId)
.where('array', isEqualTo: [])
// .where('array', isNull: true) can be used to check if it's null
.snapshots()
.map(_itemListFromSnapshot);
You cannot do it in firestore currently.
What you can do is.
Create a separate property hasProblems which defaults to false.
If the user try to register a problem, then check the flag hasProblems.
If hasProblems==false, then toggle it to true and add the problem to the list.
else no need to toggle, just add the new problem to the list.
If problems are to be removed later on, then check for the list to get empty.
Once it is empty, then toggle hasProblems back to false.
This way you can achieve what you want-
postsStream = await _firestore
.collectionGroup('posts')
.where('timestamp', isGreaterThanOrEqualTo: _start)
.where('timestamp', isLessThanOrEqualTo: _end)
.where('hasProblems',isEqualsTo: false)
.snapshots();
Maybe you can have other better solutions, but this is the one that came in my mind.
You can follow the solution #mubin986 has given, but once the list gets bigger and bigger, it impacts performance.
Cheers, hope it helps.
There is no way to check with firestore query if an array is empty or not.
But, you can follow these steps:
Query the data with range filters:
_postsStream = await _firestore.collectionGroup('posts')
.where('timestamp', isGreaterThanOrEqualTo: _start)
.where('timestamp', isLessThanOrEqualTo: _end)
.snapshots();
After fetching the data, iterate over all the DocumentSnapshot and check if problems array is empty or not.

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