2017-07-28 3 views
0

Ich verwende MongoDB (3.4) mit Spring Data. Und ich erhalte falsche Ergebnisse, wenn ich den Suchvorgang verwende. Der Sammelvorgang sind wie folgt:spring data mongodb lookup mit dbref

@Document 
public class Employee { 
    @Id 
    String id; 
    String name; 
    @DBRef 
    EmpAddress address; 

    String city;  //redundant, but intentionally 
    //getters and setters 
} 

@Document 
public class EmpAddress { 
    @Id 
    String id; 

    String city; 
    //getters and setters 
} 

DB mit den folgenden Daten

Employees [{ "_id" : { "$oid" : "597b557cfe4b9104e8409f2a"} , "_class" : "com.example.Employee" , "name" : "PKM1" , "city" : "NYC" , "address" : { "$ref" : "empAddress" , "$id" : { "$oid" : "597b557bfe4b9104e8409f25"}}}, { "_id" : { "$oid" : "597b557cfe4b9104e8409f2b"} , "_class" : "com.example.Employee" , "name" : "PKM2" , "city" : "SFO" , "address" : { "$ref" : "empAddress" , "$id" : { "$oid" : "597b557bfe4b9104e8409f26"}}}, { "_id" : { "$oid" : "597b557cfe4b9104e8409f2c"} , "_class" : "com.example.Employee" , "name" : "PKM3" , "city" : "LA" , "address" : { "$ref" : "empAddress" , "$id" : { "$oid" : "597b557bfe4b9104e8409f27"}}}, { "_id" : { "$oid" : "597b557cfe4b9104e8409f2d"} , "_class" : "com.example.Employee" , "name" : "PKM4" , "city" : "SFO" , "address" : { "$ref" : "empAddress" , "$id" : { "$oid" : "597b557bfe4b9104e8409f28"}}}, { "_id" : { "$oid" : "597b557cfe4b9104e8409f2e"} , "_class" : "com.example.Employee" , "name" : "PKM5" , "city" : "NYC" , "address" : { "$ref" : "empAddress" , "$id" : { "$oid" : "597b557bfe4b9104e8409f29"}}}] 



Cities [{ "_id" : { "$oid" : "597b557bfe4b9104e8409f25"} , "_class" : "com.example.EmpAddress" , "city" : "NYC"}, { "_id" : { "$oid" : "597b557bfe4b9104e8409f26"} , "_class" : "com.example.EmpAddress" , "city" : "SFO"}, { "_id" : { "$oid" : "597b557bfe4b9104e8409f27"} , "_class" : "com.example.EmpAddress" , "city" : "LA"}, { "_id" : { "$oid" : "597b557bfe4b9104e8409f28"} , "_class" : "com.example.EmpAddress" , "city" : "SFO"}, { "_id" : { "$oid" : "597b557bfe4b9104e8409f29"} , "_class" : "com.example.EmpAddress" , "city" : "NYC"}] 

bevölkert ist, wenn ich Lookup als

MatchOperation match = Aggregation.match(Criteria.where("name").is("PKM1")); 
LookupOperation lookup = LookupOperation.newLookup().from("empAddress").localField("address.$id").foreignField("id").as("emp_loc"); 
Aggregation aggregation = Aggregation.newAggregation(match, lookup); 
AggregationResults<DBObject> result = mongoTemplate.aggregate(aggregation, "employee", DBObject.class); 

Die Ergebnisse folgt:

[{ "_id" : { "$oid" : "597b557cfe4b9104e8409f2a"} , "_class" : "com.example.Employee" , "name" : "PKM1" , "city" : "NYC" , "address" : { "$ref" : "empAddress" , "$id" : { "$oid" : "597b557bfe4b9104e8409f25"}} , "emp_loc" : [ { "_id" : { "$oid" : "597b557bfe4b9104e8409f25"} , "_class" : "com.example.EmpAddress" , "city" : "NYC"} , { "_id" : { "$oid" : "597b557bfe4b9104e8409f26"} , "_class" : "com.example.EmpAddress" , "city" : "SFO"} , { "_id" : { "$oid" : "597b557bfe4b9104e8409f27"} , "_class" : "com.example.EmpAddress" , "city" : "LA"} , { "_id" : { "$oid" : "597b557bfe4b9104e8409f28"} , "_class" : "com.example.EmpAddress" , "city" : "SFO"} , { "_id" : { "$oid" : "597b557bfe4b9104e8409f29"} , "_class" : "com.example.EmpAddress" , "city" : "NYC"}]}] 

Das obige Ergebnis ist falsch, da alle Städte aufgelistet werden.

Allerdings, wenn ich Lookup auf einem anderen Feld bekomme ich das richtige Ergebnis

LookupOperation lookup = LookupOperation.newLookup().from("empAddress").localField("city").foreignField("city").as("emp_loc"); 

Das Ergebnis ist wie folgt:

[{ "_id" : { "$oid" : "597b557cfe4b9104e8409f2a"} , "_class" : "com.example.Employee" , "name" : "PKM1" , "city" : "NYC" , "address" : { "$ref" : "empAddress" , "$id" : { "$oid" : "597b557bfe4b9104e8409f25"}} , "emp_loc" : [ { "_id" : { "$oid" : "597b557bfe4b9104e8409f25"} , "_class" : "com.example.EmpAddress" , "city" : "NYC"} , { "_id" : { "$oid" : "597b557bfe4b9104e8409f29"} , "_class" : "com.example.EmpAddress" , "city" : "NYC"}]}] 

Wie kann ich in der zweiten Folge, wenn ich Lookup-ID verwenden ?

Antwort

0

Es ist ein Problem von DBref. Seit mongoDB 3.4 kann DBRef nicht in der Aggregationspipeline verwendet werden, außer in der $ match-Phase.

Die Abfrage gibt alle Ergebnisse zurück, weil die ID null ist, hatte ich das gleiche Problem.

Sie sollten manuelle Referenzen erstellen, um dieses Problem zu vermeiden. Sie können mehr darüber in dieser Antwort lesen: https://stackoverflow.com/a/41677055/4023844

Verwandte Themen