简体   繁体   English

Firestore 分页:如何为 REST 定义 **unique** 'startAt'-cursor?

[英]Firestore Pagination: how to define **unique** 'startAt'-cursor for REST?

This is a follow up question to an already solved one .这是一个已经解决的问题的后续问题。

For this previous question an answer was given, how to define a cursor for query-pagination with 'startAt' for REST, that relates to a range of documents .对于之前的这个问题,给出了一个答案,如何定义一个 cursor 用于 query-pagination with 'startAt' for REST, that related to a range of documents In the example below, the cursor relates to all documents with an 'instructionNumber.stringValue' equal to "instr. 101".在下面的示例中,cursor 与“instructionNumber.stringValue”等于“instr.101”的所有文档相关。 According to my testing, this results in skipping of documents.根据我的测试,这会导致跳过文档。

New question: How has the cursor to be defined, to not only relate to the stringValue of a field, that the query is ordered by?新问题:如何定义 cursor,使其不仅与查询排序依据的字段的字符串值相关? But instead to a distinct document (usually defined by its document-id)?而是针对不同的文档(通常由其文档 ID 定义)?

  "structuredQuery": {  
    "from": [{"collectionId": "instructions"}],  
    "where": {  
        "fieldFilter": {    
            "field": {  
                "fieldPath": "belongsToDepartementID"  
            },  
            "op": "EQUAL",  
            "value": {  
                "stringValue": "toplevel-document-id"  
            }  
        } 
    },  
    "orderBy": [  
        {  
            "field": {  
                "fieldPath": "instructionNumber"  
            },  
            "direction": "ASCENDING"  
        }  
    ],  
    "startAt": { 
        "values": [{ 
            "stringValue": "instr. 101" 
        }] 
    }, 
    "limit": 5  
   }  
}

For better understanding, here is the condensed schema of the documents.为了更好地理解,这里是文档的压缩模式。

{
        "document": {
            "name": "projects/PROJECT_NAME/databases/(default)/documents/organizations/testManyInstructions/instructions/i104",
            "fields": 
                "belongsToDepartementID": {
                    "stringValue": "toplevel-document-id"
                },
                "instructionNumber": {
                    "stringValue": "instr. 104"
                },
                "instructionTitle": {
                    "stringValue": "dummy Title104"
                },
                "instructionCurrentRevision": {
                    "stringValue": "A"
                }
            },
            "createTime": "2022-02-18T13:55:47.300271Z",
            "updateTime": "2022-02-18T13:55:47.300271Z"
        }
    }

For a query with no ordering:对于没有排序的查询:

"orderBy": [{
    "direction": "ASCENDING",
    "field": {"fieldPath": "__name__"}
}],
"startAt": {
    "before": false,
    "values": [{"referenceValue": "last/doc/ref"}]
}

For a query with ordering:对于带排序的查询:

"orderBy": [
    {
        "direction": "DESCENDING",
        "field": {"fieldPath": "instructionNumber"}
    },
    {
        "direction": "DESCENDING",
        "field": {"fieldPath": "__name__"}
    }
],
"startAt":
{
    "before": false,
    "values": [
        {"stringValue": "instr. 101"},
        {"referenceValue": "last/doc/ref"}
    ]
}

Be sure to use the same direction for __name__ as the previous "orderBy" or it will need a composite index.确保对__name__使用与之前的“orderBy”相同的方向,否则它将需要一个复合索引。

To ensure you have identify unique document for starting at you'll always want to include the document ID in your call to startAt .为确保您有唯一的起始文档,您总是希望在对startAt的调用中包含文档 ID。

I'm not sure of the exact syntax for the REST API, but the Firebase SDKs automatically pass this document ID when you call startAt with a DocumentSnapshot .我不确定 REST API 的确切语法,但是当您使用DocumentSnapshot调用startAt时,Firebase SDK 会自动传递此文档 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM