Mongodb将对象值转换为顶级字符串字段

问题描述 投票:0回答:1

我有以下查询mongoDB

{
    "name": "juan",
    "class": {
        "name": "person"  // is the field of another collection.
    }
}

我想要实现的是以下内容

{
    "name": "juan",
    "class": "person"
}

但是我需要所有人的支持才能实现以上目标。

mongodb bson
1个回答
0
投票

我假设您要更新多个文档,那么下面的查询将很有帮助:

db.collection.update(
  { },
  [
    {
      $set: {
        'class': '$class.name'
      }
    }
  ],
  { multi:true }
)
© www.soinside.com 2019 - 2024. All rights reserved.