Top Posts
Gửi mail trong Magento 2 – Đính kèm...
Tạo grid trong Magento 2 sử dụng block
Tạo form admin sử dụng block
Cách sử dụng insertListing trong Magento 2
Compile LESS file với Grunt trong Magento 2
Magento 2 get product image url
Format price với currency trong Magento 2
Magento 2 Add URL Rewrite programmatically
Magento 2 Upload image admin form
Magento 2 Create Category Attribute
vi-magento.com
  • Magento 2
    • Magento 2 cơ bản
    • Bài tập Magento 2
    • Magento 2 Api
  • PHP
  • JavaScript
  • Cơ sở dữ liệu
    • Học MongoDB
    • SQL Server
    • Mysql
    • Học Oracle
    • SQLite
  • Liên hệ
  • Trắc nghiệm Magento 2
Trang chủ Cơ sở dữ liệuHọc MongoDB MongoDB – PHP
Học MongoDB

MongoDB – PHP

bởi admin 17/07/2021
bởi admin 17/07/2021 0 Bình luận 39 xem

Để 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:

1
2
3
 
extension=php_mongo.dll
 

Danh mục

  1. Tạo kết nối và Chọn một Database
  2. Tạo một Collection
  3. Chèn một Document
  4. Tìm tất cả Document
  5. Cập nhật một Document
  6. Xóa một Document

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:

1
2
3
4
5
6
7
8
9
10
 
<?php
   // 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à:

1
2
3
4
 
Connection to database successfully
Database mydb selected
 

Tạo một Collection

Bạn theo dõi code sau để tạo một Collection:

1
2
3
4
5
6
7
8
9
10
11
12
 
<?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";
?>
 

Khi code trên được biên dịch và thực thi, kết quả là:

1
2
3
4
5
 
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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";
?>
 

Khi code trên được biên dịch và thực thi, kết quả là:

1
2
3
4
5
6
 
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<?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";
   }
?>
 

Khi code trên được biên dịch và thực thi, kết quả là:

1
2
3
4
5
6
7
8
 
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
<?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";
 
   // 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";
   }
?>
 

Khi code trên được biên dịch và thực thi, kết quả là:

1
2
3
4
5
6
7
8
9
10
 
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
<?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";
 
   // 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à:

1
2
3
4
5
6
 
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.

MongoDB với PHPsử dụng MongoDB với PHP
Chia sẻ
0
FacebookSkype
Bài trước
Relationship trong MongoDB
Bài tiếp theo
MongoDB – Java

Related Posts

Xóa Document trong MongoDB

17/07/2021

Tham chiếu Database trong MongoDB

17/07/2021

GridFS trong MongoDB

17/07/2021

Chỉ mục (Index) trong MongoDB

17/07/2021

Cập nhật Document trong MongoDB

17/07/2021

Xóa Database trong MongoDB

17/07/2021

Relationship trong MongoDB

17/07/2021

Tạo Database trong MongoDB

17/07/2021
0 0 vote
Đánh giá
Login
guest
guest
0 Comments
Inline Feedbacks
View all comments

Bài viết cùng chủ đề

  • GridFS trong MongoDB

  • Làm việc với Rockmongo

  • Regular Expression trong MongoDB

  • Text Search trong MongoDB

  • Map Reduce trong MongoDB

  • ObjectId trong MongoDB

  • Hạn chế của chỉ mục trong MongoDB

  • Hoạt động chỉ mục nâng cao trong MongoDB

  • Hoạt động nguyên tử (Atomic Operation) trong MongoDB

  • Phân tích truy vấn trong MongoDB

  • Covered Query trong MongoDB

  • Tham chiếu Database trong MongoDB

  • Relationship trong MongoDB

  • MongoDB – Java

  • MongoDB Deployment

  • Tạo Backup trong MongoDB

  • Shard trong MongoDB

  • Replica Set trong MongoDB

  • Aggregation trong MongoDB

  • Chỉ mục (Index) trong MongoDB

  • Sắp xếp bản ghi trong MongoDB

  • Giới hạn bản ghi trong MongoDB

  • Projection trong MongoDB

  • Xóa Document trong MongoDB

  • Cập nhật Document trong MongoDB

  • Truy vấn Document trong MongoDB

  • Chèn Document trong MongoDB

  • Kiểu dữ liệu trong MongoDB

  • Xóa Collection trong MongoDB

  • Tạo Collection trong MongoDB

  • Xóa Database trong MongoDB

  • Tạo Database trong MongoDB

  • Mô hình hóa dữ liệu trong MongoDB

  • Cài đặt MongoDB trên Windows

  • Lợi thế của MongoDB

  • Tổng quan về MongoDB

@2020 - All Right Reserved. vi-magento.com

vi-magento.com
  • Magento 2
    • Magento 2 cơ bản
    • Bài tập Magento 2
    • Magento 2 Api
  • PHP
  • JavaScript
  • Cơ sở dữ liệu
    • Học MongoDB
    • SQL Server
    • Mysql
    • Học Oracle
    • SQLite
  • Liên hệ
  • Trắc nghiệm Magento 2
wpDiscuz