3

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.

  • You can find info in this response Update field in exact element array in MongoDB https://stackoverflow.com/a/10433042/4871259 – MoiioM Oct 26 '21 at 08:11
  • Take a look at MongoDB [array operators](https://docs.mongodb.com/manual/reference/operator/query-array/) **OR** you can search through its doc to find any proper operator such as [$in](https://docs.mongodb.com/manual/reference/operator/query/in/) and MongoDB [aggregation](https://docs.mongodb.com/upcoming/aggregation/) – Ali Tavafi Oct 26 '21 at 08:11
  • @AliTavafi Thanks for your help, however I'm struggling to work out how to implement this? C currently I've got the following: ` guildModel.update( { "guildData.members._id": guildId }, { $set: { "guildData.members.$.economy.bank": "new_value" } }, { multi: true } ); ` However, it's not working - I'm likely going about it in completely the wrong way. Any chance you could give me some advice? – Toby Chambers Oct 26 '21 at 08:30
  • If you are trying to update an array element, based upon a condition, you can do that using the [Array Update Operators](https://docs.mongodb.com/manual/reference/operator/update-array/). There are various array update operators and each have a specific purpose. – prasad_ Oct 26 '21 at 10:46

1 Answers1

1

For example if you have data like that

enter image description here

if you know the position of element in the array

db.roundTest.findAndModify({
       query: { _id : ObjectId('60c916684bd16901f36efb3a') },
       update: { $set: { "holes.$[elem].holeGross" : 8 } },
       arrayFilters: [ { "elem.no": 1 } ]
    })

other method to update a single object in the array

db.document.update({
       "_id":"idofobject"
    },
   {
     "$set":{
      "holes.$.name" : "Augmon" //you should use name of yourfiled 
   }})

for more detail read that article Update single (same) field in every object of an array

watch this video to see how to use these queries update monogoDB Array Elements

Syed Ali Shahzil
  • 1,079
  • 1
  • 11
  • 17
  • Thanks for your help, however I don't know the position of the object in the array? That's what I need to search through the array by? Any chance you could give me some advice as to how I would achieve this? @SyedAliShahzil – Toby Chambers Oct 26 '21 at 16:26
  • I hope this would solve your problem. see this video to get experts in updating arrays in MongoDB – Syed Ali Shahzil Oct 26 '21 at 17:57