Mongodb查询为:
db.getCollection('country2').aggregate([{"$lookup":{"from":"state2","localFiel
d":"_id","foreignField":"countryId","as":"state2"}}]).pretty()
返回此结果:
{
"_id" : ObjectId("5b2b520333a04107677b5ce1"),
"countryName" : "India",
"state2" : [
{
"_id" : ObjectId("5b2b515b33a04107677b5cdb"),
"stateName" : "UP",
"countryId" : ObjectId("5b2b520333a04107677b5ce1")
},
{
"_id" : ObjectId("5b2b516033a04107677b5cdc"),
"stateName" : "MP",
"countryId" : ObjectId("5b2b520333a04107677b5ce1")
}
]
}
尽管原始结果包含预期结果,但聚合查询返回空白的MappedResults。 State2是我的Java Pojo。
public List<BasicDBObject> getAllData() {
LookupOperation lookUpOprn = LookupOperation.newLookup()
.from("state2")
.localField("_id")
.foreignField("countryId")
.as("state2");
org.springframework.data.mongodb.core.aggregation.AggregationOptions options = Aggregation.newAggregationOptions().cursor(new BasicDBObject()).build();
Aggregation aggregation = Aggregation.newAggregation(lookUpOprn).withOptions(options);
List<BasicDBObject> list = mongoOperations.aggregate(aggregation, "country2", BasicDBObject.class).getMappedResults();
return list;
}
可以帮忙吗?