Home > Backend Development > PHP Tutorial > PHP operates MongoDB to implement addition, deletion, modification and query functions. PHP skills

PHP operates MongoDB to implement addition, deletion, modification and query functions. PHP skills

jacklove
Release: 2023-04-01 20:12:01
Original
2399 people have browsed it

This article mainly introduces PHP to operate MongoDB to realize the add, delete, modify and query functions. It summarizes and analyzes the common techniques of operating MongoDB in PHP in the form of examples. It also comes with the method of operating MongoDB in php7, which has certain reference value. Friends who need it can refer to it.

The example in this article describes the PHP operation of MongoDB to implement the add, delete, modify and query functions. Share it with everyone for your reference, the details are as follows:

MongoDB’s PHP driver provides some core classes to operate MongoDB. Generally speaking, it can implement all the functions in the MongoDB command line, and the format of the parameters Basically similar. Versions before PHP7 and versions after PHP7 have different operations on MongoDB. This article mainly uses the previous version of PHP7 as an example to explain the various operations of PHP on MongoDB. Finally, it briefly explains the operations of MongoDB on PHP7 and later versions.

1. Data insertion

//insert()
//参数1:一个数组或对象
//参数2:扩展选项
// fsync:默认为false,若为true则mongo在确认数据插入成功之前将会强制把数据写入硬盘
// j:默认为false,若为true则mongo在确认数据插入成功之前将会强制把数据写入日志
// w:默认为1,写操作会被(主)服务器确认,若为0则将不会得到确认,使用复制集时设置为n用于确保主服务器将数据修改成功复制到n个节点后再确认
// wtimeout:默认为10000(毫秒),用于指定服务器等待接收确认的时间
// timeout:指定客户端需要等待服务器响应的超时时间(毫秒)
$mongo = new MongoClient('mongodb://localhost:27017');
$db = $mongo->mf;//选择数据库
$collection = $db->friend;//选择文档集合
$doc = [//定义一个文档,即一个数组
  'First Name' => 'Jet',
  'Last Name' => 'Wu',
  'Age' => 26,
  'Phone' => '110',
  'Address' => [
    'Country' => 'China',
    'City' => 'Shen Zhen'
  ],
  'E-Mail' => [
    '123456@qq.com',
    '666666@sina.com',
    '8888888@qq.com',
    '77887788@qq.com'
  ]
];
$res = $collection->insert($doc);//向集合中插入一个文档
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);//$res[&#39;ok&#39;]=1表示插入成功
Copy after login

2. Data Query

1. Query a single document:

