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

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

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 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