2024 年 PHP 日本研討會

Collection::removeOne

(沒有版本資訊,可能只在 Git 中)

Collection::removeOne移除一個集合文件

說明

public mysql_xdevapi\Collection::removeOne(字串 $id): mysql_xdevapi\Result

從集合中移除具有對應 ID 的單一文件。這是 Collection.remove("_id = :id").bind("id", id).execute() 的簡寫。

參數

id

要移除的集合文件的 ID。通常這是記錄新增時 MySQL 伺服器產生的 _id。

回傳值

一個 Result 物件,可用於查詢受影響項目的數量或操作產生的警告數量。

範例

範例 #1 mysql_xdevapi\Collection::removeOne() 範例

<?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();

// 通常 _id 是透過其他方式得知,
// 但在此範例中,我們擷取產生的 id 並使用它
$ids = $result->getGeneratedIds();
$alfred_id = $ids[0];

$result = $collection->removeOne($alfred_id);

if(!
$result->getAffectedItemsCount()) {
echo
"id 為 $alfred_id 的 Alfred 並未被移除。";
} else {
echo
"再見,Alfred,你可以帶著 _id $alfred_id 離開。";
}
?>

上述範例將輸出類似以下的內容

Goodbye, Alfred, you can take _id 00005b6b536100000000000000cb with you.
新增註記

使用者貢獻的註記

此頁面沒有使用者貢獻的註記。
To Top