What should be the mongodb update shell command

this is one of my documents in the collection

{   
    "_id": ObjectId("55e86e98f493590878bb45d7"),
    "KIDS_M_0_2" : "",
    "KIDS_F_0_2" : "",
    "KIDS_U_0_2" : "Y",
    "KIDS_M_3_5" : "",
    "KIDS_F_3_5" : "",
    "KIDS_U_3_5" : "Y",
    "KIDS_M_6_10" : "",
    "KIDS_F_6_10" : "",
    "KIDS_U_6_10" : "",
    "KIDS_M_11_15" : "",
    "KIDS_F_11_15" : "",
    "KIDS_U_11_15" : "",
    "KIDS_M_16_17" : "",
    "KIDS_F_16_17" : "",
    "KIDS_U_16_17" : "Y"
}

I want it to be like

{
    "_id":ObjectId("55e86e98f493590878bb45d7"),
    "KIDS": { "KIDS_M_0_2" : "",
    "KIDS_F_0_2" : "",
    "KIDS_U_0_2" : "Y",
    "KIDS_M_3_5" : "",
    "KIDS_F_3_5" : "",
    "KIDS_U_3_5" : "Y",
    "KIDS_M_6_10" : "",
    "KIDS_F_6_10" : "",
    "KIDS_U_6_10" : "",
    "KIDS_M_11_15" : "",
    "KIDS_F_11_15" : "",
    "KIDS_U_11_15" : "",
    "KIDS_M_16_17" : "",
    "KIDS_F_16_17" : "",
    "KIDS_U_16_17" : "Y"
}

Can this be done through the shell, which should be my update team.

+4
source share
1 answer

In fact, you need a simpler command.

.find(), .forEach(), "", . , , $unset $set . , "bulk" 1 .

var bulk = db.collection.initializeUnorderedBulkOp();
var count = 0; 

db.collection.find().forEach(function(doc) {
    var kids = {}; 
    for(key in doc) {
        if(key != '_id' && Object.prototype.hasOwnProperty.call(doc, key)) {
            kids[key] = doc[key];
            var unset = {};         
            unset[key] = '';         
            bulk.find( { '_id': doc._id } ).updateOne( { '$unset': unset } );
            count++;    
        }        
        bulk.find( { '_id': doc._id } ).updateOne({ '$set': { 'KIDS': kids } } ); 
        if (count % 500 === 0) {
            bulk.execute();     
            bulk = db.collection.initializeUnorderedBulkOp(); 
        }
})

if (count > 0)  
    bulk.execute()

:

{
        "_id" : ObjectId("567d99bcbc08a0817a1b06dd"),
        "KIDS" : {
                "KIDS_M_0_2" : "",
                "KIDS_F_0_2" : "",
                "KIDS_U_0_2" : "Y",
                "KIDS_M_3_5" : "",
                "KIDS_F_3_5" : "",
                "KIDS_U_3_5" : "Y",
                "KIDS_M_6_10" : "",
                "KIDS_F_6_10" : "",
                "KIDS_U_6_10" : "",
                "KIDS_M_11_15" : "",
                "KIDS_F_11_15" : "",
                "KIDS_U_11_15" : "",
                "KIDS_M_16_17" : "",
                "KIDS_F_16_17" : "",
                "KIDS_U_16_17" : "Y"
        }
}

MongoDB 3.2 Bulk() .bulkWrite().

var operations = [];

db.collection.find().forEach(function(doc) { 
    var kids = {}; 
    for(var key in doc) { 
        if(!key.startsWith('_id') && Object.prototype.hasOwnProperty.call(doc, key)) { 
            kids[key] = doc[key];
            var unset = {};
            unset[key] = ''; 
            operations.push(
                { 'updateOne': 
                    { 
                        'filter': { '_id': doc._id } ,         
                        'update': { '$unset': unset } 
                    }             
                }         
            );    
        }     
        operations.push(         
            { 'updateOne':              
                { 
                    'filter': { '_id': doc._id } ,              
                    'update': { '$set': { 'KIDS': kids } } 
                }         
            }     
        ); 
    } 
    operations.push( { 'ordered': true } ); 
})

db.collection.bulkWrite(operations)

1. Bulk() API 2.6. ,

+1

Source: https://habr.com/ru/post/1621731/


All Articles