如何将 Mongodb 聚合(展开、匹配和项目)查询转换为 java 代码

How to convert Mango DB aggregation (unwind,match and project) query to java code

我想将此查询转换为 java 代码,其中包含聚合和条件,使用 $unwind 获取单个文档并使用 $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 的示例,它运行良好

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)