聚合$ group返回字段作为字符串而不是数组

时间:2013-11-21 12:26:34

标签: mongodb

我正在进行以下聚合:

db.col.aggregate([
        {'$unwind': "$students"},
        {'$group':
            {
                "_id" : "$_id",
                'students' :
                    { '$push' :
                       {
                        'name' : '$students.name',
                        'school' : '$students.school',
                        'age' : '$students.age',
                       }
                    },
                'zipcode' :
                    {'$addToSet':
                        '$zipcode'
                    }
            }
        },
        {'$project':
            {
                '_id' : 0 ,
                'students' : 1,
                'zipcode': 1
            }
        }
])

给出了:

{
   "result" : [
      {
        "students" : [{
                         "name" : "john",
                         "school" : 102,
                         "age" : 10
                      },
                      {
                         "name" : "jess",
                         "school" : 102,
                         "age" : 11
                      },
                      {
                         "name" : "barney",
                         "school" : 102,
                         "age" : 7
                      }
                    ],
        "zipcode" : [63109]
      }
   ],
   "ok" : 1
}

是否可以让它返回"zipcode" : 63109

在实践中,这就是我希望得到聚合的返回结果:

{
   "result" : [
      {
        "students" : [{
                         "name" : "john",
                         "school" : 102,
                         "age" : 10
                      },
                      {
                         "name" : "jess",
                         "school" : 102,
                         "age" : 11
                      },
                      {
                         "name" : "barney",
                         "school" : 102,
                         "age" : 7
                      }
                    ],
        "zipcode" : 63109
      }
   ],
   "ok" : 1
}

我在$ group中尝试"zipcode" : "$zipcode",但正如documentation所说:

Every $group expression must specify an _id field. 
In addition to the _id field, $group expression can include 
computed fields. These other fields must use one of the following accumulators:

$addToSet
$first
$last
$max
$min
$avg
$push
$sum

有解决方法吗?

1 个答案:

答案 0 :(得分:1)

zipcode值作为数组返回,因为您正在使用显式返回数组的$addToSet运算符:

  

返回在该组文档中所选字段中找到的所有值的数组。

如果您打算按zipcode进行分组,则可以将其用作分组_id,例如:

db.col.aggregate([

        // Create a stream of documents from the students array
        {'$unwind': "$students"},

        // Group by zipcode
        {'$group':
            {
                "_id" : "$zipcode",
                'students' :
                    { '$push' :
                       {
                        'name' : '$students.name',
                        'school' : '$students.school',
                        'age' : '$students.age',
                       }
                    },
            }
        },

        // rename _id to zipcode
        {'$project':
            {
                '_id' : 0,
                'zipcode' : '$_id',
                'students' : 1,
            }
        }
])

或者,您可以使用$first$last等组操作符返回单个zipcode值,但这可能不是您所追求的。