Java 使用1.3.5版本的Spring data mongodb,Spring data mongodb聚合函数不起作用
我需要使用spring数据过滤mongodb中的文档,该数据包含下一个数组。我正在MongoShell上使用下面的聚合查询,它运行良好。 但当我通过springdata聚合操作触发该操作时,得到的是空响应。 工作mongo查询是:Java 使用1.3.5版本的Spring data mongodb,Spring data mongodb聚合函数不起作用,java,spring,mongodb,spring-data,spring-data-mongodb,Java,Spring,Mongodb,Spring Data,Spring Data Mongodb,我需要使用spring数据过滤mongodb中的文档,该数据包含下一个数组。我正在MongoShell上使用下面的聚合查询,它运行良好。 但当我通过springdata聚合操作触发该操作时,得到的是空响应。 工作mongo查询是: db.searchResource.aggregate({$match:{"_id" : ObjectId("53cf4e3dae92ac6561807f6d")}},{$project:{"rssSearchResponse.journeys":1}},{$unwi
db.searchResource.aggregate({$match:{"_id" : ObjectId("53cf4e3dae92ac6561807f6d")}},{$project:{"rssSearchResponse.journeys":1}},{$unwind : "$rssSearchResponse.journeys"},{$match:{"rssSearchResponse.journeys.stops":0}});
我正在使用但不工作的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);
typedaggeration aggregation=aggregation.newAggregation(SearchResource.class,aggregation.match(新条件(“\u id”).is(新对象id(searchId)),
聚合项目(“rssSearchResponse.journes”),
聚合.释放(“rssSearchResponse.行程”),
Aggregation.match(新标准(“rssSearchResponse.journes.stops”).is(0))
);
AggregationResults=mongoOperations.aggregate(聚合,JourneyInformation.class);
我已尝试破坏此聚合函数,它能够投影rssSearchResponse.journies,但在$unwind之后,它返回空结果
非常感谢您的帮助。您觉得:
@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;
}
}
那么:
@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;
}
}
在Mongo聚合中不起作用
您必须将rs.id
转换为ObjectId
match(where("_id").is(new ObjectId(rs.id)))
在Mongo聚合中不起作用
您必须将rs.id
转换为ObjectId
match(where("_id").is(new ObjectId(rs.id)))
您尝试过最新的1.4.2.版本吗?谢谢Oliver,它通过将版本更新到1.4.x来工作。您尝试过最新的1.4.2.版本吗?谢谢Oliver,它通过将版本更新到1.4.x来工作。谢谢Thomas的回复,我尝试过您上面提到的相同版本,但它在解卷时不起作用,但是当我升级到1.4.x版本的spring data mongodb时,它对我有效。感谢Thomas的回复,我尝试了与您上面提到的相同的方法,但它在解卷时不起作用,但当我升级到1.4.x版本的spring data mongodb时,它对我有效。