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

100
Vistas
Mongoose update subdocument by key

for the following collection:

id: Number
name:[ 
       new Schema({
           language: { type: String, enum: ['en-US', 'fr-CA'] },
           text: String,
       },{ _id: false }
       );
     ],
isActive: Boolean

and sample data like the following:

{
  id:1,
  "name": [
            {"language": "en-US", "text": "Book"},
            {"language": "fr-CA", "text": "livre"}
          ],
   isActive:true
   // and so many other fields
},
{
  id:2,
  "name": [
            {"language": "en-US", "text": "Pen"}
          ],
   isActive:true
   // and so many other fields
}

I would like to update the document by Id:1 and change the text only for french, I tried by:

const input={ "id":"1", "isActive": false}
const name= { "name": [{"language": "fr-CA", "text": "Nouvel article"}]}
await langs.findByIdAndUpdate(
    { _id: input.id },
    { ...input, $addToSet: { name } },
    { new: true, upsert: true }
  );

but the result is: (added another french item)

{
  id:1,
  "name": [
            {"language": "en-US", "text": "Book"},
            {"language": "fr-CA", "text": "livre"},
            {"language": "fr-CA", "text": "Nouvel article"}
          ],
   isActive:false
},

This is based on Brit comment:

https://mongoplayground.net/p/atlw5ZKoYiI

Please advise.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As long as that attribute already exists on the document, you can do something like:

   Collection.findOneAndUpdate({id: 1}, {
        $set: {
          "name.$[elem]": name
        }
      },
      {
        arrayFilters: [  { "elem.language":  name.language  } ],
        upsert: true,
        new: true
      })
about 4 years ago · Juan Pablo Isaza 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