Berikut adalah cara menggunakan CRUD mongoDB pada PHP :
1. Buat file insert-mongo.php
<?php
//$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);
2. Buat 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>';
}
3. Buat 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);
//convert cursor menjadi array
$cursor->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']);
$cursor = $cursor->toArray();
foreach ($cursor as $document) {
//convert id menjadi array
$document['_id'] = (array) $document['_id'];
echo '<pre>';
print_r($document);
echo '</pre>';
}
4. Menambah limit, offset, dan juga sort pada mongo DB:
$limit = 10;
$skip = 5;
$options = array('limit'=>$limit,'skip'=>$skip,'sort'=>array('kolomA'=>1, 'kolomB'=>-1));
//limit adalah jumlah data yang ingin ditampilkan.
//skip adalah offset, mulai dari index keberapa data ditampilkan.
//sort by kolom, nilai 1 = ascending, nilai -1 = descending.
5. Filter query not equal atau != dalam SQL
$query = array('namakolom'=>array('$ne'=>null));//namakolom tidak sama dengan null
6. Menghitung jumlah kolom atau perintah count di SQL
$manager = new MongoDB\Driver\Manager();
//query not equal null
$query = array('namakolom'=>array('$ne'=>null));//array('namakolom'=>'2020')
$command = new MongoDB\Driver\Command(array('count'=>'namatabelorcollection','query'=>$query));
$result = $manager->executeCommand('namadatabase', $command);
$count = $result->toArray()[0]->n;
7. Buat File update-mongo.php
<?php
$manager = new MongoDB\Driver\Manager();
//start update
$bulkWrite = new MongoDB\Driver\BulkWrite;
$bulkWrite->update(
['nama' => 'Deswita'],
['$set' => ['umur' => 30]],
['multi' => true, 'upsert' => false]
);
$manager->executeBulkWrite("databaseAnda.collectionUser", $bulkWrite);
//update selesai
//query utk menampilkan hasilnya
$query = new MongoDB\Driver\Query([]);
$cursor = $manager->executeQuery("databaseAnda.collectionUser", $query);
foreach ($cursor as $document) {
echo '<pre>';
print_r($document);
echo '</pre>';
}
Keterangan :
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.
8. Buat File delete-mongo.php
<?php
$manager = new MongoDB\Driver\Manager();
$bulkWrite = new MongoDB\Driver\BulkWrite;
$bulkWrite->delete(['nama' => 'Deswita'], ['limit' => true]);
$manager->executeBulkWrite("databaseAnda.collectionUser", $bulkWrite);
$query = new MongoDB\Driver\Query([]);
$cursor = $manager->executeQuery("databaseAnda.collectionUser", $query);
foreach ($cursor as $document) {
echo '<pre>';
print_r($document);
echo '</pre>';
}
Keterangan :
limit = true, artinya hanya menghapus 1 data. jika limit = false atau 0, maka semua nama “deswita” akan dihapus di collectionUser.