$filter inside $project MongoDB Using Spring Data($project MongoDB 中的 $filter 使用 Spring Data)
问题描述
我有一个子文档,它是父文档的数组.设备"
I have a subdocument that is an array of a parent document. "devices"
在该数组中,我有一个属性是 Date 属性.
In that array I've got a property which is a Date property.
我想按这样的确定日期查找包含子子文档的父文档:
I want to find the parents documents who contains the child subdocuments by determinated date like this:
{
"_id" : ObjectId("5818fa596969a1339093a7da"),
"fecha" : ISODate("2016-11-01T05:00:00.000Z"),
"spot" : "5808e3926969a126c8365c94",
"devices" : [
{
"evaluationDate" : ISODate("2016-11-01T20:26:00.000Z"),
"seenTimesCounter" : 0,
"category" : "PRE_PASAJERO",
"status" : "NO_CONECTADO"
},
{
"evaluationDate" : ISODate("2016-11-01T20:26:00.000Z"),
"seenTimesCounter" : 0,
"category" : "PRE_PASAJERO",
"status" : "NO_CONECTADO"
},
{
"evaluationDate" : ISODate("2016-11-01T20:26:00.000Z"),
"seenTimesCounter" : 0,
"category" : "PRE_PASAJERO",
"status" : "NO_CONECTADO"
},
{
"evaluationDate" : ISODate("2016-11-01T20:26:00.000Z"),
"seenTimesCounter" : 0,
"category" : "PRE_PASAJERO",
"status" : "NO_CONECTADO"
},
{
"evaluationDate" : ISODate("2016-11-01T20:26:00.000Z"),
"seenTimesCounter" : 0,
"category" : "PRE_PASAJERO",
"status" : "NO_CONECTADO"
},
{
"evaluationDate" : ISODate("2016-11-01T20:26:00.000Z"),
"seenTimesCounter" : 0,
"category" : "PRE_PASAJERO",
"status" : "NO_CONECTADO"
}
]
}
我在 mongo shell 上试过这个(查询很好,它返回我想要的):
db.getCollection('spotMovimientos').aggregate([
{$match:{'devices.evaluationDate':ISODate("2016-11-01T20:26:00.000Z")}},
{$project: {
'devices':{$filter:{
input:'$devices',
as:'device',
cond: {$eq: ['$$device.evaluationDate', ISODate("2016-11-01T20:26:00.000Z")]}
}
} }
}
])
谁能详细说明一下?我需要帮助将其转换为 SPRING DATA
Can anyone elaborate on this? I need help to convert this to SPRING DATA
谢谢.
推荐答案
我设法用 Spring boot 版本 1.4.1.RELEASE 解决了我的问题,我做到了:
I managed to solve my problem with the Spring boot Version 1.4.1.RELEASE and I did this:
Aggregation aggregation = newAggregation(
match(Criteria.where("devices.evaluationDate").is(date)),
project().and(new AggregationExpression() {
@Override
public DBObject toDbObject(AggregationOperationContext aggregationOperationContext) {
DBObject filterExpression = new BasicDBObject();
filterExpression.put("input", "$devices");
filterExpression.put("as", "device");
filterExpression.put("cond", new BasicDBObject("$eq", Arrays.<Object> asList("$$device.evaluationDate", date)));
return new BasicDBObject("$filter", filterExpression);
}
}).as("devices")
);
AggregationResults<SpotMovimientos> list = mongoOperations.aggregate(aggregation,
MyClass.class, MyClass.class);
我基于此详细阐述:Does Spring Data MongoDb support$filter 数组聚合运算符?
我的项目在 Spring boot 1.4.0.RELEASE 上,但是那个版本没有 AggregationExpression 接口 PUBLIC,所以我刚刚更新到 1.4.1.RELEASE 并且我确实工作了.
My project was on Spring boot 1.4.0.RELEASE but that version didnt have the AggregationExpression interface PUBLIC, so i just updated to 1.4.1.RELEASE and i did work.
这篇关于$project MongoDB 中的 $filter 使用 Spring Data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:$project MongoDB 中的 $filter 使用 Spring Data


基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01