Joh*_*doe 9 firebase firebase-realtime-database google-cloud-firestore
我正在努力在firebase云firestore中进行(不那么)复杂的查询.
我只需要where将该id字段 ==的所有文档都给定为id.
然后按结果排序结果,date并限制为10个结果.
所以这是我目前的情况
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
})Run Code Online (Sandbox Code Playgroud)
只有当我不使用orderBy查询但我必须处理这个排序以满足我的应用程序的需要时,我才能得到结果.
有人有想法帮我解决这个问题吗?
谢谢
K V*_*Vij 10
您可以通过在 Firestore 中创建一个索引来做到这一点。
索引的第一个字段应该是相等字段,索引的第二个字段应该是 order by 字段。
鉴于您的问题,您将需要以下索引:
first field: postId, 'asc'
second field: date, 'asc'
Run Code Online (Sandbox Code Playgroud)
请检查文档。它说
但是,如果您有一个具有范围比较(<、<=、>、>=)的过滤器,则您的第一个排序必须位于同一字段
你可以试试这个代码
db.firestore().collection('comments')
.where("postId",'==',idOfThePost)
.orderBy('postId')
.orderBy('date','asc').limit(10).get().then( snapshot => {
.....
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4244 次 |
| 最近记录: |