Mongo db java 驱动查询转换

Mongo db java driver query convert

我有如下数据结构

[{
    "id": "1c7bbebd-bc3d-4352-9ac0-98c01d13189d",
    "version": 0,
    "groups": [
        {
            "internalName": "Admin group",
            "fields": [
                {
                    "internalName": "Is verified",
                    "uiProperties": {
                        "isShow": true
                    }
                },
                {
                    "internalName": "Hide",
                    "uiProperties": {
                        "isHide": false
                    }
                },
                ...
            ]
        },
        ...
    ]     
},
{
    "id": "2b7bbebd-bc3d-4352-9ac0-98c01d13189d",
    "version": 0,
    "groups": [
        {
            "internalName": "User group",
            "fields": [
                {
                    "internalName": "Is verified",
                    "uiProperties": {
                        "isShow": true
                    }
                },
                {
                    "internalName": "Blocked",
                    "uiProperties": {
                        "isBlocked": true
                    }
                },
                ...
            ]
        },
        ...
    ]     
},
...
]

字段的内部名称可以重复。我想按 group.field.internalName 分组并剪切数组(用于分页)并获得如下输出:

{
"totalCount": 3,
"items": [
    {
         "internalName": "Blocked"
    },
    {
         "internalName": "Hide"
    },
    {
         "internalName": "Is verified"
    }
]}

我写了一个有效的查询,

db.layouts.aggregate(
{ 
    $unwind : "$groups" 
},
{ 
    $unwind : "$groups.fields" 
},
{
    $group: {
        "_id" : {
            "internalName" : "$groups.fields.internalName",
        },
        "internalName" : {
            $first : "$groups.fields.internalName"
        }
    }
},
{
    $group: {
        "_id" : null,
        "items" : {
            $push : "$$ROOT"
        },
        "totalCount" : {
            $sum : 1
        }
    }
},
{
    $project: {
        "items" : {
            $slice : [ "$items", 0, 20 ]
        },
        "totalCount": 1
    }
})

但是我在将它翻译成 java api 时遇到了问题。请注意,我需要使用 mongoTemplate 方法。这是我所拥有的以及我被打动的地方

    final List<AggregationOperation> aggregationOperations = new ArrayList<>();
    aggregationOperations.add(unwind("groups"));
    aggregationOperations.add(unwind("groups.fields"));

    aggregationOperations.add(
            group("groups.fields.internalName")
                .first("groups.fields.internalName").as("internalName")
    );

    aggregationOperations.add(
            group()
                 .push("$$ROOT").as("fields")
                 .sum("1").as("totalCount") // ERROR only string ref can be placed, but i need a number?
    );

    aggregationOperations.add(
            project()
                  .andInclude("totalCount")
                  .and("fields").slice(size, page * size)
    );
    final Aggregation aggregation = newAggregation(aggregationOperations);
    mongoTemplate.aggregate(aggregation, LAYOUTS, FieldLites.class).getMappedResults()

对于这个查询,我遇到了 sum() 的问题,因为我只能通过 api(但需要一个数字)放置一个字符串引用,并且在项目操作中出现异常 java.lang.IllegalArgumentException:无效引用 'totalCount'!] 根本原因

你能帮我翻译这个查询吗?

您可以使用count

group()
    .push("$$ROOT").as("fields")
    .count().as("totalCount")