Home > OS >  Mongodb view with $group and $project
Mongodb view with $group and $project

Time:04-26

Hi I have created a view by using $group inside with _id in that I have added fields which I need to display through $project.

{
        $group: {
          _id:{
            "name":"$name",
            "type":"$type"
          }
}
{
$project: {name:1, type:1}
}

But I am getting response in mongodb as _id:object If expanding then am able to see the result, I wanted to see result as {name: abc, type:type1}. Can anyone please help me on this?

CodePudding user response:

try use: $project,

Passes along the documents with only the specified fields to the next stage in the pipeline

{$project: 
    { 
        id: 0, 
        name: "$_id.name", 
        type: "$_id.type" 
    } 
}
  • Related