如何在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"}}}
}
}
]
)