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

152
Vistas
MongoDB (mongoose) pushing into arrays is ignoring properties

I have a User Schema that looks like this:

reputation: [
{
  voter_id: {
    type: String,
    required: true,
  },
},
{
  score: {
    type: Number,
    required: true,
  },
},
{
  comment: {
    type: String,
    maxlength: 100,
  },
},
{
  voted_at: {
    type: Date,
    default: Date.now,
  },
},
],

Whenever I try to push an object into this array like this

User.updateOne(
  { id: user.id },
  {
    $push: {
      reputation: {
        voter_id: interaction.user.id,
        score: rating,
        comment,
      },
    },
  },
);

Only the first property of the object to push, here voter_id is set in the database, even though the other values are not undefined.

The object stored in the database looks like this:

{ "_id" : ObjectId("61478043630ef626f50c9c2b"), "reputation" : [ { "voter_id" : "214772155114717184", "_id" : ObjectId("61478060630ef626f50c9c7c") } ], "__v" : 0 }

The properties are missing. For this example score was 1 and comment "test". Whats the problem here?

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

0

The array notation is what tricks into making the items within separate, should be a single object:

reputation: [
{
  voter_id: {
    type: String,
    required: true,
  },
  score: {
    type: Number,
    required: true,
  },
  comment: {
    type: String,
    maxlength: 100,
  },
  voted_at: {
    type: Date,
    default: Date.now,
  },
}
]
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