【问题标题】:Azure CosmosDB : problem with datetime (timestamp)Azure CosmosDB:日期时间问题(时间戳)
【发布时间】:2019-11-11 12:57:57
【问题描述】:

我关注this页面寻找更好的基于时间戳的查询方式

我使用 Cosmonaut 库,这是 cosmos DB 设置

var cosmosSettings = new CosmosStoreSettings(cosmosDbName, endpointUrl, key, settings: setting =>
        {
            setting.IndexingPolicy = new IndexingPolicy(
                new RangeIndex(DataType.String, precision: -1), 
                new RangeIndex(DataType.Number, precision: -1));
        });

然后我尝试根据日期时间查询如下

    public async Task<IEnumerable<collectionNameObject>> GetAsync(GetCollection query)
    {
        var result = await _objectStore
            .Query(new FeedOptions {PartitionKey = new PartitionKey(query.x)})
            .Where(r =>
                r.y == query.y
                && r.z == query.z
                && r.Timestamp.Date >= query.Date.Date)
            .ToListAsync();
        return result;
    }

这是我在 CosmosDb 中保存的内容

 [CosmosCollection("collectionNameObject")]
public class collectionNameObject: Entity
{
    [CosmosPartitionKey]
    [JsonProperty("x")]
    public string x{ get; set; }

    [JsonProperty("z")] public string z{ get; set; }

    [JsonProperty("y")] public string y{ get; set; }

    [JsonProperty("timestamp")] public DateTime Timestamp { get; set; }
}

问题是查询结果总是空的,但是,如果我删除时间戳过滤器,我会得到我所期望的。不清楚我错过了什么,所以我想知道是否有人有更好的建议或提示?

【问题讨论】:

    标签: c# azure-cosmosdb


    【解决方案1】:

    我猜由于您将其存储为字符串,因此当您将其作为 DateTime 查询时不会反映出来。最简单的方法是实现一个自定义序列化器和反序列化器来处理 JSON。

    [JsonConverter(typeof(EpochDateTimeConverter))]
    public DateTime Timestamp { get; set; }
    

    我建议您通过 Working with Dates in Azure Cosmos DBExample

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多