Cosmos 文档查询以查找每种类型的最新记录
Cosmos Document query to locate latest record per type
我有一个 collection,我在其中存储 timestamp
及其最新的 location
以及以下内容 class:
public class TrackingInfo
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("_partition_key")]
public string _PartitionKey { get; set; }
[JsonProperty("asset_id")]
public string AssetId { get; set; }
[JsonProperty("unix_timestamp")]
public double UnixTimestamp { get; set; }
[JsonProperty("timestamp")]
public string Timestamp { get; set; }
[JsonProperty("location")]
public Point Location { get; set; }
}
由 _PartitionKey
分区,其中包含如下结构:
tracking._PartitionKey = $"tracking_{tracking.AssetId.ToLower()}_{DateTime.Today.ToString("D")}";
似乎无法在 collection 上执行 Group by
。
有人可以帮我创建一个 SQL 文档查询来查找每个 AssetId
及其 Location
和 Timnestamp
记录数据时的最新条目。
更新 1:
如果我将 _PartitionKey
更改为代表每天如下所示:
tracking._PartitionKey = $"tracking_{DateTime.Today.ToString("D")}";
是否可以更轻松地获取所有资产及其最新跟踪记录?
Cosmos DB 不支持 group by
功能,您可以投赞成票 this。
提供一个third-party包[documentdb-lumenize供您参考,它支持按功能分组,它有.net
示例:
string configString = @"{
cubeConfig: {
groupBy: 'state',
field: 'points',
f: 'sum'
},
filterQuery: 'SELECT * FROM c'
}";
Object config = JsonConvert.DeserializeObject<Object>(configString);
dynamic result = await client.ExecuteStoredProcedureAsync<dynamic>("dbs/db1/colls/coll1/sprocs/cube", config);
Console.WriteLine(result.Response);
您可以按 assetId
列分组并获得最大值 timestamp
。
根据我的评论,我的建议是用不同的方式解决您的问题。
假设:您有大量的 assetIds 并且事先不知道这些值:
- 拥有一份代表您资产最新状态的文件
- 有另一个文档代表您资产的位置事件
- 每当有新的位置事件时更新第一个文档
- 您可以将两种类型的文档放在同一个集合中或将它们分开 - 两种方法都有好处。我可能会把它们分开。
然后查询"what assets are within 1km of xxx" (Querying spatial types)
旁注:使用 assetId 作为 partitionKey 而不是组合键可能是个好主意。使用这样的密钥对查询非常不利
如果您只有很少的 assetId,则可以使用这些 assetId 通过使用时间戳字段并按时间戳字段排序来仅查找最新更新。这只会 return 最后一项
我有一个 collection,我在其中存储 timestamp
及其最新的 location
以及以下内容 class:
public class TrackingInfo
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("_partition_key")]
public string _PartitionKey { get; set; }
[JsonProperty("asset_id")]
public string AssetId { get; set; }
[JsonProperty("unix_timestamp")]
public double UnixTimestamp { get; set; }
[JsonProperty("timestamp")]
public string Timestamp { get; set; }
[JsonProperty("location")]
public Point Location { get; set; }
}
由 _PartitionKey
分区,其中包含如下结构:
tracking._PartitionKey = $"tracking_{tracking.AssetId.ToLower()}_{DateTime.Today.ToString("D")}";
似乎无法在 collection 上执行 Group by
。
有人可以帮我创建一个 SQL 文档查询来查找每个 AssetId
及其 Location
和 Timnestamp
记录数据时的最新条目。
更新 1:
如果我将 _PartitionKey
更改为代表每天如下所示:
tracking._PartitionKey = $"tracking_{DateTime.Today.ToString("D")}";
是否可以更轻松地获取所有资产及其最新跟踪记录?
Cosmos DB 不支持 group by
功能,您可以投赞成票 this。
提供一个third-party包[documentdb-lumenize供您参考,它支持按功能分组,它有.net
示例:
string configString = @"{
cubeConfig: {
groupBy: 'state',
field: 'points',
f: 'sum'
},
filterQuery: 'SELECT * FROM c'
}";
Object config = JsonConvert.DeserializeObject<Object>(configString);
dynamic result = await client.ExecuteStoredProcedureAsync<dynamic>("dbs/db1/colls/coll1/sprocs/cube", config);
Console.WriteLine(result.Response);
您可以按 assetId
列分组并获得最大值 timestamp
。
根据我的评论,我的建议是用不同的方式解决您的问题。
假设:您有大量的 assetIds 并且事先不知道这些值:
- 拥有一份代表您资产最新状态的文件
- 有另一个文档代表您资产的位置事件
- 每当有新的位置事件时更新第一个文档
- 您可以将两种类型的文档放在同一个集合中或将它们分开 - 两种方法都有好处。我可能会把它们分开。
然后查询"what assets are within 1km of xxx" (Querying spatial types)
旁注:使用 assetId 作为 partitionKey 而不是组合键可能是个好主意。使用这样的密钥对查询非常不利
如果您只有很少的 assetId,则可以使用这些 assetId 通过使用时间戳字段并按时间戳字段排序来仅查找最新更新。这只会 return 最后一项