How to output/ map an array with only the required fields after a filter in mongodb? [duplicate]

♀尐吖头ヾ 提交于 2020-07-22 05:51:26

问题


I am using this data:

{
"world": "Comic",
"characters": [
  {
    "character": "megaman",
    "type":"hero",
    "code":"123"
  },
  {
    "character": "dr willow",
    "type":"villain",
    "code":"1234"
  },
  {
    "character": "spiderman",
    "type":"hero",
    "code":"12345"
  },
  {
    "character": "venom",
    "type":"villain",
    "code":"123456"
  }
  
]
}

With this code:

    db.collection.aggregate([
        {$addFields:{
            array_hero:{
                $filter:{
                    input: "$characters",
                    cond: {$eq:["$$this.type","hero"]}
                }
            },
            array_villain:{
                $filter:{
                    input: "$characters",
                    cond: {$eq:["$$this.type","villain"]}
                }
            },
        }}
    ])

the output is:

    {
    "array_hero": [
        {
        "character": "megaman",
        "type": "hero",
        "code": "123"
        },
        {
        "character": "spiderman",
        "type": "hero",
        "code": "12345"
        }
    ],
    "array_villain": [
        {
        "character": "dr willow",
        "type": "villain",
        "code": "1234"
        },
        {
        "character": "venom",
        "type": "villain",
        "code": "123456"
        }
    ]
    }

I want to have this output, where the arrays are only built with the characters.codelike this:

   {
    "array_hero": [
       "123","12345"
    ],
    "array_villain": [
       "1234","123456"
    ]
   }

how can I do it?


回答1:


Your aggregation query is almost there. Just added a $map stage to pick the required fields/attributes.

db.collection.aggregate([
  {
    $addFields: {
      array_hero: {
        $filter: {
          input: "$characters",
          cond: {
            $eq: [
              "$$this.type",
              "hero"
            ]
          }
        }
      },
      array_villain: {
        $filter: {
          input: "$characters",
          cond: {
            $eq: [
              "$$this.type",
              "villain"
            ]
          }
        }
      },
      
    },
    
  },
  {
    $project: {
      array_hero: {
        $map: {
          input: "$array_hero",
          as: "hero",
          in: "$$hero.code"
        }
      },
      array_villain: {
        $map: {
          input: "$array_villain",
          as: "villain",
          in: "$$villain.code"
        }
      },
      
    }
  }
])

Play link




回答2:


Say we have saved following document into a collection named collection.

db.collection.save(
  {
    "world": "Comic",
    "characters": [
      {
        "character": "megaman",
        "type":"hero",
        "code":"123"
      },
      {
        "character": "dr willow",
        "type":"villain",
        "code":"1234"
      },
      {
        "character": "spiderman",
        "type":"hero",
        "code":"12345"
      },
      {
        "character": "venom",
        "type":"villain",
        "code":"123456"
      }
      
    ]
  }
)

Then the following command would return codes grouped by different types of characters that the world field is Comic in this collection:

db.collection.aggregate([
  {
    $match: {
      world: "Comic"
    },
  },
  {
    $unwind: "$characters"
  },
  {
    $group: {
      _id: "$characters.type",
      codes: {$addToSet: "$characters.code"}
    }
  },
  {
    $project: {
      _id: 0,
      type: "$_id",
      codes: 1
    }
  }
])

The result running above command in mongo shell is as following:

{ "codes" : [ "12345", "123" ], "type" : "hero" }
{ "codes" : [ "123456", "1234" ], "type" : "villain" }


来源:https://stackoverflow.com/questions/62938971/how-to-output-map-an-array-with-only-the-required-fields-after-a-filter-in-mong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!