0
私はSpringデータでMongoDB(3.4)を使用しています。また、ルックアップ操作を使用すると不正な結果が表示されます。スプリングデータmongodb参照dbrefで
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"}]
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);
結果を次のように私はルックアップないときである
@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には、以下のデータが読み込まれます。次のように コレクション操作がある
[{ "_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"}]}]
すべての都市がリストされているので、上記の結果は正しくありません。
私は別のフィールドにルックアップを行う場合は、私は正しい結果を得る
LookupOperation lookup = LookupOperation.newLookup().from("empAddress").localField("city").foreignField("city").as("emp_loc");
結果は次のようになります
[{ "_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"}]}]
私はIDを使用してルックアップするとき、私は第二の結果を得るにはどうすればよいです?