【问题标题】:MongoDB: Rename Field in A Collection (Document and Subdocuments)MongoDB:重命名集合中的字段(文档和子文档)
【发布时间】:2019-09-24 09:50:07
【问题描述】:

使用 MongoDB 3.6 我的文档结构如下 `

{
    "_id" : ObjectId("5d88"),
    "Equipments" : [ 
        {
            "InnerEquipments" : {
                "AssetId" : 678
            },
            "AssetID" : 456
        }
    ],
    "AssetID" : 123
}

我想在所有级别将字段从 AssetID/AssetId 重命名为 Asset_ID。 我怎样才能用 mongo shell 做到这一点。

【问题讨论】:

  • 如果不迭代每个文档并递归遍历该文档并重命名键,然后替换每个文档(或者可能动态构造 @ 987654322@ 和 $unset 语句)。你介意更具体吗?即也许声明您的文档始终具有一致的结构,而不仅仅是一揽子“在所有级别”声明?如果你能做到这一点,那么任务就简单多了。
  • 没错,没有具体的结构。设备 -> 设备 -> 设备(设备可能有也可能没有设备,而设备又可能有一系列设备)
  • 我的意思是可能 1. 顶级字段,2. Equipments 数组成员中的字段,3. 每个Equipments 的InnerEquipments 中的字段大批; 没有其他变体。点是一组可能位置的有限且可预测(意味着没有其他近无限嵌套可能性),要简单得多实现比完全递归的文档遍历。而且效率也更高。
  • 花时间没关系。这将是一次性的活动。没有明确的结构,否则我只会使用重命名。

标签: mongodb mongodb-query mongo-shell


【解决方案1】:

以下代码可以解决问题:

// Getting all documents from the collection
var data = db.collection.find({},{"_id":0}).toArray();

// Converting the data into JSON string
var string = JSON.stringify(data);

// Replacing all variations of assetid with Asset_ID
string = string.replace(/assetid/ig,"Asset_ID");

// Removing existing documents from collection
db.collection.remove({});

// Converting the string back to JSON array and inserting it into the DB
db.collection.insertMany(JSON.parse(string));

之前:

{
    "_id" : ObjectId("5d89e9ab0558a18dd9cfc03a"),
    "Equipments" : [ 
        {
            "InnerEquipments" : {
                "AssetId" : 678
            },
            "AssetID" : 456
        }
    ],
    "AssetID" : 123
}

之后:

{
    "_id" : ObjectId("5d89eea80558a18dd9cfc03b"),
    "Equipments" : [
        {
            "InnerEquipments" : {
                "Asset_ID" : 678
            },
            "Asset_ID" : 456
        }
    ],
    "Asset_ID" : 123
}

【讨论】:

  • 我想我只需稍加修改就可以使用它。而不是单独的每个文档,将整个集合导出为文本文件。并利用“AssetID:”使其不包含其他字段或数据。
  • @AshishBagaddeo 是的,当然! :)
【解决方案2】:

您需要为此编写一个脚本。
我还没有测试过。相应地进行更改。

db.collection.find({}).forEach(function(doc) {
    if(doc['AssetID']) {
      doc['Asset_ID'] = doc['AssetID'];
      delete doc['AssetID'];
    } else if (doc['AssetId']) {
      doc['Asset_ID'] = doc['AssetId'];
      delete doc['AssetId']
    }
    if(doc.Equipments && doc.Equipments.length) {
      doc.Equipments.forEach(function(rec) {
        if(rec['AssetID']) {
          rec['Asset_ID'] = rec['AssetID'];
          delete rec['AssetID'];
        } else if (rec['AssetId']) {
          rec['Asset_ID'] = rec['AssetId'];
          delete rec['AssetId']
        }

        if(rec['InnerEquipments']['AssetID']) {
          rec['InnerEquipments']['Asset_ID'] = rec['InnerEquipments']['AssetID'];
          delete rec['InnerEquipments']['AssetID'];
        } else if (rec['InnerEquipments']['AssetId']) {
          rec['InnerEquipments']['Asset_ID'] = rec['InnerEquipments']['AssetId'];
          delete rec['InnerEquipments']['AssetId']
        }
      })
    }
    db.collection.update({'_id':doc._id},doc);
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-09
    • 2015-10-19
    • 2012-03-04
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2013-07-12
    相关资源
    最近更新 更多