Bagian I
sebelumnya install terlebih dahulu php7.0 dan juga bisa mengincludekan php-mysql di ubuntu ;
setelah itu, install MongoDB
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
sudo apt-get update
sudo apt-get install -y mongodb
Bagian II
Setelah menginstall php dan mongodb, kita akan menambahkan extension mongodb pada php agar database mongodb dapat digunakan pada script php;
sebelumnya install git terlebih dahulu,
sudo apt-get install -y git-core
git config --global user.name "useranda" git config --global user.email "emailanda"setelah itu kita akan menginstall MongoDB driver secara manual, lakukan langkah berikut;
source : https://www.php.net/manual/en/mongodb.installation.manual.php
$ git clone https://github.com/mongodb/mongo-php-driver.git $ cd mongo-php-driver $ git submodule update --init $ phpize
untuk phpize jika tidak bisa silahkan ikuti langkah berikut,sudo apt install php7.0-dev
kemudian test untuk cek versi,phpize7.0 -v
lalu jalankan$ ./configure $ make all $ sudo make installphpize7.0
Setelah melakukan perintah make install maka akan muncul seperti ini,
Installing shared extensions: /usr/lib/php/extensions/debug-non-zts-20151012/
atau
Installing shared extensions: /usr/lib/php/20151012/
maka mongodb.so akan muncul di direktori tersebut,
nah setelah itu kita masukkan direktori diatas ke dalam php.ini dengan perintah
lalu tambahkan script berikut,nano /etc/php/7.0/cli/php.ini
extension=/usr/lib/php/20151012/mongodb.so
/etc/init.d/apache2 restart
Maka PHP-MongoDB telah berhasil di tambahkan untuk mengecek apakah MongoDB sudah bisa digunakan cek pada phpinfo() maka akan muncul seperti ini;
Bagian III
untuk membangun koneksi php dengan mongodb ikuti langkah berikut,
Make a Connection and Select a Database
To make a connection, you need to specify the database name, if the database doesn't exist then MongoDB creates it automatically.
Following is the code snippet to connect to the database −
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb; echo "Database mydb selected"; ?>
When the program is executed, it will produce the following result −
Connection to database successfully Database mydb selected
Create a Collection
Following is the code snippet to create a collection −
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb; echo "Database mydb selected"; $collection = $db->createCollection("mycol"); echo "Collection created succsessfully"; ?>
When the program is executed, it will produce the following result −
Connection to database successfully Database mydb selected Collection created succsessfully
Insert a Document
To insert a document into MongoDB, insert() method is used.
Following is the code snippet to insert a document −
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb; echo "Database mydb selected"; $collection = $db->mycol; echo "Collection selected succsessfully"; $document = array( "title" => "MongoDB", "description" => "database", "likes" => 100, "url" => "http://www.tutorialspoint.com/mongodb/", "by" => "tutorials point" ); $collection->insert($document); echo "Document inserted successfully"; ?>
When the program is executed, it will produce the following result −
Connection to database successfully Database mydb selected Collection selected succsessfully Document inserted successfully
Find All Documents
To select all documents from the collection, find() method is used.
Following is the code snippet to select all documents −
<?php // connect to mongodb $m = new MongoClient(); echo "Connection to database successfully"; // select a database $db = $m->mydb; echo "Database mydb selected"; $collection = $db->mycol; echo "Collection selected succsessfully"; $cursor = $collection->find(); // iterate cursor to display title of documents foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
When the program is executed, it will produce the following result −
Connection to database successfully Database mydb selected Collection selected succsessfully { "title": "MongoDB"
}
Mungkin cukup sekian, jika ada yang perlu ditanyakan silahkan komen dibawah. Terima kasih.
0 Comments