1

I want to update many documents on my db but are nested arrays so I need to use arrayFilters. In the shell works perfect but in my php code never works. I have tried this way:

$command = new \MongoDB\Driver\Command(
  [
    'update'  => 'students2',
    'updates' => [["grades.grade" => ['$gte' => 85]], ['$set' => ["grades.$[elem].mean" => 98 ]], 'arrayFilters'   => $filters, 'multi' => true]
  ]
);

with error: BSON field 'update.updates.grades.grade' is an unknown field

this way:

$db->students2->updateMany([],['$set'=>["grades.$[elem].mean" => 100 ]],['arrayFilters'=>  ["elem.grade"=> ['$gte'=>85 ]]]);

with error: "arrayFilters" option has invalid keys for a BSON array

If I execute this command works right but only in one document:

$command = new \MongoDB\Driver\Command(
  [
    'findAndModify'  => 'students2',
    'query'          => ["grades.grade" => ['$gte' => 85]],
    'update'         => ['$set' => ["grades.$[elem].mean" => 98 ]],
    'upsert'         => true,
    'returnDocument' => true,
    'new'            => true,
    'arrayFilters'   => [['elem.grade'] => ['$gte' => 85]]
  ]
);

this is my data in mongodb table:

{
   "_id" : 1,
   "grades" : [
      { "grade" : 80, "mean" : 75, "std" : 6 },
      { "grade" : 85, "mean" : 100, "std" : 4 },
      { "grade" : 85, "mean" : 100, "std" : 6 }
   ]
}
{
   "_id" : 2,
   "grades" : [
      { "grade" : 90, "mean" : 100, "std" : 6 },
      { "grade" : 87, "mean" : 100, "std" : 3 },
      { "grade" : 85, "mean" : 100, "std" : 4 }
   ]
}

What am I doing wrong? Thanks so much

  • You're using the wrong driver. The `MongoDB\Driver` namespace is from the **core** driver installed from PECL. You actually use the [PHPLIB](https://docs.mongodb.com/php-library/current/) driver which is consistent with other language API's. It has [MongoDB\Collection::updateMany()](https://docs.mongodb.com/php-library/current/reference/method/MongoDBCollection-updateMany/). Your code should basically never touch the `MongoDB\Driver` namespace. You might have installed it, but you really never should be calling `Command`. – Neil Lunn Apr 02 '19 at 05:38
  • And it's `['arrayFilters'=> [ ["elem.grade"=> ['$gte'=>85 ] ] ] ]` just like the error is actually saying because you are missing the wrapping `[ ]` in order to define a BSON array. I also recommend you use `json_encode()` when prototyping in order to check that the serialized JSON of your PHP data structures actually matches the examples in JavaScript notation you are likely to more commonly find. – Neil Lunn Apr 02 '19 at 05:39
  • Thanks so much, I was missing the [] in the arrayfilters array, now is solved. – Charles Pasc Apr 18 '19 at 11:15

1 Answers1

-1

Try This way

$updateResult = $collection->updateMany(
    ["inspector" => "J. Clouseau", "Sector" => 4 ],
    ['$set' => ['Patrolling' => false]]
);
Basant Rules
  • 785
  • 11
  • 8
  • But what I try to do is update only de nested arrays, this way only updates the mail fields. I want to update only the grades.mean field – Charles Pasc Apr 01 '19 at 15:21