我的包含查询和项目命令的聚合查询可在mongo终端上运行,但是通过Spring Boot我无法使其正常运行。
db.categories.aggregate([
{
$lookup:
{
from: "brands",
localField: "brands",
foreignField: "_id",
as: "brands"
}
},
{
$project: {"brands.categories": 0}
}
])
这是弹簧靴的方式
fun lookupOperation(): MutableList<CategoryDocument> {
val lookupOperation = LookupOperation.newLookup()
.from("brands")
.localField("brands")
.foreignField("_id")
.`as`("brands")
val project = Aggregation.project().andExclude("brands.categories")
val aggregation = Aggregation.newAggregation(lookupOperation, project)
return mongoTemplate.aggregate(aggregation, "categories", CategoryDocument::class.java).mappedResults
}