TP5 of thinkPHP5 Framework mongodb Extension Installation and Special Operation Examples

  • 2021-11-02 00:09:35
  • OfStack

This article illustrates the TP5 (thinkPHP5) framework mongodb extension installation and special operations. Share it for your reference, as follows:

ThinkPHP 5.0 MongoDb Drive

First install the official mongodb extension:

http://pecl.php.net/package/mongodb

Find the extension of the corresponding php version

Then, the type parameter of the database configuration file database. php for the configuration application is:


'type' => '\think\mongo\Connection',

You can use MongoDb normally, for example:

Use the latest mongodb extensions


Db::name('demo')
  ->find();
Db::name('demo')
  ->field('id,name')
  ->limit(10)
  ->order('id','desc')
  ->select();

Or use model operations:


User::get(1);
User::all('1,2,3');

The default primary key of MongoDb is _ id and is an ObjectID object. If you want to use id as the primary key like mysql1, you can use the following parameters:


//  Forcibly put _id Convert to id
'pk_convert_id' => true,

tp5 Mongodb Special Operation

Push operation

Add data


public function add(){
    $this->data = [
      ' Author '  => 'tuzi',
      ' Age '  => '22',
      ' Title '  => ' Defense tower and replenishment of troops ',
      ' Comments '  => [
        [
          ' Serial number '  => '001',
          ' Content '  => '5 Kill '
        ]
      ]
    ];
    $res = Db::table('document')->insert($this->data);
    if($res){
      echo "success";
    }else{
      echo "error";
    }
}

Results

array(1) {
[0] = > array(5) {
["_id"] = > object(MongoDB\BSON\ObjectId)#12 (1) {
["oid"] = > string(24) "5a51f73083869e4b65549c36"
}
["Author"] = > string(4) "tuzi"
["Age"] = > string(2) "22"
["Title"] = > string (15) "Anti-tower and Replenishment"
["Comment"] = > array(1) {
[0] = > array(2) {
["Serial number"] = > string(3) "001"
["Content"] = > string (6) "5 Kills"
}
}
}
}

Update data with push


public function update()
{
    $update_data[' Comments '] = [
      '$push',
      [
        ' Serial number '  => '002',
        ' Content '  => '3 Kill '
      ]
    ];
    $update_res = Db::table('document')->where(' Title ',' Defense tower and replenishment of troops ')->update($update_data);
    if($update_res){
      echo "success";
    }else{
      echo "error";
    }
}

Results

array(1) {
[0] = > array(5) {
["_id"] = > object(MongoDB\BSON\ObjectId)#12 (1) {
["oid"] = > string(24) "5a51f73083869e4b65549c36"
}
["Author"] = > string(4) "tuzi"
["Age"] = > string(2) "22"
["Title"] = > string (15) "Anti-tower and Replenishment"
["Comment"] = > array(2) {
[0] = > array(2) {
["Serial number"] = > string(3) "001"
["Content"] = > string (6) "5 Kills"
}
[1] = > array(2) {
["Serial number"] = > string(3) "002"
["Content"] = > string (6) "3 Kills"
}
}
}
}

Readers who are interested in thinkPHP can check the topics of this site: "Introduction to ThinkPHP", "Summary of thinkPHP Template Operation Skills", "Summary of Common Methods of ThinkPHP", "Introduction to codeigniter", "Advanced Course of CI (CodeIgniter) Framework", "Introduction to Zend FrameWork Framework" and "Summary of PHP Template Technology".

I hope this article is helpful to PHP programming based on ThinkPHP framework.


Related articles: