![](/img/trans.png)
[英]How to preserve the reference of elements from a collection for O(1) accessibility in Java
[英]How to efficiently get most recent elements from collection and preserve initial document structure
我正在尝试在 MongoDB(使用 Kotlin)中执行以下聚合,在 db 中具有以下结构:
{
"_id" : 214134152323423
"orderId" : 1,
"createdAt" : 2000-01-01,
"details" : "some very important note"
}
现在假设我可以拥有多个具有相同orderId
但createdAt
日期不同的文档,我想获取最新的文档并保留初始文档结构。
现在我有:
mongoTemplate
.aggregate(
newAggregation(
sort(
Sort.Direction.DESC, "createdAt"
),
group("orderId")
.first("createdAt").`as`("createdAt")
.first("details").`as`("details")
),
"order-collection",
String::class.java
)
.mappedResults
.forEach { println(it) }
这种方法正确吗? 是否可以在不对整个集合进行排序的情况下做到这一点?
主要思想:定期/每次order-collection
更新时,您插入/刷新order-collection-view
预先计算的数据。 这样,您可以获得最近的订单详细信息,而无需扫描整个order-collection
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.