【问题标题】:How to query objects in Azure Cosmos DB with datetime field如何使用日期时间字段查询 Azure Cosmos DB 中的对象
【发布时间】:2019-11-14 19:04:58
【问题描述】:

我们需要使用日期时间字段过滤 Azure Cosmos DB 的对象。对象的结构如下。

{
    "nodeId": 3,
    "nodeName": "HAVC",
    "assetId": 2,
    "assetName": "Chiller_02",
    "assetMeasurementId": 22,
    "assetMeasurementName": "CHL01_WATER_ENTER_TEMP",
    "streamId": 0,
    "streamName": null,
    "deviceId": 4,
    "deviceTagId": 48,
    "rawTelemetryData": 49,
    "observationPropertyId": 1,
    "isActive": false,
    "enqueueDatetime": "2019-05-31T07:17:45",
    "receivedDatetime": "2019-06-03T18:51:16.6684284",
    "id": "1e9fe261-5e73-4a64-8921-817a6c601ea0",
    "_rid": "PfsxALhViWYWAAAAAAAAAA==",
    "_self": "dbs/PfsxAA==/colls/PfsxALhViWY=/docs/PfsxALhViWYWAAAAAAAAAA==/",
    "_etag": "\"0000f80f-0000-1800-0000-5cf51e930000\"",
    "_attachments": "attachments/",
    "_ts": 1559568019
}

这里,我们需要使用'receivedDatetime'字段查询数据,我们需要'18:51'到'18:57'之间的数据

在 C# 中,我们使用类似于 LINQ 的语法

var queryable = client.CreateDocumentQuery<TestDto>(
        UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),
        new { EnableCrossPartitionQuery = true })
    .Where(x => x.receivedDatetime >= DateTime.Parse(startDate))
    .Where(x => x.receivedDatetime <= DateTime.Parse(endDate))
    .OrderBy(x => x.receivedDatetime)
    .AsDocumentQuery();

但我们无法按预期接收数据。感谢您提供一些支持

【问题讨论】:

    标签: c# .net linq azure azure-cosmosdb


    【解决方案1】:

    你可以检查这个 -DateTime, the Epoch and DocumentDb 你必须以 ISO-8601 日期/时间格式存储 receivedDatetime

    【讨论】:

      【解决方案2】:

      要对 Cosmosdb 中的数字字段进行有效的范围查询,您必须在创建容器时在包含数字字段的路径上定义范围索引。

      CosmosDB 不支持查询 DateTime。你可以看看这里的博客了解more

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-05
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      相关资源
      最近更新 更多