如何将Mango DB聚合(展开,匹配和项目)查询转换为Java代码

我想使用$ unwind将该查询转换为具有Aggregation和条件的Java代码,以获取单个文档,并使用$ project以获得个性化响应。

db.getcollection('WarrantyCategory').aggregate([{
    $unwind: '$subWarrantyCategories'
},{
    $match: {
        $and: [{
                $and: [{
                        'subWarrantyCategories.filtersAllowed.type._id': 'CODE_actE'},{
                        $and: [{'subWarrantyCategories.filtersAllowed.valuesAutorized.id': 'ORT'},{'subWarrantyCategories.filtersAllowed.valuesAutorized.operator': '='}]

                    }
                ]
            },{

                $and: [{'subWarrantyCategories.filtersAllowed.type._id': 'TYP_accORD'},{
                        $and: [{'subWarrantyCategories.filtersAllowed.valuesAutorized.id': 'O'},{'subWarrantyCategories.filtersAllowed.valuesAutorized.operator': '='}]

                    }]
            }
        ]
    }
},{
    $project: {

        _id: 0,label: 1,code: 1,labelSCat: '$subWarrantyCategories.label',CodeSCat: '$subWarrantyCategories._id'}}])

我使用@Query尝试了一个示例,但没有$ unwind,它工作得很好

mingshiyy483 回答:如何将Mango DB聚合(展开,匹配和项目)查询转换为Java代码

spring数据mongodb的示例代码

List<AggregationOperation> stages = new ArrayList<>();

stages.add(unwind("$subWarrantyCategories"));

List<Criteria> andList = new ArrayList<>();

Criteria c1 = new Criteria("subWarrantyCategories.filtersAllowed.type._id").is("CODE_ACTE");
Criteria c2  new Criteria("subWarrantyCategories.filtersAllowed.valuesAutorized.id").is("ORT");
Criteria c3 =  new Criteria("subWarrantyCategories.filtersAllowed.valuesAutorized.operator").is("=");
Criteria c4  new Criteria("subWarrantyCategories.filtersAllowed.type._id").is("TYP_ACCORD");
Criteria c5 =  new Criteria("subWarrantyCategories.filtersAllowed.valuesAutorized.id").is("0");
Criteria c5 =  new Criteria("subWarrantyCategories.filtersAllowed.valuesAutorized.operator").is("=");


andList.add(new Criteria().andOperator(c1,new Criteria().andOperator(c2,c3)));
andList.add(new Criteria().andOperator(c4,new Criteria().andOperator(c5,c6)));

stages.add(match(new Criteria().andOperator(andList.toArray(new Criteria[andList.size()]))));

ProjectionOperation projection = project("_id","label","code").and("$subWarrantyCategories.label").as("labelSCat").and("$subWarrantyCategories._id").as("CodeSCat");

stages.add(projection);

newAggregation(stages)
本文链接:https://www.f2er.com/2871771.html

大家都在问