sql >> Base de Datos >  >> NoSQL >> MongoDB

¿Cómo actualizar una gran cantidad de documentos en MongoDB de manera más eficiente?

Si su servidor MongoDB es 2.6 o más nuevo, sería mejor aprovechar el uso de comandos de escritura API masiva que permiten la ejecución masiva de update operaciones que son simplemente abstracciones en la parte superior del servidor para facilitar la creación de operaciones masivas. Estas operaciones masivas vienen principalmente en dos sabores:

  • Operaciones masivas ordenadas . Estas operaciones ejecutan todas las operaciones en orden y fallan en el primer error de escritura.
  • Operaciones masivas desordenadas . Estas operaciones ejecutan todas las operaciones en paralelo y agregan todos los errores. Las operaciones masivas no ordenadas no garantizan el orden de ejecución.

Tenga en cuenta que para servidores anteriores a 2.6, la API convertirá las operaciones a una escala inferior. Sin embargo, no es posible realizar una conversión descendente del 100 %, por lo que puede haber algunos casos extremos en los que no pueda informar correctamente los números correctos.

Para sus tres casos de uso comunes, podría implementar la API masiva de esta manera:

Caso 1. Cambiar el tipo de valor de la propiedad, sin cambiar el valor:

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
    // Handle error
    if(err) throw err;

    // Get the collection and bulk api artefacts
    var col = db.collection('users'),           
        bulk = col.initializeOrderedBulkOp(), // Initialize the Ordered Batch
        counter = 0;        

    // Case 1. Change type of value of property, without changing the value.        
    col.find({"timestamp": {"$exists": true, "$type": 2} }).each(function (err, doc) {

        var newTimestamp = parseInt(doc.timestamp);
        bulk.find({ "_id": doc._id }).updateOne({
            "$set": { "timestamp": newTimestamp }
        });

        counter++;

        if (counter % 1000 == 0 ) {
            bulk.execute(function(err, result) {  
                // re-initialise batch operation           
                bulk = col.initializeOrderedBulkOp();
            });
        }
    });

    if (counter % 1000 != 0 ){
        bulk.execute(function(err, result) {
            // do something with result
            db.close();
        }); 
    } 
});

Caso 2. Agregue una nueva propiedad según el valor de la propiedad existente:

MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
    // Handle error
    if(err) throw err;

    // Get the collection and bulk api artefacts
    var col = db.collection('users'),           
        bulk = col.initializeOrderedBulkOp(), // Initialize the Ordered Batch
        counter = 0;        

    // Case 2. Add new property based on value of existing property.        
    col.find({"name": {"$exists": false } }).each(function (err, doc) {

        var fullName = doc.firstname + " " doc.lastname;
        bulk.find({ "_id": doc._id }).updateOne({
            "$set": { "name": fullName }
        });

        counter++;

        if (counter % 1000 == 0 ) {
            bulk.execute(function(err, result) {  
                // re-initialise batch operation           
                bulk = col.initializeOrderedBulkOp();
            });
        }
    });

    if (counter % 1000 != 0 ){
        bulk.execute(function(err, result) {
            // do something with result
            db.close();
        }); 
    } 
});

Caso 3. Simplemente agregando propiedades de eliminación de documentos.

MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
    // Handle error
    if(err) throw err;

    // Get the collection and bulk api artefacts
    var col = db.collection('users'),           
        bulk = col.initializeOrderedBulkOp(), // Initialize the Ordered Batch
        counter = 0;        

    // Case 3. Simply adding removing properties from documents.    
    col.find({"street_no": {"$exists": true } }).each(function (err, doc) {

        bulk.find({ "_id": doc._id }).updateOne({
            "$set": { "no": doc.street_no },
            "$unset": { "street_no": "" }
        });

        counter++;

        if (counter % 1000 == 0 ) {
            bulk.execute(function(err, result) {  
                // re-initialise batch operation           
                bulk = col.initializeOrderedBulkOp();
            });
        }
    });

    if (counter % 1000 != 0 ){
        bulk.execute(function(err, result) {
            // do something with result
            db.close();
        }); 
    } 
});