//findOne()
//参数1:搜索条件
//参数2:指定返回字段,array(&#39;fieldname&#39; => true, &#39;fieldname2&#39; => true)。_id字段总会返回,除非在第二个参数显式加入&#39;_id&#39;=>false。不设置则返回所有字段
$mongo = new MongoClient(&#39;mongodb://localhost:27017&#39;);
$db = $mongo->mf;
$collection = $db->friend;
$one = $collection->findOne([&#39;First Name&#39; => &#39;Jet&#39;]);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($one);//返回一个数组,查不到数据则返回NULL
Copy after login

2. Query multiple documents:

//find()
//参数1:搜索条件
//参数2:指定返回字段,array(&#39;fieldname&#39; => true, &#39;fieldname2&#39; => true)。_id字段总会返回,除非显式设置为false不返回。不设置则返回所有字段
$mongo = new MongoClient(&#39;mongodb://localhost:27017&#39;);
$db = $mongo->mf;
$collection = $db->friend;
$cursor = $collection->find([&#39;Address.Country&#39; => &#39;China&#39;]);//使用点操作符查找数组元素
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
while($doc = $cursor->getNext()) {//循环读取每个匹配的文档
  print_r($doc);
}
Copy after login

Define queries using various conditional operators:

//mongodb分别使用$lt、$lte、$eq、$gte、$gt、$ne表示<、<=、=、>=、>、<>,用于整数字段查询
$mongo = new MongoClient(&#39;mongodb://localhost:27017&#39;);
$db = $mongo->mf;
$collection = $db->friend;
$cursor = $collection->find([&#39;Age&#39; => [&#39;$gt&#39; => 30]]);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
while($doc = $cursor->getNext()) {
  print_r($doc);
}
//查询某个字段的所有不重复的值
$res = $collection->distinct(&#39;Age&#39;);
//$in:匹配多个值中任意一个
$cursor = $collection->find([&#39;Address.Country&#39; => [&#39;$in&#39; => [&#39;China&#39;, &#39;USA&#39;]]]);
//$all:匹配多个值中所有值(用于数组字段查询)
$cursor = $collection->find([&#39;E-Mail&#39; => [&#39;$all&#39; => [&#39;123456@qq.com&#39;, &#39;77887788@qq.com&#39;]]]);
//$or:或查询
$cursor = $collection->find([&#39;$or&#39; => [[&#39;First Name&#39; => &#39;Jet&#39;], [&#39;Address.Country&#39; => &#39;USA&#39;]]]);
//$slice:获取数组字段中指定数目的元素,位于find()函数第二个参数中
$cursor = $collection->find([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;E-Mail&#39; => [&#39;$slice&#39; => 2]]);//只返回前两个email
$cursor = $collection->find([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;E-Mail&#39; => [&#39;$slice&#39; => -2]]);//只返回最后两个email
$cursor = $collection->find([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;E-Mail&#39; => [&#39;$slice&#39; => [1, 2]]]);//忽略第一个,返回接下来两个
//$exists:根据某个字段是否有设置值进行查询
$cursor = $collection->find([&#39;Hobby&#39; => [&#39;$exists&#39; => false]]);//查找Hobby字段未设置值的文档
//正则表达式查询
$cursor = $collection->find([&#39;First Name&#39; => new MongoRegex(&#39;/^Je/i&#39;)]);//查找First Name字段以Je开头的文档,忽略大小写差异
Copy after login

Use other functions provided by the MongoCursor class:

//排序:1升序,-1降序
$cursor->sort([&#39;Age&#39; => 1]);
//忽略前n个匹配的文档
$cursor->skip(1);
//只返回前n个匹配的文档(limit()与skip()结合使用可实现数据分页功能)
$cursor->limit(1);
//匹配文档的总数
$cursor->count();
//指定查询索引
$cursor->hint([&#39;Last Name&#39; => -1]);//若索引不存在则会报错
Copy after login

Aggregate query: group data for statistics

//聚合查询:对数据进行分组统计
$mongo = new MongoClient(&#39;mongodb://localhost:27017&#39;);
$db = $mongo->mf;
$collection = $db->friend;
$res = $collection->aggregate([
  &#39;$group&#39; => [
    &#39;_id&#39; => &#39;$Address.Country&#39;,//分组字段,注意要加上“$”,这里是根据数组字段某个元素值进行分组
    &#39;total&#39; => [&#39;$sum&#39; => 1],//求总和,表示每匹配一个文档总和就加1
    &#39;maxAge&#39; => [&#39;$max&#39; => &#39;$Age&#39;],//分组中Age字段最大值
    &#39;minAge&#39; => [&#39;$min&#39; => &#39;$Age&#39;]//分组中Age字段最小值
  ]
]);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);//返回一个数组,$ret[&#39;result&#39;]为数组,存放统计结果
//存在其它操作的聚合查询:多个操作之间执行先后顺序取决于它们位置的先后顺序
//聚合查询中的所有操作,包括&#39;$group&#39;在内,都是可选的。
$mongo = new MongoClient(&#39;mongodb://localhost:27017&#39;);
$db = $mongo->mf;
$collection = $db->friend;
$res = $collection->aggregate([
  [//过滤条件:只对符合条件的原始文档进行聚合运算,若是放在&#39;$group&#39;之后则是只返回符合条件的结果文档
    &#39;$match&#39; => [&#39;Age&#39; => [&#39;$gt&#39; => 30]]
  ],
  [//指定分组字段、统计字段
    &#39;$group&#39; => [
      &#39;_id&#39; => &#39;$Address.Country&#39;,
      &#39;totalAge&#39; => [&#39;$sum&#39; => &#39;$Age&#39;]//计算各个分组Age字段总和
    ]
  ],
  //以下操作若是放在&#39;$group&#39;之前则在聚合前作用于原始文档,若放在&#39;$group&#39;之后则在聚合后作用于结果文档
  [&#39;$unwind&#39; => &#39;$E-Mail&#39;],//将包含有某个数组类型字段的文档拆分成多个文档,每个文档的同名字段的值为数组中的一个值。
  [&#39;$project&#39; => [&#39;myAge&#39; => &#39;$Age&#39;, &#39;First Name&#39; => &#39;$First Name&#39;]],//指定返回字段,可以对字段进行重命名,格式:返回字段名 => $原来字段名
  [&#39;$skip&#39; => 2],//跳过指定数量的文档
  [&#39;$limit&#39; => 2],//只返回指定数量的文档
  [&#39;$sort&#39; => [&#39;totalAge&#39; => 1]]//排序
]);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);
Copy after login

3. Data modification

//update()
//参数1:更新条件,指定更新的目标对象。
//参数2:指定用于更新匹配记录的对象。
//参数3:扩展选项组。
// upsert:若设置为true,当没有匹配文档的时候会创建一个新的文档。
// multiple:默认为false,若设置为true,匹配文档将全部被更新。
// fsync:若设置为true,w参数将被覆盖为0,数据将在更新结果返回前同步到磁盘。
// w:默认为1;若设置为0,更新操作将不会得到确认;使用复制集时可设置为n,确保主服务器在将修改复制到n个节点后才确认该更新操作
// j:默认为false,若设置为true,数据将在更新结果返回之前写入到日志中。
// wtimeout:默认为10000(毫秒),用于指定服务器等待接收确认的时间
// timeout:指定客户端需要等待服务器响应的超时时间(毫秒)
//注意:若不使用任何修改操作符,则匹配文档将直接被整个替换为参数2指定的对象。
//$inc:增加特定键的值,若字段不存在则新建字段并赋值
$mongo = new MongoClient(&#39;mongodb://localhost:27017&#39;);
$db = $mongo->mf;
$collection = $db->friend;
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$inc&#39; => [&#39;Age&#39; => 2]]);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);//$res[&#39;ok&#39;]=1表示修改成功,$res[&#39;nModified&#39;]表示修改的文档数量
//$set:重置特定键的值,若字段不存在则新建字段并赋值
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$set&#39; => [&#39;Hobby&#39; => &#39;pingpong&#39;]]);
//$unset:删除字段
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$unset&#39; => [&#39;Hobby&#39; => 1]]);
//$rename:重命名字段,若字段不存在则不进行任何操作
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$rename&#39; => [&#39;Hobby&#39; => &#39;hobby&#39;, &#39;Age&#39; => &#39;age&#39;]]);
//注意:如果文档中已经使用了指定名称的字段,则该字段将会被删除,然后再进行重命名操作。
//$setOnInsert:设置了upsert为true,并且发生了插入操作的时候,将某个字段设置为特定的
$res = $collection->update([&#39;First Name&#39; => &#39;jet&#39;], [&#39;$setOnInsert&#39; => [&#39;lang&#39; => &#39;English&#39;]], [&#39;upsert&#39; => true]);
//$push:向指定字段添加一个值(作用于数组字段),若字段不存在会先创建字段,若字段值不是数组会报错
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$push&#39; => [&#39;E-Mail&#39; => &#39;123123@qq.com&#39;]]);
//$push:向指定字段添加多个值(作用于数组字段),若字段不存在会先创建字段,若字段值不是数组会报错
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$pushAll&#39; => [&#39;E-Mail&#39; => [&#39;666@qq.com&#39;, &#39;8888888@qq.com&#39;]]]);
//使用$push和$each向某个字段添加多个值(作用于数组字段),若字段不存在会先创建字段,若字段值不是数组会报错
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$push&#39; => [&#39;E-Mail&#39; => [&#39;$each&#39; => [&#39;123123@qq.com&#39;, &#39;666@qq.com&#39;]]]]);
//$addToSet:将数据添加到数组中(只在目标数组没有该数据的时候才将数据添加到数组中)
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$addToSet&#39; => [&#39;E-Mail&#39; => &#39;123123@qq.com&#39;]]);
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$addToSet&#39; => [&#39;E-Mail&#39; => [&#39;$each&#39; => [&#39;123123@qq.com&#39;, &#39;666@qq.com&#39;]]]]);
//$pop:从数组中删除一个元素,-1表示删除第一个元素,1表示删除最后一个元素(其实负数都删除第一个元素,0或正数都删除最后一个元素)
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$pop&#39; => [&#39;E-Mail&#39; => 1]]);
//$pull:删除数组中所有指定值
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$pull&#39; => [&#39;E-Mail&#39; => &#39;123123@qq.com&#39;]]);
//$pullAll:删除数组中多个元素的所有值
$res = $collection->update([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$pullAll&#39; => [&#39;E-Mail&#39; => [&#39;123123@qq.com&#39;, &#39;666@qq.com&#39;]]]);
//save()
//参数1:希望保存的信息数组
//参数2:扩展选项
// fsync:若设置为true,w参数将被覆盖为0,数据将在更新结果返回前同步到磁盘。
// w:默认为1;若设置为0,更新操作将不会得到确认;使用复制集时可设置为n,确保主服务器在将修改复制到n个节点后才确认该更新操作
// j:默认为false,若设置为true,数据将在更新结果返回之前写入到日志中。
// wtimeout:默认为10000(毫秒),用于指定服务器等待接收确认的时间
// timeout:指定客户端需要等待服务器响应的超时时间(毫秒)
//注意:若已存在则更新,若不存在则插入;更新时使用参数1指定的信息数组替换整个文档。
//若想更新则应该在参数1中指定_id键的值。
$mongo = new MongoClient(&#39;mongodb://localhost:27017&#39;);
$db = $mongo->mf;
$collection = $db->friend;
$doc = [//定义一个文档,即一个数组
  &#39;First Name&#39; => &#39;Jet&#39;,
  &#39;Last Name&#39; => &#39;Wu&#39;,
  &#39;Age&#39; => 26,
  &#39;Phone&#39; => &#39;110&#39;,
  &#39;Address&#39; => [
    &#39;Country&#39; => &#39;China&#39;,
    &#39;City&#39; => &#39;Shen Zhen&#39;
  ],
  &#39;E-Mail&#39; => [
    &#39;123456@qq.com&#39;,
    &#39;666666@sina.com&#39;,
    &#39;8888888@qq.com&#39;,
    &#39;77887788@qq.com&#39;
  ]
];
$res = $collection->save($doc);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);//$res[&#39;ok&#39;]=1表示操作成功,$res[&#39;updatedExisting&#39;]=1表示更新,$res[&#39;upserted&#39;]=1表示插入
//findAndModify()
//参数1:指定查询条件
//参数2:指定用于更新文档的信息
//参数3:可选,指定希望返回的字段
//参数4:扩展选项
// sort:以特定顺序对匹配文档进行排序
// remove:若设置为true,第一个匹配文档将被删除
// update:若设置为true,将在被选择的文档上执行更新操作
// new:默认为false,若设置为true则返回更新后的文档,否则返回更新前的文档
// upsert:若设置为true,没有找到匹配文档的时候将插入一个新的文档
$mongo = new MongoClient(&#39;mongodb://localhost:27017&#39;);
$db = $mongo->mf;
$collection = $db->friend;
$res = $collection->findAndModify([&#39;First Name&#39; => &#39;Jet&#39;], [&#39;$push&#39; => [&#39;E-Mail&#39; => &#39;111@qq.com&#39;]]);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);
Copy after login

##4. Data deletion

//remove()
//参数1:查询条件
//参数2:扩展选项
// justOne:若设置为true,则最多只有一个匹配的文档将被删除
// fsync:若设置为true,w参数将被覆盖为0,数据将在更新结果返回前同步到磁盘。
// w:默认为1;若设置为0,更新操作将不会得到确认;使用复制集时可设置为n,确保主服务器在将修改复制到n个节点后才确认该更新操作
// j:默认为false,若设置为true,数据将在更新结果返回之前写入到日志中。
// wtimeout:默认为10000(毫秒),用于指定服务器等待接收确认的时间
// timeout:指定客户端需要等待服务器响应的超时时间(毫秒)
$mongo = new MongoClient(&#39;mongodb://localhost:27017&#39;);
$db = $mongo->mf;
$collection = $db->friend;
$res = $collection->remove([&#39;First Name&#39; => &#39;jet&#39;]);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);//$res[&#39;n&#39;]表示删除了几个文档
Copy after login

The above are the MongoDB operations of previous versions of PHP7. The following is a brief introduction to the operations of later versions of PHP7.

PHP7 operation method

Data insertion:

$manager = new MongoDB\Driver\Manager(&#39;mongodb://localhost:27017&#39;);
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert([&#39;name&#39; => &#39;JetWu5&#39;, &#39;age&#39; => 26]);
$bulk->insert([&#39;name&#39; => &#39;JetWu6&#39;, &#39;age&#39; => 26]);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认
$res = $manager->executeBulkWrite(&#39;wjt.friend&#39;, $bulk, $writeConcern);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);
Copy after login

Data query:

$manager = new MongoDB\Driver\Manager(&#39;mongodb://localhost:27017&#39;);
$query = new MongoDB\Driver\Query([&#39;age&#39; => 24], [&#39;sort&#39; => [&#39;age&#39; => 1]]);
$cursor = $manager->executeQuery(&#39;wjt.friend&#39;, $query);
$data = [];
foreach($cursor as $doc) {
  $data[] = $doc;
}
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($data);
Copy after login

Data modification:

$manager = new MongoDB\Driver\Manager(&#39;mongodb://localhost:27017&#39;);
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
  [&#39;name&#39; => &#39;JetWu5&#39;],
  [&#39;$set&#39; => [&#39;age&#39; => 30, &#39;promise&#39; => &#39;always smile!&#39;]]
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认
$res = $manager->executeBulkWrite(&#39;wjt.friend&#39;, $bulk, $writeConcern);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);
Copy after login

##Data deletion:

$manager = new MongoDB\Driver\Manager(&#39;mongodb://localhost:27017&#39;);
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete([&#39;name&#39; => &#39;JetWu3&#39;]);
$bulk->delete([&#39;name&#39; => &#39;JetWu4&#39;]);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认
$res = $manager->executeBulkWrite(&#39;wjt.friend&#39;, $bulk, $writeConcern);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);
Copy after login

Articles you may be interested in:

Common tips for PHP operation Redis Summary of PHP tips


Analysis of php skills on the method of reading and writing excel files in native php


Analysis of php skills on the principles and implementation methods of the singleton mode of PHP design pattern


The above is the detailed content of PHP operates MongoDB to implement addition, deletion, modification and query functions. PHP skills. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template