本文介绍了mongodb php更新上的arrayFilters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新数据库上的许多文档,但是它们是嵌套数组,因此我需要使用arrayFilters.在外壳中可以完美运行,但是在我的php代码中却永远无法运行.我已经尝试过这种方式:

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]
  ]
);

有错误:BSON字段"update.updates.grades.grade"是未知字段

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

这种方式:

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

有错误:"arrayFilters"选项的BSON数组键无效

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]]
  ]
);

这是我在mongodb表中的数据:

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

推荐答案

尝试这种方式

$updateResult = $collection->updateMany(
    ["inspector" => "J. Clouseau", "Sector" => 4 ],
    ['$set' => ['Patrolling' => false]]
);

这篇关于mongodb php更新上的arrayFilters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 08:51