0

So I tried several ways, but I can't, I can modify several objects with the same key but I can't modify any with different keys, if anyone can help me is quite a complex problem

{ 
  id: 123, 
  "infos": [ 
    { name: 'Joe', value: 'Disabled', id: 0 }, 
    { name: 'Adam', value: 'Enabled', id: 0 }
  ]
};

In my database I have a collection with an array and several objects inside which gives this.

I want to modify these objects, filter by their name and modify the value.

To give you a better example, my site returns me an object with the new data, and I want to modify the database object with the new object, without clearing the array, the name key never changes.

const object = [
  { name: 'Joe', value: 'Hey', id: 1 }, 
  { name: 'Adam', value: 'None', id: 1 }
];

for(const obj in object) {
  Schema.findOneAndUpdate({ id: 123 }, {
    $set: {
      [`infos.${obj}.value`]: "Test"
    }
  })
}

This code works but it is not optimized, it makes several requests, I would like to do everything in one request, and also it doesn't update the id, only the value.

If anyone can help me that would be great, I've looked everywhere and can't find anything

My schema structure

new Schema({
  id: { "type": String, "required": true, "unique": true }, 
  infos: []
})

I use the $addToSet method to insert objects into the infos array

2
  • Please add your schema structure. Commented Mar 14, 2022 at 13:22
  • I edited with the schema Commented Mar 14, 2022 at 14:18

2 Answers 2

3

Try This :

db.collection.update({
  id: 123,
  
},
{
  $set: {
    "infos.$[x].value": "Value",
    "infos.$[x].name": "User"
  }
},
{
  arrayFilters: [
    {
      "x.id": {
        $in: [ 
          1 
        ]
      }
    },
    
  ],
  multi: true
})
  1. The all positional $[] operator acts as a placeholder for all elements in the array field.

  2. In $in you can use dynamic array of id. Ex :

    const ids = [1,2,..n]

    db.collection.update( //Same code as it is... { arrayFilters: [ { "x.id": { $in: ids } }, ], multi: true })

MongoPlayGround Link : https://mongoplayground.net/p/Tuz831lkPqk

Sign up to request clarification or add additional context in comments.

2 Comments

But i have an array with 30k users and i only want to edit 10-15 of them not all, i also want to edit multiples values in the object
In this code I have seen two object users with the same id 1. In that case I would assume that there are multiple user objects with the same id. and in my code, I have talked about how to change the field of multiple by using multiple id.
1

Maybe you look for something like this:

db.collection.update({},
{
  $set: {
   "infos.$[x].value": "test1",
   "infos.$[x].id": 10,
   "infos.$[y].value": "test2",
   "infos.$[y].id": 20
   }
 },
{
 arrayFilters: [
{
  "x.name": "Adam"
},
{
  "y.name": "Joe"
}
],
  multi: true
})

Explained:

You define arrayFilters for all names in objects you have and update the values & id in all documents ...

playground

3 Comments

Yes it's that, but if i got 10 users, how can i automate this, i don't want to add 10 filters
How can i add an array to the $set method ? I dont want to add each line manually
Btw , you can prepare array of many update operations in single bulkWrite call , check here if you want to reduce the many single calls to the database: docs.mongodb.com/manual/reference/method/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.