Is it possible to rename _id field after mongo's group aggregation?

后端 未结 4 1116
青春惊慌失措
青春惊慌失措 2020-12-30 19:34

I have a query like this (simplified):

db.report.aggregate([{
        $match: {
            main_id: ObjectId(\"58f0f67f50c6af16709fd2c7\")
        }
    },          


        
4条回答
  •  天涯浪人
    2020-12-30 20:02

    From mongo v3.4 you could use $addFields in conjunction with $project to avoid to write all the fields in $project that could be very tedious.

    This happen in $project because if you include specifically a field, the other fields will be automatically excluded.

    Example:

    { 
      $addFields: { my_new_id_name: "$_id" }
    },
    {
      $project: { _id: 0 }
    }
    

提交回复
热议问题