简体   繁体   English

Spring数据mongodb聚合函数无法使用spring-data-mongodb的1.3.5-RELEASE

[英]Spring data mongodb aggregate function not working using 1.3.5-RELEASE of spring-data-mongodb

I have requirement to filter document in mongodb using spring data which contains nexted arrays of arrays. 我需要使用包含nexted数组数组的spring数据来过滤mongodb中的文档。 I am using following aggregation query on mongo shell and it is working fine. 我在mongo shell上使用以下聚合查询,它工作正常。 But when I am firing that through springdata aggregation operation, I am gettin empty response. 但是当我通过springdata聚合操作解雇时,我得到空的响应。 Working mongo query is: 工作mongo查询是:

db.searchResource.aggregate({$match:{"_id" : ObjectId("53cf4e3dae92ac6561807f6d")}},{$project:{"rssSearchResponse.journeys":1}},{$unwind : "$rssSearchResponse.journeys"},{$match:{"rssSearchResponse.journeys.stops":0}});

Spring data code which I am using and s not working: 我使用的Spring数据代码不起作用:

TypedAggregation<SearchResource> aggregation = Aggregation.newAggregation(SearchResource.class, Aggregation.match(new Criteria("_id").is(new ObjectId(searchId))),
                Aggregation.project("rssSearchResponse.journeys"),
                Aggregation.unwind("rssSearchResponse.journeys"),
                Aggregation.match(new Criteria("rssSearchResponse.journeys.stops").is(0))
                );
AggregationResults<SearchResource> result = mongoOperations.aggregate(aggregation, JourneyInformation.class);

I have tried breaking this aggregate function and it is able to project rssSearchResponse.journeys but after $unwind it returns empty result. 我试过打破这个聚合函数,它能够投射rssSearchResponse.journeys但是在$ unwind之后它返回空结果。

Any help is highly appreciated. 任何帮助都非常感谢。

How about: 怎么样:

@Test
public void foo() {

    mongoTemplate.dropCollection(RssSearchResponse.class);

    RssSearch rs = new RssSearch();
    rs.id  = "123";
    rs.rssSearchResponse = new RssSearchResponse(
            new Journey[] {new Journey("A", 1),new Journey("B", 0),new Journey("C", 0),new Journey("D", 1)}
    );

    mongoTemplate.insert(rs);

    Aggregation agg = newAggregation(RssSearch.class, //
          match(where("_id").is(rs.id)) //
        , project("rssSearchResponse.journeys") //
        , unwind("journeys") //
        , match(where("journeys.stops").is(0)) //
    );

    AggregationResults<DBObject> result = mongoTemplate.aggregate(agg, RssSearch.class, DBObject.class);
    System.out.println(result);

}

static class RssSearch{
    String id;
    RssSearchResponse rssSearchResponse;
}

static class RssSearchResponse{
    Journey[] journeys;

    public RssSearchResponse(Journey[] journeys) {
        this.journeys = journeys;
    }
}

static class Journey{
    String name;
    int stops;

    public Journey(String name, int stops) {
        this.name = name;
        this.stops = stops;
    }
}

This will return B and C. 这将返回B和C.

We generate the following aggregation command: 我们生成以下聚合命令:

{ 
    "aggregate" : "rssSearch" 
  , "pipeline" : [ 
     { "$match" : { "_id" : "123"}} 
   , { "$project" : { "journeys" : "$rssSearchResponse.journeys"}}
   , { "$unwind" : "$journeys"}
   , { "$match" : { "journeys.stops" : 0}}
  ]
}
 match(where("_id").is(rs.id)) 

Doesn't work in Mongo Aggregation . Mongo Aggregation不起作用。

You have to convert rs.id to ObjectId 您必须将rs.id转换为ObjectId

match(where("_id").is(new ObjectId(rs.id))) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM