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

252
Vistas
How do I set values in a nested map in mongoose directly

I have a mongoose schema which has a map(holding) nested in other map(users).

const UserSchema = new mongoose.Schema({
  users: {
    type: Map,
    of: new mongoose.Schema({
      holding: {
        type: Map,
        of: Number,
      },
    }),
  },
});
const UserModel = mongoose.model('black', UserSchema);

I create a new user with the below info.

  var user = new userModel({
    users: {
      jose: {
        holding: { itc: 9, hcl: 300 },
      },
    },
  });

Now I am trying to edit a value in the map holdings for my user with the set method. Which I am able to do as below.

  await user.set(`users.jose`, {
    holding: { itc: 90, hcl: 300 },
  });
  user.get(`users.jose.holding.itc`) //displays new value(90)

However if I try to set the values in nested map "holding" directly this does not work.

ie the below code fails(value is not set)

await user.set(`users.jose.holding.itc`, 1000);
user.get(`users.jose.holding.itc`) //gives old value or does not display 1000

Why does this fail? .How do I set values in a nested map in mongoose directly.

about 4 years ago · Juan Pablo Isaza
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