A
A
Anime Flame2022-01-18 07:17:33
Mongoose
Anime Flame, 2022-01-18 07:17:33

How to edit an object property in an array?

I need to edit a string that is in an object that is in an array.
I am using mongoose and javascript.

I have an array of Users, which contains 2 objects:

You need to look up the Users.Depost value based on the user ID and change it

"Users": [{
        "Id": "808656561244995655",
        "Rank": "owner",
        "Deposit": 0,
        "Joined": 1642435574167
    }, {
        "Id": "905432168950538282",
        "Rank": "mod",
        "Deposit": 0,
        "Joined": 1642435574167
    }],

I use similar work with mongodb, when I try to filter the value of docs.Users and change it while saving it in the database, it does not change anything and does not give errors.

guildDb.findOne({ GuildId: guild.id, GuildOwner: status.ownerId }, async (err, docs) => {
   if (err) throw err
   if (!docs) return

   docs.Users.find(x => x.id === user.id).Deposit += +500
   await docs.save().catch(error => console.log(error)
})

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WapSter, 2022-01-18
@AnimeFlame

https://qna.habr.com/q/851343

R
Rag'n' Code Man, 2022-01-18
@iDmitriyWinX

It seems to me that you should look aside populateand lookupnot store objects in an array, but put them in a separate collection. So it will be much more convenient to manage them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question