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

313
Vistas
(closed) How to increment a value which is in an object which is in an array which is in another object

So I'm trying to code a bot in discord.js where you basically have items in an inventory and when I use an item, its durability goes down. I am using MongoDB Atlas to store all the data. The structure of the inventory is:

user: {
  id: 'user-id',
  name: 'user-name',
  inventory: [
    {
      name: 'item',
      durability: 5
    ,
    {
      name: 'another-item',
      durability: 20
    }
  ]
}

So I just wanted to find a way to basically just decrement the durability in the item without updating any other item in the inventory array but I wasn't able to get a way to do it.

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

0

You can use arrayFilters to update only the value you want.

Also you can use $inc to increment the value (in this case increment by -1, i.e, decrement)

db.collection.update({
  "id": "user-id"
},
{
  "$inc": {
    "inventory.$[element].durability": -1
  }
},
{
  "arrayFilters": [
    {
      "element.name": "item" //here use the name of the object you want to update
    }
  ]
})

Example here

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