Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在mongodb中分组,但不包括_id_Mongodb - Fatal编程技术网

如何在mongodb中分组,但不包括_id

如何在mongodb中分组,但不包括_id,mongodb,Mongodb,因此,我想得出以下结论: { "id" : 888789999, "name" : "Malaysian with Attendance Allowance", } 但是我试过了 {$group : { 'id' : '$profiles.id', 'name' : {$first:'$profiles.name'}, }} 如果我得到一个错误: “errmsg”:“字段“id”必须是累加器对象” 您可以尝试这样做,按配置文件id分组并获取名字,如果需要不带下

因此,我想得出以下结论:

{
    "id" : 888789999,
    "name" : "Malaysian with Attendance Allowance",
}
但是我试过了

{$group : {
    'id' : '$profiles.id',
    'name' : {$first:'$profiles.name'},
}}
如果我得到一个错误:

“errmsg”:“字段“id”必须是累加器对象”


您可以尝试这样做,按配置文件id分组并获取名字,如果需要不带下划线的id,则添加项目

在组中,除_id外,其他字段应具有累加或聚合

{$group : {
    '_id' : '$profiles.id',
    'name' : {$first:'$profiles.name'},
}}
如果您不想包括_id,那么

{$group : {
        _id: null,
        'id' :  {$first:'$profiles.id'},// any accumulation which you need
        'name' : {$first:'$profiles.name'},
    }}
db.collectionName.aggregate(
   [
      {
        $group : {
            id : { profiles: { $profiles: "$id" }},
name : {$first:{profiles:{$profiles:"name"}}}
        }
      }
   ]
)