Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda