2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHP で MongoDB のデータを削除 (Delete)

Last updated at Posted at 2018-06-02
mongo_delete.php
# ! /usr/bin/php
<?php
// --------------------------------------------------------------------
/*
	mongo_delete.php

					Jun/08/2019

*/
// --------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");

$id_in = $argv[1];
print	$id_in . "\n";

require_once './vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

//
$user = $_ENV['user'];
$password = $_ENV['password'];

$str_connect = "mongodb://" . $user . ":" . $password . "@localhost:27017";

$manager = new MongoDB\Driver\Manager($str_connect);

$bulk = new MongoDB\Driver\BulkWrite;

$bulk->delete(['key' => $id_in]);

$result = $manager->executeBulkWrite('city.saitama', $bulk);

fputs (STDERR,"*** 終了 ***\n");

// --------------------------------------------------------------------
?>
.env
user = '******'
password = '******'

実行コマンドの例

./mongo_delete.php t1164

次のバージョンで確認しました。

$ php --version
PHP 8.0.8 (cli) (built: Oct 26 2021 11:42:42) ( NTS )
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?