Tok*_*s94 6 javascript rest firebase google-cloud-firestore
第一次发布所以任何提示将不胜感激。我正在尝试使用 REST api 在 StructuredQuery 中设置一个 'orderBy' to firestore。如果不使用 orderBy 字段,请注意我的查询确实有效,这也很有帮助。
我的查询如下所示:
var collection = "testing";
var Query = { //body of the http request
"structuredQuery": {
"from": {
"collectionId":collection,
"allDescendants":true
},
"orderBy": {
"field": {
"fieldPath" : "uploaded"
},
"direction" : "DESCENDING"
},
"limit": 1
},
"newTransaction": {}
}
var urlPOST = "https://firestore.googleapis.com/v1beta1/projects/"+id+"/databases/"+database+"/documents:runQuery?key="+key;
var optPOST = { //http header
'method' : 'post',
'contentType' : 'application/json',
'muteHttpExceptions': true,
'payload': JSON.stringify(Query)
};
var response = UrlFetchApp.fetch(urlPOST, optPOST);
Run Code Online (Sandbox Code Playgroud)
从这个请求我得到:
"error": {
"code": 400,
"message": "The query requires a COLLECTION_GROUP_DESC index for collection testing and field uploaded. You can create it here: https://console.firebase.google.com/project/<mydatabase>/database/firestore/indexes/single-field ",
"status": "FAILED_PRECONDITION"
}
Run Code Online (Sandbox Code Playgroud)
Googles firestore API doco 说它应该给我一个链接来创建所需的索引,但这里返回的链接没有这样做,而是将我返回到概述页面。
关于我正在尝试做的事情的更多细节:我希望能够从数据库中获取最后上传的文档。我通过有一个字段“上传”来做到这一点,它只是一个时间戳字段(通过使用 firestore_admin 库调用 firestore.SERVER_TIMESTAMP 的 python 脚本上传)
如果需要更多信息,请告诉我
提前致谢
TL;DR - 该allDescendants字段现在没有做任何有用的事情,您不应该使用它。它被放入协议中是为了期待一个名为“集合组查询”的功能,但该功能尚未启动。
如您所见,除了其他约束子句之外,每个 Firestore 查询都有一个parent和一个 target 。collectionId
在您的情况下,它parent基本上/projects/$YOUR_PROJECT/datbases/(default)/是数据库的根。是collectionId您正在搜索文档的集合的名称,您已将其指定为testing。
当allDescendants为 false 时,它将仅搜索具有该名称且是父级的直接后代的集合。在这种情况下,这意味着它将搜索集合/testing。
如果allDescendants设置为true搜索将覆盖父级下任意深度的任何名为“testing”的集合。因此,不仅如此/testing,而且还/widgets/foo/testing需要执行此查询,如错误消息中所示,我们称之为“集合组”索引。现在你还没有办法创建这样的索引,这就是为什么我上面说这个参数基本上没什么用。
正如我们在其他一些论坛上所说的那样,我们确实打算在将来发布此类查询,在这种情况下该allDescendants参数会更有用。
| 归档时间: |
|
| 查看次数: |
1827 次 |
| 最近记录: |