10

是否可以使用聚合框架按数组的特定元素进行分组?

这样的文件是这样的:

{
  name: 'Russell',
  favourite_foods: [
    { name: 'Pizza', type: 'Four Cheeses' },
    { name: 'Burger', type: 'Veggie'}
  ],
  height: 6
}

我可以得到一份最喜欢的食物(即索引为 0 的食物)的不同列表,以及最喜欢食物的最高人的身高?

像这样的东西(尽管它不起作用,因为数组索引访问点符号似乎在聚合框架中不起作用):

db.people.aggregate([
  { $group : { _id: "$favourite_foods.0.name", max_height: { $max : "$height" } } }
])
4

4 回答 4

17

似乎您正在依靠每个人最喜欢的食物排在第一位。如果是这样,您可以利用聚合框架运算符。

这是您可以使用的管道:

db.people.aggregate(
[
    {
        "$unwind" : "$favourite_foods"
    },
    {
        "$group" : {
            "_id" : {
                "name" : "$name",
                "height" : "$height"
            },
            "faveFood" : {
                "$first" : "$favourite_foods"
            }
        }
    },
    {
        "$group" : {
            "_id" : "$faveFood.name",
            "height" : {
                "$max" : "$_id.height"
            }
        }
    }
])

在此示例数据集上:

> db.people.find().pretty()
{
    "_id" : ObjectId("508894efd4197aa2b9490741"),
    "name" : "Russell",
    "favourite_foods" : [
        {
            "name" : "Pizza",
            "type" : "Four Cheeses"
        },
        {
            "name" : "Burger",
            "type" : "Veggie"
        }
    ],
    "height" : 6
}
{
    "_id" : ObjectId("5088950bd4197aa2b9490742"),
    "name" : "Lucy",
    "favourite_foods" : [
        {
            "name" : "Pasta",
            "type" : "Four Cheeses"
        },
        {
            "name" : "Burger",
            "type" : "Veggie"
        }
    ],
    "height" : 5.5
}
{
    "_id" : ObjectId("5088951dd4197aa2b9490743"),
    "name" : "Landy",
    "favourite_foods" : [
        {
            "name" : "Pizza",
            "type" : "Four Cheeses"
        },
        {
            "name" : "Pizza",
            "type" : "Veggie"
        }
    ],
    "height" : 5
}
{
    "_id" : ObjectId("50889541d4197aa2b9490744"),
    "name" : "Augie",
    "favourite_foods" : [
        {
            "name" : "Sushi",
            "type" : "Four Cheeses"
        },
        {
            "name" : "Pizza",
            "type" : "Veggie"
        }
    ],
    "height" : 6.2
}

你会得到这些结果:

{
    "result" : [
        {
            "_id" : "Pasta",
            "height" : 5.5
        },
        {
            "_id" : "Pizza",
            "height" : 6
        },
        {
            "_id" : "Sushi",
            "height" : 6.2
        }
    ],
    "ok" : 1
}
于 2012-10-25T01:41:38.593 回答
5

看起来目前无法从聚合数组中提取特定元素: https ://jira.mongodb.org/browse/SERVER-4589

于 2012-10-24T20:20:15.783 回答
3

只需在使用后添加有关结果的更多信息"$wind"


文档 :

> db.people.find().pretty()
{
    "_id" : ObjectId("508894efd4197aa2b9490741"),
    "name" : "Russell",
    "favourite_foods" : [
        {
            "name" : "Pizza",
            "type" : "Four Cheeses"
        },
        {
            "name" : "Burger",
            "type" : "Veggie"
        }
    ],
    "height" : 6
},
...


聚合:

db.people.aggregate([{
    $unwind: "$favourite_foods"
}]);


结果 :

{
    "_id" : ObjectId("508894efd4197aa2b9490741"),
    "name" : "Russell",
    "favourite_foods" :{
            "name" : "Pizza",
            "type" : "Four Cheeses"
    },
    "height" : 6
},
{
    "_id" : ObjectId("508894efd4197aa2b9490741"),
    "name" : "Russell",
    "favourite_foods" : {
            "name" : "Burger",
            "type" : "Veggie"
    },
    "height" : 6
}


另外:
如果一个集合记录中有两个以上的数组字段,我们可以使用“$project”阶段来指定数组字段。

db.people.aggregate([
    {
        $project:{
            "favourite_foods": 1
        }
    },
    {
        $unwind: "$favourite_foods"
    }
]);
于 2016-05-06T07:22:08.303 回答
2

我认为您可以使用 $project 和 $unwind 运算符(如果这不是您想要完成的,请告诉我):

> db.people.aggregate(
   {$unwind: "$favourite_foods"}, 
   {$project: {food : "$favourite_foods", height: 1}}, 
   {$group : { _id: "$food", max_height: { $max : "$height" } } })


{
    "result" : [
        {
            "_id" : {
                "name" : "Burger",
                "type" : "Veggie"
            },
            "max_height" : 6
        },
        {
            "_id" : {
                "name" : "Pizza",
                "type" : "Four Cheeses"
            },
            "max_height" : 6
        }
    ],
    "ok" : 1
}

http://docs.mongodb.org/manual/applications/aggregation/

于 2012-10-24T23:22:37.990 回答