M
M
Modin2016-11-10 23:32:59
PHP
Modin, 2016-11-10 23:32:59

How to update an object in an array in a mongodb collection (php)?

I have an object in the collection:

{
  _id:{},
  name: 'name',
  someArray: [{
    id: 234,
    info: 'some info'
  }, {
    id: 567,
    info: 'other info'
  }]
}

For example, I need to change info in an object with id 567, what is the best way to do this? It is possible to find an object in an array, but not to update it. I use puff as a language. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Karavaev, 2016-12-26
@Quieteroks

Lots of options. For example, updating with a search query:

$retval = $col->findAndModify(
     ["name" => "name"],
     ['$set' => ['someArray.0.info' => 'update info']],
);

Or if the processing is significant:
$doc = $col->find(["name" => "name"]);
$doc['someArray'][0]['info'] = 'update info';
$col->save($doc);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question