1

I have a collection with this structure:

{
    "name": "1",
    "array1": [
        {
            "arrayname1A" : "A",
             "array2": [
                 { "value": "1" },
                 { "value": "3" }
             ]
        },
        {
             "arrayname1B": "B",
             "array2": [
                 { "value": "5" },
             ]
        }
    ]
},
{
    "name": "2",
    "array1": [
        {
            "arrayname1A": "A",
            "array2": [
                { "value": "1" },
                { "value": "7" }
            ]
        }
    ]
}

How can I extract the unique "value" from every different array2? The end result I am looking for would be something like "1","3","5","7" with no repeated values.

4

1 回答 1

0

只需使用distinct()方法和点符号来访问“值”字段。

db.collection.distinct('array1.array2.value')

产生:

[ "1", "3", "5", "7" ]
于 2016-05-29T13:33:48.777 回答