简体   繁体   English

如何:从 Cosmos DB 获取最新(最新)文档?

[英]How To: Get latest (newest) document from Cosmos DB?

I have a document database that has various properties including one named "SensorCaptureTime".我有一个文档数据库,它具有各种属性,包括一个名为“SensorCaptureTime”的属性。 I would like to retrieve only the newest document based on this property.我想只检索基于此属性的最新文档。 So far, all I have figured out is how to retrieve all the documents sorted by SensorCaptureTime and then loop through them taking the last document in the list.到目前为止,我所知道的只是如何检索按 SensorCaptureTime 排序的所有文档,然后遍历它们以获取列表中的最后一个文档。 Here's my current code:这是我当前的代码:

            SensorData mostRecentSensorReadingDocument = null;

            // TODO: There has to be a better way to do this... just not sure what it is yet :-)
            var queryable = documentClient.CreateDocumentQuery<SensorData>(
                collectionUri,
                new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
                .OrderBy(b => b.SensorCaptureTime)
                .AsDocumentQuery();

            while (queryable.HasMoreResults)
            {
                foreach (SensorData sensorReadingsDocument in await queryable.ExecuteNextAsync<SensorData>())
                {
                    mostRecentSensorReadingDocument = sensorReadingsDocument;
                }
            }

            return mostRecentSensorReadingDocument;

I am assuming there is a way to request a single document, based on something like an aggregate function (eg Max), from a (Cosmos) document database without having to loop through the entire result set.我假设有一种方法可以从(Cosmos)文档数据库中请求单个文档,基于诸如聚合 function(例如 Max)之类的东西,而无需遍历整个结果集。 Unfortunately, I haven't been able to find a working example.不幸的是,我找不到一个可行的例子。

Thanks!谢谢!

Is there only one of these for the entire database or is there one for each device that has data written?整个数据库只有一个,还是每个写入数据的设备都有一个?

In either case, the problem with this approach is that over time the query will get increasingly more expensive and slow, especially if this is a cross partition query.在任何一种情况下,这种方法的问题在于,随着时间的推移,查询将变得越来越昂贵和缓慢,特别是如果这是一个跨分区查询。 A better approach may be to use Change Feed and update another collection that only contains a single record for every device that contains the latest reading.更好的方法可能是使用更改提要并更新另一个集合,该集合仅包含包含最新读数的每个设备的单个记录。 Whenever a new reading is inserted into Cosmos DB, Change Feed gets fired, you would then read change feed and do an Upsert on a second collection to update the relevant record with the latest reading.每当将新读数插入 Cosmos DB 时,就会触发 Change Feed,然后您将读取 change feed 并对第二个集合执行 Upsert 以使用最新读数更新相关记录。 This way, each time you need that value it is simply a point-read which is super efficient.这样,每次您需要该值时,它只是一个非常有效的点读。

Another option that may work is to use a composite index with the device id and SensorCaptureTime with Desc order and then do a Top 1 on the collection to get the last item inserted.另一个可行的选择是使用具有设备 id 的复合索引和具有 Desc 顺序的 SensorCaptureTime,然后在集合上执行 Top 1 以插入最后一个项目。

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

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