Reputation: 3499
I have an orderBy query like this:
async list() {
const snapshot = await this.firestore
.collection("queue")
.orderBy(`ignoreUserIds.Po0fouROHVQnrV1ZR17L8Tql3OJ2`)
.limit(10)
.get();
return snapshot.docs.map((e) => e.data());
}
But when I run it, it returns 0 documents while there are documents in the collection.
The document has a different userId in the map. But that shouldn't matter, right? orderBy should return all the documents and just order them?
I also made an index:
This is what a document looks like
I want to find all the documents but just ordered on the field.
Upvotes: 0
Views: 350
Reputation: 1586
An orderBy()
clause has a limitation that it also filters for existence of the given field. The result set will not include documents that do not contain the given field.
For example, let's say a Firestore collection users
contains 3 documents user1
, user2
and user3
. Every document has a field ignoreUserIds
of type Map which contains id : 4
, id2 : 2
and id3 : 5
for user1
, user2
and user3
respectively.
users------>
user1------>
ignoreUserIds :
id : 4
user2----->
ignoreUserIds :
id2 : 2
user3----->
ignoreUserIds :
id3 : 5
The following queries will give the corresponding results
db.collection("users").orderBy('ignoreUserIds.id').get() => only user1
db.collection("users").orderBy('ignoreUserIds.id2').get() => only user2
db.collection("users").orderBy('ignoreUserIds.id3').get() => only user3
db.collection("users").orderBy('ignoreUserIds.id4').get() => 0 documents //as there are no documents inside the collection 'users' containing the field 'ignoreUserIds.id4'.
In your case, it seems, the field mentioned in the orderBy()
clause, ignoreUserIds.Po0fouROHVQnrV1ZR17L8Tql3OJ2
, is not present in any of the documents inside the queue
collection. This is the reason you are getting 0 documents.
Additionally, as you want to sort the documents based on the userIds present inside the ignoreUserIds
field, I would suggest you to mention only ignoreUserIds
inside the orderBy clause which will sort the documents based on the userIds present inside the ignoreUserIds
field.
So your query should look like the following -
const snapshot = await this.firestore
.collection("queue")
.orderBy(‘ignoreUserIds’)
.limit(10)
.get();
You can refer to this document to know more about the orderBy()
clause and its limitations.
Upvotes: 1
Reputation: 138824
But when I run it, it returns 0 documents
That's the expected behavior since, inside the "ignoreUserIds" Map, there is no key that is equal to "Po0fouROHVQnrV1ZR17L8Tql3OJ2".
The document has a different userId in the map.
That's the reason why you are getting no documents.
But that shouldn't matter right?
No, it indeed matters.
orderBy should return all the documents and just order them?
No, it won't since you are trying to order on a field that actually doesn't exist.
I want to find all the documents but just ordered on the field
To achieve that, you have to use the exact UID (aKWp...VNo2) that exists in the database.
You might also take into consideration using either not equal (!=) or not-in it better fit your needs.
Upvotes: 1