mongotemplate聚合具有独特的结果 [英] mongotemplate aggregation with unique results

查看:250
本文介绍了mongotemplate聚合具有独特的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mongotemplate,我的收藏如下:

I am using mongotemplate , my collection look like this:

    {
    "_id": "theid",
    "tag": "taga",
    "somefield": "some value",
    "fieldC": {
        "anotherfielad": "more data",
        "an_array": [
            {
                "a": "abc",
                "b": 5
            },
            {
                "a": "bca",
                "b": 44
            },
            {
                "a": "ddd",
                "b": 21
            }
        ]
    }
}

{
    "_id": "anotherid",
    "tag": "taga",
    "somefield": "some other value",
    "fieldC": {
        "anotherfielad": "other more data",
        "an_array": [
            {
                "a": "ccc",
                "b": 6
            },
            {
                "a": "abc",
                "b": 99
            },
            {
                "a": "ddd",
                "b": 21
            }
        ]
    }
}

我需要从$ fieldC.an_array.a获得一个唯一的结果 在这种情况下:("abc","bca","ddd","ccc")

I need to get a unique result from $fieldC.an_array.a in this case: ("abc","bca","ddd","ccc")

此查询有效:

[
    {
        "$match": {
            "tag": "taga"
        }
    },
    {
        "$unwind": "fieldC.an_array"
    },
    {
        "$group": {
            "_id": null,
            "array": {
                "$addToSet": "fieldC.an_array.a"
            }
        }
    }
]

但是我如何使用mongotemplate呢?

but how do I do it using mongotemplate ?

推荐答案

这就是您想要的 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