使用Spring Data在$ project MongoDB中使用$ filter

我有一个子文档,它是父文档的数组。 “设备”

在那个数组中,我有一个属性是Date属性。

我想通过确定的日期找到包含子子文档的父文档,如下所示:

{ "_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

谢谢。

我设法用Spring引导版本1.4.1.RELEASE解决了我的问题,我这样做了:

 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. asList("$$device.evaluationDate", date))); return new BasicDBObject("$filter", filterExpression); } }).as("devices") ); AggregationResults list = mongoOperations.aggregate(aggregation, MyClass.class, MyClass.class); 

我基于此阐述了: Spring Data MongoDb是否支持$ filter数组聚合运算符?

我的项目是在Spring boot 1.4.0.RELEASE上,但是那个版本没有AggregationExpression接口PUBLIC,所以我刚刚更新到1.4.1.RELEASE并且我确实工作了。