如何查询一个字段,然后在Firebase Cloud Firestore中按另一个字段排序? [英] How to query one field then order by another one in Firebase cloud Firestore?
问题描述
我正在努力在Firebase Cloud Firestore中进行(并非如此)复杂的查询.
I'm struggling to make a (not so) complex query in firebase cloud firestore.
我只需要给所有文档where
id
字段==
一个给定的ID.
I simply need to get all docs where
the id
field ==
a given id.
然后按date
的顺序对结果进行排序,并限制为10个结果.
Then order the results by their date
and limit to 10 results.
这是我目前的情况
db.firestore().collection('comments')
.where("postId",'==',idOfThePost)
.orderBy('date','asc').limit(10).get().then( snapshot => {
//Nothing happens and the request wasn't even been executed
})
仅当我不使用orderBy
查询但我必须根据我的应用程序的需要处理这种排序时,我才能获得结果.
I can get the result only if i don't use the orderBy
query but i have to process this sorting for the needs of my application.
有人想帮助我解决这个问题吗?
Someone has an idea to help me to fix this ?
谢谢
推荐答案
请检查文档.它说
但是,如果您有一个具有范围比较的过滤器(<,< =,>,> =),则您的第一次排序必须在同一字段上
However, if you have a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field
您可以尝试使用此代码
db.firestore().collection('comments')
.where("postId",'==',idOfThePost)
.orderBy('postId')
.orderBy('date','asc').limit(10).get().then( snapshot => {
.....
})
这篇关于如何查询一个字段,然后在Firebase Cloud Firestore中按另一个字段排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!