略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Collection::replaceOne

2024-11-13

Collection::replaceOne

(No version information available, might only be in Git)

Collection::replaceOneReplace one collection document

说明

public mysql_xdevapi\Collection::replaceOne(string $id, string $doc): mysql_xdevapi\Result

Updates (or replaces) the document identified by ID, if it exists.

参数

id

ID of the document to replace or update. Typically this is the _id that was generated by MySQL Server when the record was added.

doc

Collection document to update or replace the document matching the id parameter.

This document can be either a document object or a valid JSON string describing the new document.

返回值

A Result object that can be used to query the number of affected items and the number warnings generated by the operation.

范例

示例 #1 mysql_xdevapi\Collection::replaceOne() example

<?php
$session 
mysql_xdevapi\getSession("mysqlx://user:password@localhost");

$session->sql("DROP DATABASE IF EXISTS addressbook")->execute();
$session->sql("CREATE DATABASE addressbook")->execute();

$schema     $session->getSchema("addressbook");
$collection $schema->createCollection("people");

$result $collection->add('{"name": "Alfred", "age": 18, "job": "Butler"}')->execute();

// Normally the _id is known by other means, 
// but for this example let's fetch the generated id and use it
$ids       $result->getGeneratedIds();
$alfred_id $ids[0];

// ...

$alfred $collection->getOne($alfred_id);
$alfred['age'] = 81;
$alfred['job'] = 'Guru';

$collection->replaceOne($alfred_id$alfred);

?>
add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/mysql-xdevapi-collection.replaceone.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3