2016-11-03 33 views
1

我有一個子文檔是一個父文檔的數組。 「設備」

在該數組中,我有一個屬性是Date屬性。

我想找個誰包含determinated日期子子文檔這樣的父母文件:

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

我已經試過這在蒙戈外殼程序(和查詢是好的,它返回我想要的) :

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

謝謝。

+0

請添加你到目前爲止嘗試過的。 – Veeram

回答

6

我設法解決我的問題與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.<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 array aggregations operator?

我的項目是在春季啓動1.4 .0.RELEASE,但該版本沒有AggregationExpression接口PUBLIC,所以我只是更新到1.4.1.RELEASE,我沒有工作。