首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Spring数据的$filter内部$project MongoDB

使用Spring数据的$filter内部$project MongoDB
EN

Stack Overflow用户
提问于 2016-11-03 00:33:08
回答 2查看 8.7K关注 0票数 2

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

在这个数组中,我有一个属性,它是一个日期属性。

我想按确定的日期查找包含子文档的父母文档,如下所示:

代码语言:javascript
运行
复制
{
"_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上尝试过了(查询很好,它返回我想要的):

代码语言:javascript
运行
复制
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

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-03 16:55:23

我设法解决了Spring引导版本1.4.1.RELEASE的问题,并完成了以下操作:

代码语言:javascript
运行
复制
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 1.4.0.RELEASE,但是那个版本没有AggregationExpression接口公共,所以我只是更新到1.4.1.RELEASE,我做了工作。

票数 8
EN

Stack Overflow用户

发布于 2019-03-12 08:46:40

这是在spring-data-monogodb-2.0.10 (Spring 2)中制作的。

代码语言:javascript
运行
复制
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")
        );
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40391987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档