Operasi CRUD MongoDB dengan PHP 7.2

Bismillaahirrohmaanirrohiim…

Sebelum melanjutkan artikel ini, sebaiknya server sudah terinstall mongoDB dan PHP Driver untuk mongoDB juga sudah terinstall dengan benar.

Cara mengetahuinya adalah jika di phpinfo() sudah muncul berarti sudah oke, kalau belum anda harus menginstallnya di sini dan di sini.

Pertama buat file insert-mongo.php
<?php
//bisa seperti ini
//$manager = new MongoDB\Driver\Manager("mongodb://user:password@localhost:27017");
//$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
//$manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");

$manager = new MongoDB\Driver\Manager();
$bulkWrite = new MongoDB\Driver\BulkWrite;
$bulkWrite->insert(['nama' => 'Erlangga', 'umur' => 26, 'status' => 'pria']);
$bulkWrite->insert(['nama' => 'Arbi', 'umur' => 31, 'status' => 'pria', 'lahir' => new MongoDB\BSON\UTCDatetime(strtotime('1987-04-02') * 1000)]);
$bulkWrite->insert(['nama' => 'Deswita', 'umur' => 25, 'status' => 'wanita']);
$manager->executeBulkWrite("databaseAnda.collectionUser", $bulkWrite);
File select-mongo.php
<?php
$manager = new MongoDB\Driver\Manager();

$query = new MongoDB\Driver\Query( [] );
$cursor = $manager->executeQuery("databaseAnda.collectionUser", $query);

foreach ($cursor as $document) {
    echo '<pre>';
    print_r($document);
    echo '</pre>';
}
File select-filter-mongo.php
<?php
$manager = new MongoDB\Driver\Manager();

$filter = ['umur' => ['$lt' => 26]];
$options = [];

$query = new MongoDB\Driver\Query($filter, $options);

$cursor = $manager->executeQuery("databaseAnda.collectionUser", $query);

foreach ($cursor as $document) {
    echo '<pre>';
    print_r($document);
    echo '</pre>';
}
File update-mongo.php
<?php
$manager = new MongoDB\Driver\Manager();

$bulkWrite = new MongoDB\Driver\BulkWrite;
$bulkWrite->update(
    ['nama' => 'Deswita'],
    ['$set' => ['umur' => 30]],
    ['multi' => true, 'upsert' => false]
);


$query = new MongoDB\Driver\Query([]);

$cursor = $manager->executeQuery("databaseAnda.collectionUser", $query);

foreach ($cursor as $document) {
    echo '<pre>';
    print_r($document);
    echo '</pre>';
}

multi = true, artinya semua nama “deswita” akan di update. Kalau muti = false, hanya 1 baris dokumen pertama yang diupdate.
upsert = false, artinya jika tidak ada data yang diupdate maka tidak melakukan update apa-apa. Jika upsert = true, maka akan menginsert data baru sesuai filter ada.

File delete-mongo.php
<?php
$manager = new MongoDB\Driver\Manager();

$bulkWrite = new MongoDB\Driver\BulkWrite;
$bulkWrite->delete(['nama' => 'Deswita'], ['limit' => true]);

$query = new MongoDB\Driver\Query([]);

$cursor = $manager->executeQuery("databaseAnda.collectionUser", $query);

foreach ($cursor as $document) {
    echo '<pre>';
    print_r($document);
    echo '</pre>';
}

limit = true, artinya hanya menghapus 1 data. jika limit = false atau 0, maka semua nama “deswita” akan dihapus di collectionUser.

Nah, begitulah operasi CRUD (create, read, update ,delete) mongoDB di PHP 7.2

Semoga bermanfaat.

You May Also Like

About the Author: rasupe

Leave a Reply

Your email address will not be published. Required fields are marked *