Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
Upsert issue when updating multiple documents using an array of IDs with $in

This query is doing the job fine :

db.collection.update(
    { "_id": oneIdProvided }, 
    { $inc: { "field": 5 } },{ upsert: true }
)

Now I would like to do the same operation multiple time with different IDs, I thought the good way was to use $in and therefore I tried :

db.collection.update(
    { "_id": { $in: oneArrayOfIds} }, 
    { $inc: { "field": 5 } },{ upsert: true }
)

Problem is : if one of the provided ID in the array is not existing in the collection, a new document is created (which is what I want) but will be attributed an automatic ID, not using the ID I provided and was looking for.

One solution I see could be to do first an insert query with my array of ID (those already existing would not be modified) and then doing my update query with upsert: false

Do you see a way of doing that in only one query ?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

We can do this by performing multiple write operations using the bulkWrite() method.

function* range(start, end, step) { 
    for (let val=start; val<end; val+=step)
        yield val
}

let oneArrayOfIds; // For example [1, 2, 3, 4]

let bulkOp = oneArrayOfIds.map( id => { 
    return { 
        "updateOne": { 
            "filter": { "_id": id }, 
            "update": { "$set": { "field": 5 } }, 
            "upsert": true 
        }
    };
});

const limit = 1000;
const len = bulkOp.length;

let chunks = [];
if (len > 1000) {
    for (let index of range(0, len, limit)) {
        db.collection.bulkWrite(bulkOp.slice(index, index+limit));
    }
} else {
    db.collection.bulkWrite(bulkOp);
}   
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda