Reputation: 2337
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.
Upvotes: 0
Views: 99
Reputation: 15215
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
Upvotes: 1