The problem with the request in MongoDB. It is necessary to update several data with one request. Here is an example request:

$coll -> update( array( array( 'article' => 100500 ), array( '$set' => array( 'a' => 555 ), '$pull' => array( 'arr' => array( 't' => array( '$lt': 20 ) ) ) ) ) ); 

Here is an example of the document itself:

 { article: 100500, a: 400, arr: [ { t: 30, b: 12, n: 90 }, { t: 10, b: 16, n: 60 } ] } 

My request:

  1. Updates the value of a to 555 .
  2. Removes all elements of the arr array, where t < 20 .

The essence of the question : It is necessary to update the values ​​of b in the mass arr . That is, wherever n == 90 , the value of b must be changed to 777 . How can this request be supplemented to “kill 3 birds with one stone”?

The result should be such a document:

 { article: 100500, a: 555, /* Тут было: 400 */ arr: [ { t: 30, b: 777, n: 90 }, /* Тут был элемент массива */ ] } 
  • one
    Refresh whole document is not an option? - Yura Ivanov
  • 3
    MongoDB is not SQL. A completely different approach to working with data. It’s one thing to do a complex selection with a complex query, and another to do an update of the data in the document. You just need to select the desired document, update all the necessary data in it and save it - zhenyab

1 answer 1

MongoDB does not allow sampling within samples. You want to find inside the existing records (which you chose as your condition) some other data and update only them. You can not do it this way. You can familiarize yourself with all the update operators in the documentation .

It is easier to follow another request, which will update the data you need.