从 cosmos 数据库按时间获取最新记录时出现问题

Pra*_*eep 6 c# azure azure-cosmosdb

我开发了简单的 Web 应用程序,因为我使用 cosmos 数据库将一些数据保存到其中。到目前为止一切正常。但是每当我尝试从 cosmos 数据库中按时间获取最新值时,\xe2\x80\x99s 需要一分钟多才能给出最新记录。

\n\n

这是我为从 cosmos 数据库获取最新记录而编写的代码。

\n\n
        public static async Task<IEnumerable<IoTHubDataPoint>> GetItemsAsync()\n    {\n        IDocumentQuery<IoTHubDataPoint> query = client.CreateDocumentQuery<IoTHubDataPoint>(\n            UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),               \n            new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true}).OrderByDescending(x=>x.Time).Take(1)\n            .AsDocumentQuery();\n\n        List<IoTHubDataPoint> results = new List<ServiceMaxIoTHubDataPoint>();\n        while (query.HasMoreResults)\n        {\n            results.AddRange(await Task.Run(async () => await query.ExecuteNextAsync<IoTHubDataPoint>()) );\n        }\n        //return results.OrderByDescending(x => x.Time).Take(1).ToArray();\n        return results;\n\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是 azure cosmos 数据库中可用的示例数据。

\n\n
{\n"DeviceID": "IoT-1",\n"Time": "2017-11-02T14:46:06.7846161",\n"Temperature": 28.63403,\n"Pressure": "95089.47",\n"Altitude": "532.5873",\n"LightStatus": "Too Bright",\n"LightCDSValue": "193",\n"LightCDSVoltageValue": "0.943304",\n"EventProcessedUtcTime": "2017-11-02T14:46:40.3930989Z",\n"PartitionId": 0,\n"EventEnqueuedUtcTime": "2017-11-02T14:46:07.6Z",\n"IoTHub": {\n    "MessageId": null,\n    "CorrelationId": null,\n    "ConnectionDeviceId": "IoT-1",\n    "ConnectionDeviceGenerationId": "636449561753440920",\n    "EnqueuedTime": "2017-11-02T14:46:07.826Z",\n    "StreamId": null\n},\n"id": "XXXXXXXXXXXXXX",\n"_rid": "XXXXXXXXXXXXXx",\n"_self": "dbs/4RM3AA==/colls/4RM3AOJ1XQA=/docs/XXXXXXXXXXXx==/",\n"_etag": "\\"XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXx\\"",\n"_attachments": "attachments/",\n"_ts": XXXXXX\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

谁能告诉我如何解决上述问题?

\n

Rob*_*MBA 5

这是您可以在 Azure 中执行的查询SELECT top 1 * FROM c order by c._ts desc