マイクロフレームワーク Flight で MongoDB と csv を取り扱う

次のプログラムを改造して、MongoDB を取り扱うようにしました。
>[マイクロフレームワーク Flight で MariaDB と csv を取り扱う](https://qiita.com/ekzemplaro/items/cd5baee4c0bf4b9bf7a9)

次のルートを作成します。
>http://localhost:9000/mongodb
http://localhost:9000/csv
http://localhost:9000/export
http://localhost:9000/import

1) index.php

```php:index.php
<?php
// -----------------------------------------------------------------
require 'flight/Flight.php';

require 'app/views/greeting.php';


Flight::route('/',array('Greeting','hello'));
Flight::route('/mongodb',array('Greeting','mongodb'));
Flight::route('/csv',array('Greeting','csv'));
Flight::route('/export',array('Greeting','export'));
Flight::route('/import',array('Greeting','import'));


Flight::start();
// -----------------------------------------------------------------
```

2) app/views/greeting.php

```php:app/views/greeting.php
<?php
require_once ("csv_manipulate.php");
require_once ("mongo_manipulate.php");

// -----------------------------------------------------------------
class Greeting {
// -----------------------------------------------------------------
    public static function hello() {
        echo '*** Greeting ***<p />';
	echo '<a href="/mongodb">mongodb</a><p />';
	echo '<a href="/csv">csv</a><p />';
	echo '<a href="/export">export</a><p />';
	echo '<a href="/import">import</a><p />';
        echo '*** Jul/31/2018 *** AM 10:37<p />';
    }

// -----------------------------------------------------------------
    public static function export() 
{
	$file_csv = "data/cities_out.csv";
	$user = 'scott';
	$password = 'tiger123';

	$dict_aa = Mongo_manipulate::mongo_to_dict_proc ($user,$password);

	Csv_manipulate::dict_display_proc ($dict_aa);

	Csv_manipulate::csv_write_proc ($file_csv,$dict_aa);

	echo '<p />';
        echo '*** Jul/31/2018 *** AM 10:40<p />';
    }
// -----------------------------------------------------------------
public static function import()
{
	$file_csv = "data/cities.csv";

	$dict_aa = Csv_manipulate::csv_read_proc ($file_csv);

	Csv_manipulate::dict_display_proc ($dict_aa);

	$user = 'scott';
	$password = 'tiger123';

	Mongo_manipulate::dict_to_mongo_proc ($dict_aa,$user,$password);

	$dbcon=null;

	echo '<p />';
        echo '*** Jul/31/2018 *** PM 15:26<p />';
}

// -----------------------------------------------------------------
public static function csv()
{
	$file_csv = "data/cities.csv";

	$dict_aa = Csv_manipulate::csv_read_proc ($file_csv);
	Csv_manipulate::dict_display_proc ($dict_aa);

	echo '<p />';
        echo '*** Jul/31/2018 *** AM 10:55<p />';
}

// -----------------------------------------------------------------
public static function mongodb()
{
	$user = 'scott';
	$password = 'tiger123';

	$dict_aa = Mongo_manipulate::mongo_to_dict_proc ($user,$password);
	Csv_manipulate::dict_display_proc ($dict_aa);

	echo '<p />';
        echo '*** Jul/31/2018 *** AM 10:55<p />';
}

// -----------------------------------------------------------------
}
// -----------------------------------------------------------------
?>
```

3) app/views/mongo_manipulate.php

```php:app/views/mongo_manipulate.php
<?php
// --------------------------------------------------------------------
/*
	mongo_manipulate.php

					Aug/01/2018

*/
// --------------------------------------------------------------------
class Mongo_manipulate {
// --------------------------------------------------------------------
function dict_append_proc ($dict_aa,$id,$name,$population,$date_mod)
{
	$dict_unit = array ();
	$dict_unit['name'] = $name;
	$dict_unit['population'] = $population;
	$dict_unit['date_mod'] = $date_mod;
	$dict_aa[(string)$id]= $dict_unit;

	return	$dict_aa;
}

// --------------------------------------------------------------------
function mongo_to_dict_proc ($user,$password)
{
	$dict_aa = array ();

	$str_connect = "mongodb://" . $user . ":" . $password . "@localhost:27017";
	$manager = new MongoDB\Driver\Manager($str_connect);

	$filter = [];
	$options = [
		'projection' => ['_id' => 0],
		'sort' => ['_id' => -1],
	];

	$query = new MongoDB\Driver\Query($filter, $options);
	$cursor = $manager->executeQuery('city.saitama', $query);

	$dict_aa = array ();

	foreach ($cursor as $document) {
		$dict_aa = Mongo_manipulate::dict_append_proc ($dict_aa,$document->key,$document->name,$document->population,$document->date_mod);
	}

	return	$dict_aa;
}

// --------------------------------------------------------------------
function mongo_update_proc ($id_in,$population_in,$user,$password)
{
	$str_connect = "mongodb://" . $user . ":" . $password . "@localhost:27017";
	$manager = new MongoDB\Driver\Manager($str_connect);

	$bulk = new MongoDB\Driver\BulkWrite;

	date_default_timezone_set('Asia/Tokyo');
	$today = date ("Y-m-d");

	$newdata = array('$set' => array("population" => $population_in,"date_mod" => $today));

	$bulk->update(array("key" => $id_in), $newdata);

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

// --------------------------------------------------------------------
function mongo_delete_proc ($id_in,$user,$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);
}

// --------------------------------------------------------------------
function dict_to_mongo_proc ($dict_aa,$user,$password)
{
	$str_connect = "mongodb://" . $user . ":" . $password . "@localhost:27017";

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

	$manager->executeCommand('city', new \MongoDB\Driver\Command(["drop" => "saitama"]));


	$bulk = new MongoDB\Driver\BulkWrite;
	foreach ($dict_aa as $key => $value)
		{
	$bulk->insert(['key' => $key, 'name' => $value["name"], 'population' => $value['population'], 'date_mod' => $value['date_mod']]);
		}

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

// --------------------------------------------------------------------
}
// --------------------------------------------------------------------
?>
```

4) app/views/csv_manipulate.php
これは、
次のものと同じです。
[マイクロフレームワーク Flight で MariaDB と csv を取り扱う](https://qiita.com/ekzemplaro/items/cd5baee4c0bf4b9bf7a9)