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 ?