You are on page 1of 1

//Controlador

<?php
class MongoConnection {
private $host;
private $port;
private $dbname;

public function __construct($host, $port, $dbname) {


$this->host = $host;
$this->port = $port;
$this->dbname = $dbname;
}

public function connect() {


try {
// Establishing a connection to the MongoDB server
return new MongoClient("mongodb://" .$this->host . ":" .$this-
>port);

} catch (MongoConnectionException $e) {

die('Error connecting to MongoDB server');

} catch (MongoException $e) {

die('Error: ' .$e->getMessage());

}
?>

//Modelo
<?php class Model { protected $_connection; protected $_collection;
protected $_documentArray; public function __construct(MongoConnection
$mongoConnection) { // Create a connection to the db $_connection = new
MongoClient($mongoConnection); // Select the collection $_collection =
$_connection->selectCollection($mongoConnection->getDbName(), 'mycollection');
// Get the document array from the collection $_documentArray = $_collection-
>find(); } // Other model methods go here...} ?>
//Vista <?php echo '<table border="1">'; foreach ($_documentArray as $doc)
{ echo '<tr>'; foreach ($doc as $key =>$value){ echo '<td>'.$key.'</td><td>'.
$value.'</td>'; } echo '</tr>';}echo '</table>'; ?>

You might also like