如何在PHP上使用updateOne过滤MongoDB条目进行更新



我正在尝试使用下面的代码更新MongoDB上的一个条目。我希望它能更新从html表单中发布的与studentid相关的"name"字段。我一直收到一个语法错误(意外(,尽管我已经更改了很多,但都没有用。我也直接从MongoDB文档中得到了这些代码?

<?php
require 'vendor/autoload.php' ;
$client = new MongoDBClient('mongodb://127.0.0.1:27017');
$db_name = 'studentsinfo';
$db = $client->$db_name;
$collection = $db->students;
if($_POST)
{
$update = $collection->updateOne([
'studentid'=> $_POST['studentid'],
[ '$set' => [ 'name' => 'Brunos on Astoria' ]]

);
}

updateOne方法中缺少方括号]

if($_POST){
$update = $collection->updateOne(
['studentid'=> $_POST['studentid']],
[ '$set' => [ 'name' => 'Brunos on Astoria' ]]
);
}

最新更新