我正在寻找一种在Resful Web API中使用Java实现graphlookup的方法。我正在尝试像MongoDB(https://docs.mongodb.com/v3.4/reference/operator/aggregation/graphLookup/)
那样实现层次结构 { "_id" : 1, "name" : "Dev" }
{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }
{ "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot" }
{ "_id" : 4, "name" : "Andrew", "reportsTo" : "Eliot" }
{ "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" }
{ "_id" : 6, "name" : "Dan", "reportsTo" : "Andrew" }
这是员工集合,我想要的是能够创建存储在MongoDB中的这个结构
{
"_id" : 1,
"name" : "Dev",
"reportingHierarchy" : [ ]
}
{
"_id" : 2,
"name" : "Eliot",
"reportsTo" : "Dev",
"reportingHierarchy" : [
{ "_id" : 1, "name" : "Dev" }
]
}
{
"_id" : 3,
"name" : "Ron",
"reportsTo" : "Eliot",
"reportingHierarchy" : [
{ "_id" : 1, "name" : "Dev" },
{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }
]
}
我已经看过这样的例子用于聚合,但在graphlookup
上没有Aggregation agg = newAggregation(
match(Criteria.where("pageId").is("2210")),
unwind("postIds"),
group("_id").sum("1").as("sum")
//project("$sum").and("pageId").previousOperation()
);
有没有办法让graphlookup成为这样的格式?而不是使用match,unwind,group,我可以使用GraphLookupOperation,然后使用get map result。
答案 0 :(得分:0)
AggregationOperation aggregationOperation = new AggregationOperation() {
@Override public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
DBObject graphLookup = new BasicDBObject(
"from", "individual").append(
"startWith", "$reportsTo").append(
"connectFromField", "reportsTo").append(
"connectToField", "firstName").append(
"maxDepth", 2).append(
"as", "reportingHierarchy");
return new BasicDBObject("$graphLookup", graphLookup);
此代码允许解决方法,因为我使用旧版本,我认为1.10.0 spring mongo。现在我有" reportingHierarchy"看起来不像我想要的那样。它没有给我一个名字。只有reportsTo和reportingHierarchy,也包括我不想要的_class。