Java 如何在Spring中编写这个Mongo聚合查询?
我在MongoDB中有一个聚合查询,当我直接在shell中运行它时,它就可以工作了。下面是shell查询:Java 如何在Spring中编写这个Mongo聚合查询?,java,spring,mongodb,spring-data,spring-data-mongodb,Java,Spring,Mongodb,Spring Data,Spring Data Mongodb,我在MongoDB中有一个聚合查询,当我直接在shell中运行它时,它就可以工作了。下面是shell查询: db.MyCollection.aggregate([ {$match: {_id: {$in: ['A', 'B', 'C']}}}, {$project: {"versions": "$nested.field.version"}}, {$unwind: "$versions"}, {$group: {_id: "$_id", "maxVersion":
db.MyCollection.aggregate([
{$match: {_id: {$in: ['A', 'B', 'C']}}},
{$project: {"versions": "$nested.field.version"}},
{$unwind: "$versions"},
{$group: {_id: "$_id", "maxVersion": {$max: "$versions"}}}
])
如您所见,这将执行以下操作:
Aggregation aggregation = newAggregation(
match(Criteria.where("_id").in(listOfIds))
,project().and("versions").nested(bind("versions", "nested.field.version"))
,unwind("versions")
,group("_id").max("versions").as("maxVersion")
);
当我尝试在调试模式下运行代码时,我可以看到我实际上在newAggregation上得到一个IllegalArgumentException,表示它无法计算。如果我用$group子句注释掉这一行,那么我可以看到聚合变量的toString()表示形式,它揭示了$project子句的一个问题:
{
"aggregate" : "__collection__" ,
"pipeline" : [
{ "$match" : { "_id" : { "$in" : [ "A" , "B" , "C"]}}} ,
{ "$project" : { "versions" : { "versions" : "$nested.field.version"}}} ,
{ "$unwind" : "$versions"}
]
}
显然,这与我的意图不符,因此我没有得到正确的语法。但是我觉得SpringMongoops语法不是很直观,它们的文档也不是很好
如果不首先包括对和()
的调用,我看不到任何调用nested()
方法的方法。我认为这是主要的问题,因为它在那里的筑巢量加倍了。这里有没有SpringMongoops英雄可以帮助我正确地编写等价的Java代码
编辑:这是我正在使用的集合的快照:管道不是必需的,因为您仍然可以对嵌套字段执行,因此此聚合管道可以产生与当前集合相同的结果:
db.MyCollection.aggregate([
{
"$match": {
"_id": { "$in": ['A', 'B', 'C'] }
}
},
{ "$unwind": "$nested.field" },
{
"$group": {
"_id": "$_id",
"maxVersion": { "$max": "$nested.field.version" }
}
}
])
Spring数据MongoDB聚合等价物:
Aggregation agg = newAggregation(
match(Criteria.where("_id").in(ids)),
unwind("nested.field"),
group("_id").max("nested.field.version").as("maxVersion")
);
回到当前聚合,您需要在嵌套的.field
数组上,而不是嵌套的.field.version
字段上,因为该字段是字符串,而不是数组:
db.MyCollection.aggregate([
{$match: {_id: {$in: ['A', 'B', 'C']}}},
{$project: {"fields": "$nested.field"}},
{$unwind: "$fields"},
{$group: {_id: "$_id", "maxVersion": {$max: "$fields.version"}}}
])
Sprind Data MongoDB等效项如下所示:
Aggregation agg = newAggregation(
match(Criteria.where("_id").in(ids)),
project().and("nested.field").as("fields")
unwind("fields"),
group("_id").max("fields.version").as("maxVersion")
);
在修复下划线错误之前使用map reduce方法。 比如:
GroupBy-GroupBy=GroupBy.key(“用户id”)
.initialDocument(“{total:0,used:0}”)
.reduceFunction(“函数(curr,result){result.total++;if(curr.status==1){result.used++;}result.userID=curr.user_id;”;
GroupByResults yourResultInfo=
mongoTemplate.group(标准,其中(“用户id”)。在(用户id)中,
“您的集合名称”、groupBy、YourResult.class);
给你的结果分类{
私有字符串用户标识;
私人长型总计=0升;
私人长期使用=0升;
//getter和setter`在此处输入代码
}
Spring在聚合操作中执行字段引用验证时,将uu用作数组的通配符,并拆分snake_u案例字段
为了避免验证,您可以使用以下MongoTemplate方法,该方法执行聚合,而无需字段转换和验证
public <O> AggregationResults<O> aggregate(Aggregation aggregation, String collectionName, Class<O> outputType)
publicAggregationResults聚合(聚合聚合聚合、字符串集合名称、类输出类型)
嗯,您的回答是正确的。但不幸的是,在我的特殊情况下,由于Spring数据的错误,它不起作用。我总是在发布前清理我的字段名,但事实上,我在一些嵌套字段名中有下划线,看起来Spring数据在执行s时会对下划线进行某种拆分一些引用完整性检查导致它失败。因此,感谢您的努力,但不幸的是,Spring数据太多,无法在我的用例中立即使用。@SoaperGEM不用担心,很遗憾您没有成功。
public <O> AggregationResults<O> aggregate(Aggregation aggregation, String collectionName, Class<O> outputType)