聚合函数不添加新列 MongoDB

Aggregate function not adding new column MongoDB

当我在列下方执行时,它在添加列时给出结果,但是当我查询 table 时,新列没有显示。

db.getCollection("arcm_qc").aggregate([
 {
        $addFields: {
            MGE_ID: { $arrayElemAt: [{ "$split": ["$MCA Go", "-"] }, 0] }

        }
    }

])

您不能仅使用聚合在集合中添加字段。但是你可以像这样使用 aggregate in an update query

db.collection.update({/*the documents you want to update or empty to update all*/},
[
  {
    $addFields: {
      MGE_ID: {
        $arrayElemAt: [{"$split": ["$MCA Go","-"]},0]
      }
    }
  }
],
{
  multi: true
})

示例here