PHP操作Mongodb

小编复习MongoDB,基础请参考:MongoDB-CSDN博客(主要是linux操作)

PHP操作

<?php

/**

* 注意 本类仅适用于PHP7.0版本以上

* 请注意:mongoDB 支持版本 3.2+

* mongo具体参数参考: https://docs.mongodb.com/manual/reference/command/

*/

class MyMongodb {

private $manager;

private $dbname='yun';

/**

* 创建实例

* @param string $confkey

* @return object

*/

public function __construct($dns){

this-\>manager = new MongoDB\\Driver\\Manager(dns);

}

/**

* 插入

*/

public function insert(table,data){

$bulk = new MongoDB\Driver\BulkWrite;

bulk-\>insert(data);

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);

res = this->manager->executeBulkWrite(this-\>dbname.'.'.table, bulk, writeConcern);

return $res?true:false;

}

public function insert_batch(table,data)

{

$bulk = new MongoDB\Driver\BulkWrite;

foreach (data as val){

bulk-\>insert(val);

}

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);

res = this->manager->executeBulkWrite(this-\>dbname.'.'.table, bulk, writeConcern);

return $res?true:false;

}

/**

* 查询

* eg:'age' =\> 24]

* eg;$options = [

* 'projection' => '_id' =\> 0, //不输出_id字段

* 'sort' => 'leavetime'=\>-1 //根据user_id字段排序 1是升序,-1是降序

* ];

*/

public function select(table,filter,$options=array()){

!$filter && dieError('param of filter is error');

$options'projection'='_id' =\> 0;

query = new MongoDB\\Driver\\Query(filter, $options); //查询请求

cursor = this->manager->executeQuery(this-\>dbname.'.'.table, $query);

$result = \[\];

foreach(cursor as doc) {

result\[\] = (array)doc;

}

return $result;

}

/**

* 修改

* eg:$condition='name' =\> 'JetWu5'

* eg:set_array= \['set' => 'age' =\> 30, 'promise' =\> 'always smile!']

*/

public function update(table,condition=array(),$set_array=array()){

!$condition && dieError('param of condition is error');

!$set_array && dieError('param of set_array is error');

$bulk = new MongoDB\Driver\BulkWrite;

$bulk->update(

$condition,

$set_array

);

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认

res = this->manager->executeBulkWrite(this-\>dbname.'.'.table, bulk, writeConcern);

return $res?true:false;

}

/**

* 删除

* eg:$condition='name' =\> 'JetWu5'

* if $condition==\[\] then delete all table documents!

*/

public function delete(table,condition=\[\]){

!is_array($condition) && dieError('param of condition is error');

$bulk = new MongoDB\Driver\BulkWrite;

bulk-\>delete(condition);

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认

res = this->manager->executeBulkWrite(this-\>dbname.'.'.table, bulk, writeConcern);

return $res?true:false;

}

function exec($opts) {

cmd = new MongoDB\\Driver\\Command(opts);

cursor = this->manager->executeCommand(this-\>dbname, cmd);

$result = \[\];

foreach(cursor as doc) {

result\[\] = (array)doc;

}

return $result;

}

}

关键字

$eq:等于

$gt:大于

$gte:大于等于

$lt:小于

$lte:小于等于

$ne:不等于

$in:在指定数组中

$nin:不在指定数组中

$and:逻辑与

$or:逻辑或

$not:逻辑非

$exists:字段是否存在

//数据库链接

$mongodb = new MongoDB\Driver\Manager('mongodb://localhost:27017');

//数据写入

$bulk = new MongoDB\Driver\BulkWrite;

$bulk->insert('x' =\> 1, 'name'=\>'测试数据1', 'type' =\> 1,'desc'=\>'描述1');

$bulk->insert('x' =\> 2, 'name'=\>'测试数据2', 'type' =\> 2,'desc'=\>'描述2');

$bulk->insert('x' =\> 3, 'name'=\>'测试数据3', 'type' =\> 1,'desc'=\>'描述3');

$bulk->insert('x' =\> 4, 'name'=\>'测试数据4', 'type' =\> 2,'desc'=\>'描述4');

$bulk->insert('x' =\> 5, 'name'=\>'测试数据5', 'type' =\> 1,'desc'=\>'描述5');

$bulk->insert('x' =\> 6, 'name'=\>'测试数据6', 'type' =\> 2,'desc'=\>'描述6');

mongodb-\>executeBulkWrite('message.test', bulk);//数据写入(写入meaasge库的test表)

//数据查询

filter = \['x' =\> \['lte' => 3]];

$options = [

'projection' => '_id' =\> 0,

'sort' => 'x' =\> 1,

];

query = new MongoDB\\Driver\\Query(filter, $options); // 查询数据(预处理语句)

cursor = mongodb->executeQuery('message.test', $query);//执行查询

foreach (cursor as document) {//数据循环输出

print_r($document);

}

//数据修改

$bulk = new MongoDB\Driver\BulkWrite;

$bulk->update(

'x' =\> 2,

'$set' =\> \['name' =\> '测试数据22', 'desc' =\> '描述22'],

'multi' =\> false, 'upsert' =\> false

);

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);

result = mongodb->executeBulkWrite('message.test', bulk, writeConcern);

var_dump($result);

//数据删除

$bulk = new MongoDB\Driver\BulkWrite;

$bulk->delete('x' =\> 1, 'limit' =\> 1); // limit 为 1 时,删除第一条匹配数据,limit 为 0 时,删除所有匹配数据

$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);

result = mongodb->executeBulkWrite('message.test', bulk, writeConcern);

var_dump($result);

die;

相关推荐
第九序言T4 小时前
Python 数据处理进阶(十八)批量处理同结构文件:一个文件夹一次跑完
开发语言·python·自动化·pandas·数据处理
龙亘川5 小时前
构建城市公共安全数字底座:亘川智城智慧安防系统全维度解读
java·大数据·开发语言·人工智能·信息可视化
程序喵大人5 小时前
【C++入门】内存模型与资源管理 - 09 shared_ptr 和 weak_ptr 解决共享,也带来代价
开发语言·c++·资源管理·智能指针·内存模型
Raas1006 小时前
AI网关部署方案?从部署到上线全流程
java·开发语言·人工智能·gateway·ai网关·企业级ai网关·企业级ai产品
niucloud-admin6 小时前
NIUCLOUD PHP SAAS上门家政使用文档:城市管理
java·前端·php
专注仿真6 小时前
线性回归和逻辑回归算法详解
开发语言·人工智能·python·大模型训练·控制算法
ESDWAN7 小时前
企业网络监控应该监测哪些目标?网关、网络节点还是业务系统?
服务器·网络·php
jimy17 小时前
C++ 中的 `T&&`:右值引用与转发引用
开发语言·c++
呆呆虫127 小时前
用 orjson 加速 SerpBase 响应解析:基准对比与注意事项
开发语言·php
心疼你的一切8 小时前
Python自动发邮件实战定时发报表
大数据·开发语言·python