【问题标题】:$filter inside $project MongoDB Using Spring Data$project MongoDB 中的 $filter 使用 Spring Data
【发布时间】:2017-03-16 11:25:42
【问题描述】:

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

在该数组中,我有一个属性是 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

谢谢。

【问题讨论】:

    标签: java spring mongodb spring-data spring-mongo


    【解决方案1】:

    这就是它在 spring-data-monogodb-2.0.10 (Spring Boot 2) 中的制作方式

    Aggregation aggregation = newAggregation(
                    match(Criteria.where("devices.evaluationDate").is(date)),
                    project().and(new AggregationExpression() {
                        @Override
                        public Document toDocument(AggregationOperationContext aggregationOperationContext) {
                            Document filterExpression = new Document();
                            filterExpression.put("input", "$devices");
                            filterExpression.put("as", "device");
                            filterExpression.put("cond", new Document("$eq", Arrays.<Object> asList("$$device.evaluationDate", date)));
                            return new Document("$filter", filterExpression);
                        }
                    }).as("devices")
            );
    

    【讨论】:

      【解决方案2】:

      我设法用 Spring boot 版本 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?

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

      【讨论】:

        猜你喜欢
        • 2018-11-09
        • 2019-09-29
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-25
        • 1970-01-01
        相关资源
        最近更新 更多