Esempio n. 1
0
<?php

require __DIR__ . '/../vendor/autoload.php';
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// connect
$page = isset($_GET['page']) ? intval($_GET['page']) : 10;
$per = isset($_GET['per']) ? intval($_GET['per']) : 10;
$qOpts = array('limit' => $per, 'skip' => $per * ($page - 1));
$query = new MongoDB\Driver\Query(array(), $qOpts);
// equal find({})
$readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$cursor = $manager->executeQuery("test.interview", $query, $readPreference);
header('Content-Type: application/json');
// query count
$q1 = new MongoDB\Driver\Query(array());
$c1 = $manager->executeQuery("test.interview", $q1, $readPerference);
// return result with count
echo json_encode(array('items' => $cursor->toArray(), 'count' => count($c1->toArray())));
Esempio n. 2
0
<?php

header('Content-Type: text/html; charset=utf-8');
echo '<pre>';
echo "<b>Old way</b>\n\n";
$connection = new Mongo('mongodb://127.0.0.1', array("replicaSet" => false));
$collection = $connection->selectDB("oferty");
$database = $collection->selectCollection("eml");
$cursor = $database->find()->limit(3);
foreach ($cursor as $val) {
    print_r($val);
}
//#################################################
//#################################################
echo "\n\n<b>New way</b>\n\n";
$connection = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$opt = ['limit' => 2, 'sort' => ['eml' => -1]];
$query = new MongoDB\Driver\Query([], $opt);
try {
    $cursor = $connection->executeQuery("oferty.eml", $query);
    $rows = $cursor->toArray();
    foreach ($rows as $data) {
        print_r((array) $data);
    }
} catch (MongoDB\Driver\Exception\Exception $e) {
    echo $e->getMessage(), "\n";
}