//Connection
$manager=new MongoDB\Driver\Manager("mongodb://root:testtest@localhost:27017");
db='myDB';
$collection='myCollection';
//All Data List
$query=new MongoDB\Driver\Query([]);
$rows=$manager->executeQuery($db.'.'.$collection, $query);
foreach($rows as $row) {
echo $row->_id.' ';
echo $row->fname.' ';
echo $row->lname.' ';
echo "<br>";
}
//DB List
$mng = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$listdatabases = new MongoDB\Driver\Command(["listDatabases" => 1]);
$res = $mng->executeCommand("admin", $listdatabases);
$databases = current($res->toArray());
foreach ($databases->databases as $el) {
echo $el->name . "<br>";
}
//Insert
$fname = "ดาดา";
$lname = "ยะโกะ";
$bulk = new MongoDB\Driver\BulkWrite();
$doc = ['fname' => $fname, 'lname' => $lname];
$bulk->insert($doc);
$result = $manager->executeBulkWrite($db.'.'.$collection, $bulk);
//Filter with multiple Condition
$filter = [ 'office_id' => array('$regex' => '12901001'),
'occu' => array('$regex' => 'LorryDriver'),
'dec_date' => ['$gte' => '2021-01-01']
];
// 'dec_date' => array('$regex' => '2021-01') //เดือนเดียว
$options = [
'projection' => ['_id' => 0],
//'sort' => ['office_id' => -1]
];
$query = new MongoDB\Driver\Query($filter, $options);
$res = $mng->executeQuery($db.'.'.$collection, $query);
$c=0;
foreach ($res as $doc) {
echo $doc->office_id,' ' ,$doc->dec_date,' ',$doc->fullname,'<br>';
$c++;
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น