0
我正在使用MongoDB(3.4)与Spring数据。当我使用查找操作时,我得到不正确的结果。 收集操作如下:弹簧数据与dbref的mongodb查找
@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填充了以下数据
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);
结果是:
[{ "_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在第二个结果?