在mongoose中嵌套内部数组

时间:2016-10-04 07:04:40

标签: angularjs arrays node.js mongodb mongoose

我的mongoDb内容如下:

[{
"_id": {
    "$oid": "57c6699711bd6a0976cabe8a"
},
"ID": "1111",
"FullName": "AAA",
"Category": [
    {
        "CategoryId": {
            "$oid": "57c66ebedcba0f63c1ceea51"
        },
        "_id": {
            "$oid": "57e38a8ad190ea1100649798"
        },
        "Value": [
            {
                "Name": ""
            }
        ]
    },
    {
        "CategoryId": {
            "$oid": "57c3df061eb1e59d3959cc40"
        },
        "_id": {
            "$oid": "57e38a8ad190ea1100649797"
        },
        "Value": [
            [
                "111",
                "XXXX",
                "2005"
            ],
            [
                "1212",
                "YYYY",
                "2000"
            ],
            [
                "232323",
                "ZZZZZ",
                "1999"
            ]
        ]
    }
]
},{
    "_id": {
        "$oid": "57c6699711bd6a0976cabe8a"
    },
    "ID": "1111",
    "FullName": "BBB",
    "Category": [
        {
            "CategoryId": {
                "$oid": "57c66ebedcba0f63c1ceea51"
            },
            "_id": {
                "$oid": "57e38a8ad190ea1100649798"
            },
            "Value": [
                {
                    "Name": ""
                }
            ]
        },
        {
            "CategoryId": {
                "$oid": "57c3df061eb1e59d3959cc40"
            },
            "_id": {
                "$oid": "57e38a8ad190ea1100649797"
            },
            "Value": [
                [
                    "4444",
                    "XXXX",
                    "2005"
                ],
                [
                    "7777",
                    "GGGG",
                    "2000"
                ],
                [
                    "8888",
                    "ZZZZZ",
                    "1999"
                ]
            ]
        }
    ]
}]

这里我有一个名为'ResumeCategory'的数组,其中包含具有不同类别ID的对象。

我需要 1.选择id为“57c3df061eb1e59d3959cc40”的类别 2.上面选择的类别包含值作为数组 3.从值数组中,我必须根据第二个值对元素进行分组,并且需要获取用户名列表

例如输出:

[{
    'CategoryName': 'XXXX',
    'Users': ['AAA', 'BBB']
},
{
    'CategoryName': 'YYYY',
    'Users': ['AAA']
},
{
    'CategoryName': 'ZZZZZ',
    'Users': ['AAA', 'BBB']
},
{
    'CategoryName': 'GGGG',
    'Users': ['BBB']
}]

我尝试使用聚合函数进行分组,如下所示:

resume.aggregate([
  {$match: {'Category.CategoryId': new ObjectId('57c3df191eb1e59d3959cc43')}},
  {$unwind: '$Category'},
  {$unwind: '$Category.Value'},
  {$match: {'Category.CategoryId': new ObjectId('57c3df191eb1e59d3959cc43')}},
  {$group: { _id: '$Category.Value', count: {$sum: 1}}},
  {$project: {'_id':0, TagValue: '$_id',count: '$count'}}
],function(err, resData){
        res.send(resData);
});

从上面的查询中,它基于Value数组进行分组。它需要将具有三个元素的数组分组。任何人都可以帮助根据内部数组值(单个元素,即值[1])对值进行分组,并获得所需的结果。

提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以在项目阶段使用$arrayElemAt(在3.2版中引入)来选择数组中的第二个元素。

db.resume.aggregate([
                        {"$unwind":"$Category"}, 
                        {"$match":{"Category.CategoryId" :ObjectId("57c3df061eb1e59d3959cc40")}}, 
                        {"$unwind":"$Category.Value"}, 
                        {"$project":{"FullName":1, "secondVal" : {"$arrayElemAt": ["$Category.Value",1]} }}, 
                        {"$group":{"_id":"$secondVal", "Users":{"$addToSet":"$FullName"}}}, 
                        {"$project":{"CategoryName":"$_id", "_id": 0, "Users":1}}
                    ])

示例输出:

{ "Users" : [ "AAA", "BBB" ], "CategoryName" : "XXXX" }
{ "Users" : [ "AAA" ], "CategoryName" : "YYYY" }
{ "Users" : [ "BBB" ], "CategoryName" : "GGGG" }
{ "Users" : [ "AAA", "BBB" ], "CategoryName" : "ZZZZZ" }

答案 1 :(得分:1)

您需要 $unwind "Category.Value"字段两次,因为它有双嵌套数组:

resume.aggregate([
    { "$match": { "Category.CategoryId": "57c3df061eb1e59d3959cc40" } },
    { "$unwind": "$Category" },
    { "$match": { "Category.CategoryId": "57c3df061eb1e59d3959cc40" } },    
    { "$unwind": "$Category.Value" },// <-- unwind twice
    { 
        "$unwind": {
            "path": "$Category.Value",
            "includeArrayIndex": "idx" 
        }
    },
    { "$match": { "idx": 1 } },
    {
        "$group": {
            "_id": "$Category.Value",
            "Users": { "$push": "$FullName" },
            "count": { "$sum": 1 }
        }
    },
    { "$project": { "_id": 0, "CategoryName": "$_id", "Users": 1, "count": 1 } } 
], function(err, resData){
    res.send(resData);
});