我有一个mongo聚合组查询:
db.wizard.aggregate(
{
$group: {
_id: "$title",
versions: { $push: {version:"$version", author:"$author", dateAdded:"$dateAdded"}}
}
})
我需要在Java Spring-Data-MongoDB中使用此查询,我目前的解决方案如下所示:
Aggregation agg = Aggregation.newAggregation(
Aggregation.group("title").
push("version").as("versions")
);
问题是我不知道如何为推送方法(版本,作者,dateAdded)添加更多字段。 是否可以使用Spring-Data-MongoDB?
答案 0 :(得分:10)
您可以直接将BasicDbObject传递给任何聚合管道阶段。
Aggregation agg = newAggregation(
group("title").
push(new BasicDBObject
("version", "$version").append
("author", "$author").append
("dateAdded", "$dateAdded")).as("versions"));
答案 1 :(得分:2)
注意:MongoDBversión2.6 - 3.0(兼容的Java驱动程序)
我已经扩展org.springframework.data.mongodb.core.aggregation.AggregationOperation
类来实现自定义toDBObject
方法:
public class GenericAggregationOperation implements AggregationOperation {
private String operator;
private DBObject query;
/**
* Default constructor.
*
* @param operator MongoDB operator ($group, $sort, $project, etc..)
* @param query MongoDB aggregation query step string
*/
public GenericAggregationOperation(String operator, String query) {
this(operator, (DBObject) JSON.parse(query));
}
/**
* Default constructor.
*
* @param operator MongoDB operator ($group, $sort, $project, etc..)
* @param query MongoDB aggregation query step DBObject
*/
public GenericAggregationOperation(String operator, DBObject query) {
this.operator = operator;
this.query = query;
}
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return new BasicDBObject(operator, query);
}
}
在你的情况下,它将是:
List<AggregationOperation> list = new ArrayList<AggregationOperation>();
lista.add(new GenericAggregationOperation("$group", "{ \"_id\": \"$title\", \"versions\": { \"$push\": { \"version\":\"$version\", \"author\":\"$author\", \"dateAdded\":\"$dateAdded\"}} }"));
TypedAggregation<EpisodeIndexDto> agg = Aggregation.newAggregation(
YourClassTitleVersion.class, list);
mongoOperations.aggregate(agg, YourClassTitleVersion.class,
YourClassTitleVersion.class).getMappedResults();
希望这会对你有所帮助。
答案 2 :(得分:0)
在新版本的spring-data-mongodb:2.x.x中,AggregationOperation需要返回Document而不是DBObject,因此更新后的类将是:
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
public class GenericAggregationOperation implements AggregationOperation {
private String operator;
private DBObject query;
public GenericAggregationOperation(String operator, DBObject query) {
this.operator = operator;
this.query = query;
}
public GenericAggregationOperation(String operator, String query) {
this(operator, BasicDBObject.parse(query));
}
@Override
public Document toDocument(AggregationOperationContext context) {
return new Document(operator, query);
}
}
另外,为了更易于使用,我将添加实用程序接口(java 8+,对于Java 7或更低版本,您可以将其转换为类utils):
import com.mongodb.DBObject;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
public interface GenericAggregationUtils {
static AggregationOperation aggregate(String operation, String query) {
return new GenericAggregationOperation(operation, query);
}
static AggregationOperation aggregate(String operation, DBObject query) {
return new GenericAggregationOperation(operation, query);
}
}
然后我们可以将接口静态导入到我们的类中:
import static com.example.mongodb.aggregation.GenericAggregationUtils.*;
并将其与其他Spring数据AggregationOperation一起在聚合管道中使用,如下所示:
Aggregation aggregation = newAggregation(YourDocCollection.class,
aggregate("$group", "{ \"_id\": \"$title\", \"versions\": { \"$push\": { \"version\":\"$version\", \"author\":\"$author\", \"dateAdded\":\"$dateAdded\"}} }"),
sort(Sort.Direction.ASC, "title"),
...
);