I'm working with Mongoose and MongoDB - and am searching through a nested array in my document for an object with a specified ID.
Currently, I am doing the following:
const dbGuild = guildModel.findById(guildId);
for (member of dbGuild.guildData.members) {
if (member._id === mentionedUserId) {
console.log(member.economy.hand);
console.log(member.economy.bank);
member.economy.bank = 5000;
const updated = await dbGuild.save();
}
}
Where I am looping through every single value in that array - however this seems very inefficient as it would mean that if there was a large amount of data in the array I would be querying a huge amount of data.
Is it possible to do this on the database? And only return the single object with that id.