Copyright
©
vietjack.com
MongoDB với PHP
Để sử dụng MongoDB với PHP, bạn cần sử dụng MongoDB PHP Driver. Tải Driver từ Tải
PHP Driver. Bạn nên tải phiên bản mới nhất. Sau đó unzip và đặt php_mongo.dll vào trong
thư mục PHP Extension của bạn (ext theo mặc định) và thêm dòng sau vào php.ini file:
extension=php_mongo.dll
Tạo kết nối và Chọn một Database
Để tạo kết nối, bạn cần xác định tên của Database, nếu nó không tồn tại thì MongoDB sẽ
tự động tạo nó.
Bạn theo dõi code sau:
// connect to mongodb
$m = new MongoClient();
echo "Connection
to database successfully";
// select a database
$db = $m->mydb;
echo
"Database mydb selected"; ?>
Khi code trên được biên dịch và thực thi, kết quả là:
Connection to database successfully Database mydb selected
Tạo một Collection
Bạn theo dõi code sau để tạo một Collection:
// 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"; ?>
Khi code trên được biên dịch và thực thi, kết quả là:
Connection to database successfully Database mydb selected Collection created
succsessfully
Chèn một Document
Để chèn một Document vào trong MongoDB, bạn sử dụng phương thức insert().
Bạn theo dõi code sau:
Trang
chia
sẻ
các
bài
học
online
miễn
phí
Copyright
©
vietjack.com
// 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" =>
" />"by", "tutorials point"
);
$collection->insert($document);
echo "Document inserted successfully"; ?>
Khi code trên được biên dịch và thực thi, kết quả là:
Connection to database successfully Database mydb selected Collection selected
succsessfully Document inserted successfully
Tìm tất cả Document
Để chọn tất cả Document từ Collection, bạn sử dụng phương thức find().
Bạn theo dõi code sau:
// 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";
} ?>
Khi code trên được biên dịch và thực thi, kết quả là:
Connection to database successfully Database mydb selected Collection selected
succsessfully {
"title": "MongoDB" }
Cập nhật một Document
Để cập nhật một Document, bạn cần sử dụng phương thức update().
Trong ví dụ dưới, chúng ta sẽ cập nhật title của Document đã chèn. Chương trình sau minh
họa điều này.
// 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";
// now update the document
$collection>update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB
Tutorial")));
echo "Document updated successfully";
// now display the
updated document
$cursor = $collection->find();
// iterate cursor to
display title of documents
echo "Updated document";
foreach ($cursor as
$document) {
echo $document["title"] . "\n";
} ?>
Trang
chia
sẻ
các
bài
học
online
miễn
phí
Copyright
©
vietjack.com
Khi code trên được biên dịch và thực thi, kết quả là:
Connection to database successfully Database mydb selected Collection selected
succsessfully Document updated successfully Updated document {
"title":
"MongoDB Tutorial" }
Xóa một Document
Để xóa một Document, bạn cần sử dụng phương thức remove().
Trong ví dụ dưới, chúng ta sẽ xóa các Document mà có title là MongoDB Tutorial. Chương
trình sau minh họa điều này.
// 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";
// now remove the document
$collection>remove(array("title"=>"MongoDB Tutorial"),false);
echo "Documents deleted
successfully";
// now display the available documents
$cursor =
$collection->find();
// iterate cursor to display title of documents
echo
"Updated document";
foreach ($cursor as $document) {
echo
$document["title"] . "\n";
} ?>
Khi code trên được biên dịch và thực thi, kết quả là:
Connection to database successfully Database mydb selected Collection selected
succsessfully Documents deleted successfully
Trong ví dụ trên, tham số thứ hai là kiểu Boolean và được sử dụng cho
trường justOne của phương thức remove().
Các phương thức findOne(), save(), limit(), skip(), sort(), ... của MongoDB sẽ làm việc
tương tự như đã trình bày ở trên.
Trang
chia
sẻ
các
bài
học
online
miễn
phí