简体   繁体   English

更新数组中的 MongoDB hash 字段名称

[英]Update MongoDB hash field name in array

I have a MongoDB collection with items like that:我有一个 MongoDB 集合,其中包含以下项目:

_id: object id
h: value
w: value
v: array
     {
         p: value
         n: value
         c: array
     },
     {
     },...

I want to rename the d field of all values of all items to something else, let's say f .我想将所有项目的所有valuesd字段重命名为其他名称,比如说f

How can that be done?怎么可能呢? (Basically, can it be done?) (基本上可以做到吗?)

For anyone who might be interested, this is exactly how I did it (MongoDB version: 3.6.3)对于任何可能感兴趣的人,这正是我所做的(MongoDB 版本:3.6.3)

db.getCollection('COLLECTION_NAME').find({"v": { $exists : 1 }}).forEach(
    function( doc ){

        for( i=0; i < doc.v.length; ++i ) {
            doc.v[i].f = doc.v[i].c;
            delete doc.v[i].c;
        }

        db.getCollection('COLLECTION_NAME').update(
         { _id : doc._id }
         , doc 
        );
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM