Unable to get the documents after creating a query index - firebase

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.

Related

firestore using "where" and "orderBy"

I am trying to run a simple query on Firestore, using both where and orderBy. The query does not work. After some research, I found out that I might need to build index in Firestore. I did that, but once again, this query does not work.
error result
The initial orderBy() field "[[FieldPath([lastTime]), true]][0][0]" has to be the same as the where() field parameter "FieldPath([lastChat])" when an inequality operator is invoked. 'package:cloud_firestore/src/query.dart': package:cloud_firestore/src/query.dart:1 Failed assertion: line 456 pos 13: 'field == orders[0][0]
here is the code:
Stream<QuerySnapshot<Map<String, dynamic>>> chatsStream(String email) {
var result = firestore
.collection('users')
.doc(email)
.collection("chats")
.where("lastChat", isNotEqualTo: "")
.orderBy("lastTime", descending: true)
.snapshots();
return result;
}
and this is index in firestore:
sorry for my english, thanks.
You need to specify an orderBy for the lastChat field, as isNotEqualTo is a range condition (it helps to think of it as <> for that purpose):
var result = firestore
.collection('users')
.doc(email)
.collection("chats")
.orderBy("lastChat")
.where("lastChat", isNotEqualTo: "")
.orderBy("lastTime", descending: true)
.snapshots();
The index on lastTime is ascending, but your query is descending. That means the index doesn't match the query, and the database will return no results.
It should actually also log a warning/error with a direct link to create the index with all fields pre-populated (including the correct sort order for lastTime).

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

Firebase check if item is not in array in Flutter

I want to check in my Flutter app if an item is not in an array. If it is in it I want to sort it out, if not I want to take it.
There is an arrayContains function for the reverse function, but I couldn't find an arrayContainsNot function or something. How can I solve this?
I want to do it like this:
await FirebaseFirestore.instance
.collection("someCollection")
.where("someArray", arrayContainsNot: "someItem")
.get();
You can use the not-in operator.
A not-in query returns documents where the given field exists, is not
null, and does not match any of the comparison values.
So your code becomes:
await FirebaseFirestore.instance
.collection("someCollection")
.where("someArray", whereNotIn: ["someItem"])
.get();
Note that it supports up to 10 comparison values which have to be passed in a list to the whereNotIn property.

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 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