spring - Spring MongoDB 在数组上使用自定义表达式进行排序
问题描述
文件结构:
{
id: ObjectId("5ef1cd704b35c6d6698f2050")
date: 2021-12-15T11:03:00.000+00:00
priorityList: Array
0: "any-String-id-here"
1: ...
}
我想查找按具有以下条件的自定义表达式排序的文档:
- 起初包含给定字符串的文档
priorityList
- 按日期排序
我所做的
TypedAggregation<MyDocument> aggregation = newAggregation(MyDocument.class,
project("id")
.and("isInList").applyCondition(ConditionalOperators
.when(Criteria.where("priorityList").all("here-my-string-to-check"))
.then(1)
.otherwise(0)),
sort(Sort.by(
Sort.Order.desc("isInList"),
Sort.Order.desc("date"))));
mongoTemplate.aggregate(aggregation, "my-collection-name", String.class);
生产
com.mongodb.MongoCommandException:
Command failed with error 168 (InvalidPipelineOperator): 'Invalid $project ::
caused by :: Unrecognized expression '$all''
如何实现所需的排序条件?
解决方案
推荐阅读
- java - 在反序列化期间将 POJO 映射到具有不可变列表的案例类
- sql - 如何在 BigQuery 标准 SQL 中取消嵌套多个数组
- python - 我如何匹配遵循少数特定规则的字符串,例如使用 spacy 匹配器的某些特定字符串之后
- c# - 为什么我在 DataTemplate 中找不到元素?
- objective-c - 无法使用带有某些链接的 NSURLSession 下载图像
- shell - 递归查找文件大小大于 1 GB 的目录中的文件
- azure - consumer.azure.com 的 v3 API 大部分时间都无法返回 offerId
- navigationbar - SwiftUI navigationBarTitle - displayMode: .inline - 崩溃
- excel - 使用 Powershell 从 Excel 工作表中获取复选框?
- reactjs - Material-UI 中添加的自托管字体不起作用?