2017-12-12 12 views
0

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" } 

これは、私がしたいことは

{ 
    "_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" } 
    ] 
} 

I MongoDBの

に保存されている。この構造を作成できるようにするには、従業員のコレクションです「集約のため、このような例を見ましたが、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

以下のコードを試すことができます。

GraphLookupOperation graphLookupOperation = GraphLookupOperation.builder() 
    .from("employees") 
    .startWith("reportsTo") 
    .connectFrom("reportsTo") 
    .connectTo("name") 
    .as("reportingHierarchy"); 
Aggregation aggregation = Aggregation.newAggregation(graphLookupOperation); 
List<Document> results = mongoOperations.aggregate(aggregation, "employees", Document.class).getMappedResults() 
+0

これは良いことですが、.asにはエラーがあり、アクセスできません。 – Andrew

+0

1.10.6 spring mongoにこのようなエラーはありません。あなたがいる春のmongoのバージョンは何ですか? – Veeram

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春のMongoのだと思います。今、私はそれが欲しいように見えない "reportingHierarchy"の問題を持っています。それは私に名前を与えていない。私が欲しくない_classも含むreportsToとreportingHierarchyだけです。

関連する問題