Spring数据mongodb聚合框架无效的引用

时间:2017-02-28 21:42:25

标签: java spring mongodb spring-data-mongodb

我用:  1. Mongo 3.4v数据库。 (我是mongo的新手)  2. Spring boot 1.5.1.RELEASE(spring data mongodb 1.10.0.RELEASE)

我的数据库包含两个系列“类别”和“广告”(广告)

分类

{
"_id" : ObjectId("58a4b39eca0cdc1ba88d9bf5"),
"_class" : "com.mongotest.model.Category",
"title" : {
    "en" : "My title"
},
"parentId" : ObjectId("58b1fca82dc4b81365110fe4")
}

广告

{
"_id" : ObjectId("58a62e4a27dccb1bec742052"),
"_class" : "com.mongotest.model.Ads",
"categoryId" : ObjectId("58a4b39eca0cdc1ba88d9bf5"),
"description" : "Description",
"phone" : "29890000",
"email" : "john@inbox.com",
"price" : "65.0"
}

我创建了mongo查询,它返回每个子类别的类别数组和每个子类别的广告数。

这个mongo查询看起来像这样(工作100%我在问题之前仔细检查):

db.categories.aggregate([
{
    $match : {
       "parentId": null     
    }    
},
{
    $lookup:{
        from: "categories",
        localField: "_id",
        foreignField: "parentId",
        as: "subcategories"
    }   
},
{ 
    $unwind : { 
        path : "$subcategories",
        preserveNullAndEmptyArrays : true
    }
},
{
    $lookup:{
        from: "ads",
        localField: "subcategories._id",
        foreignField: "categoryId",
        as: "subcategories.adsList"
    }
},
{
    $project : {                
        "title" : "$title",
        "subcategories._id" : "$subcategories._id",
        "subcategories.title" : "$subcategories.title",
        "subcategories.adsCount" : {$size : "$subcategories.adsList"}
    }
},
{
    $group : { 
        "_id" : "$_id",
        "title" : { $first : "$title" },
        "subcategories" : { $push: "$subcategories" }
    }
}])

这是我对Spring mongo数据聚合框架的翻译:

    AggregationOperation match = Aggregation.match(Criteria.where("parentId").is(null));
    AggregationOperation lookup = Aggregation.lookup("categories", "_id", "parentId", "subcategories");
    AggregationOperation unwindSubcategories = Aggregation.unwind("subcategories", true);
    AggregationOperation lookupAds = Aggregation.lookup("ads", "subcategories._id", "categoryId", "subcategories.adsList");
    AggregationOperation project = Aggregation.project().and("subcategories._id").as("subcategories._id").and("title").as("title").and("subcategories.title").as("subcategories.title").and("subcategories.adsList").size().as("adsCount");
    AggregationOperation group = Aggregation.group("_id").first("title").as("title").push("subcategories").as("subcategories");
    Aggregation aggregation = Aggregation.newAggregation(match, lookup, unwindSubcategories, lookupAds, project, group); //Invalid reference 'subcategories'
    AggregationResults<CategoryTO> aggregate = mongoTemplate.aggregate(aggregation, Category.class, CategoryTO.class); 

问题出在小组操作

在这种情况下,我会收到无效的敬畏'子类别'。如果在project()操作中它可用,我无法理解为什么'subategories'引用在push()方法中不可用?!

我如何解决?

我也知道相关问题 Spring Data MongoDB - Aggregation Framework - invalid reference in group Operation

Spring Data MongoDB: aggregation framework - sort with nested property throws invalid reference

非常感谢您的反馈。

0 个答案:

没有答案