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

115
Vistas
How would I increment this number in mongoose?

I want to increment or decrement the likes value in the schema. My fear is that if two people were to grab the value of likes close to the same time, and they both incremented it, the value would only increment once.

For instance if likes = 100 and person A incremented it at the same time person B incremented it, likes would be 101 instead of 102.

Should I make a separate schema for likes and for each like I give it a postId?

var postSchema = mongoose.Schema({
    name: {
        type: String,
        required: true,
        unique: false
    },
    likes: {
         type: Number, 
         default: 0 
    }
});
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use this in mongoose to increment(change likes to -1 for decrement)

var query = {};
  var update = {$inc: {likes: 1}};
  var options = {'new': true};
  PostSchema.findOneAndUpdate(query, update, options, function (err, doc) {...
  })
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