Home >Backend Development >PHP Tutorial >Example tutorial of mongodb
I was looking for information this morning and found mongodb. I studied it when I had nothing to do. I used the phpstudy integration package to add the mongodb extension.
1. Add mongo and mongodb extensions
The phpstudy integrated environment generally uses 32-bit php. Go to the official website to download 32-bit mongo and mongodb extensions. mongodb extension
Mongo:
mongodb:
Choose your own PHP version Download the corresponding extension library, unzip the file and copy the .dll file inside to the ext directory of php, and add the following two lines to the php.ini file:
extension=php_mongo. dll
## extension=php_mongodb.dll
Restart the phpstudy environment, visit the phpinfo() page, and see mongo, The mongodb extension indicates that the installation is successful.
2.mongodb add, delete, modify check
a.Link mongodb
$conn = new MongoClient ();//不写参数就是用本地mongodb,'localhost:27017',远程服务器就写远程地址和端口 $alldb = $conn ->listDBs(); //var_dump($alldb); 看是否有返回结构,有就表示连接成功。 $db = $conn->demo; //选择数据库,如果不存在则会自动创建 $collection = $db->test; //集合相当于数据表,如果不存在则会自动创建
b.curd operation
1 //插入数据 2 $insertArray = array('id'=>rand(1,50),'name'=>'admin','pwd'=>md5("123456"));//格式是数组的key(列)和value(列值) 3 $insertRes = $collection->insert($insertArray); 4 5 //获得所有数据 6 $where = array("id"=>array('$gt'=>20)); //可以where查询条件也是以数组形式 7 $selectDb = $collection->find($where)->fields(array('name'=>true,'pwd'=>true)); 8 $array = array(); 9 foreach ($selectDb as $id => $value) {10 $array[] = $value;11 }12 13 //获得一条数据14 $selectOne = $collection->findOne();15 16 //更新数据17 $sign = array("name" => 'admin');18 $param = array("name" => 'admin888','pwd'=>md5('12345'));19 $updateRes = $collection->update($sign, $param);20 21 //删除数据22 $collection->remove(array('name'=>'hm')); 、、删除指定条件数据23 $conn -> dropDB('demo');//删除库24 $collection->remove();//清空集合(删除所有数据)25 26 //断开MongoDB连接 27 $m->close();
Other information:
The above is the detailed content of Example tutorial of mongodb. For more information, please follow other related articles on the PHP Chinese website!