这个问题已经在这里有了答案: > lookup in mongodb aggregation 1个
在最新版本的spring上,我已经看到了很多关于堆栈溢出的问题,这表明spring-data-mongodb中不支持此操作,而新的spring-data-mongodb 1.10.0中对此操作也没有任何支持
db.orders.aggregate([
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
])
解决方法:
从1.9版本开始就提供查找支持.
LookupOperation lookupOperation = LookupOperation.newLookup().
from("inventory").
localField("item").
foreignField("sku").
as("inventory_docs");
Aggregation aggregation = Aggregation.newAggregation(lookupOperation);
List<BasicDBObject> results = mongoOperations.aggregate(aggregation, "orders", BasicDBObject.class).getMappedResults();