php7 安装mongodb4.0.4的扩展以及操作mongodb的方法

Posted saintdingtheGreat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php7 安装mongodb4.0.4的扩展以及操作mongodb的方法相关的知识,希望对你有一定的参考价值。

数据插入:

$manager = new MongoDBDriverManager(‘mongodb://localhost:27017‘);
$bulk = new MongoDBDriverBulkWrite;
$bulk->insert([‘name‘ => ‘JetWu5‘, ‘age‘ => 26]);
$bulk->insert([‘name‘ => ‘JetWu6‘, ‘age‘ => 26]);
$writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);//可选,修改确认
$res = $manager->executeBulkWrite(‘wjt.friend‘, $bulk, $writeConcern);
echo ‘<pre>‘;
print_r($res);

数据查询:

$manager = new MongoDBDriverManager(‘mongodb://localhost:27017‘);
$query = new MongoDBDriverQuery([‘age‘ => 24], [‘sort‘ => [‘age‘ => 1]]);
$cursor = $manager->executeQuery(‘wjt.friend‘, $query);
$data = [];
foreach($cursor as $doc) {
  $data[] = $doc;
}
echo ‘<pre>‘;
print_r($data);

数据修改:

 
$manager = new MongoDBDriverManager(‘mongodb://localhost:27017‘);
$bulk = new MongoDBDriverBulkWrite;
$bulk->update(
  [‘name‘ => ‘JetWu5‘],
  [‘$set‘ => [‘age‘ => 30, ‘promise‘ => ‘always smile!‘]]
);
$writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);//可选,修改确认
$res = $manager->executeBulkWrite(‘wjt.friend‘, $bulk, $writeConcern);
echo ‘<pre>‘;
print_r($res);

数据删除:

 
$manager = new MongoDBDriverManager(‘mongodb://localhost:27017‘);
$bulk = new MongoDBDriverBulkWrite;
$bulk->delete([‘name‘ => ‘JetWu3‘]);
$bulk->delete([‘name‘ => ‘JetWu4‘]);
$writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);//可选,修改确认
$res = $manager->executeBulkWrite(‘wjt.friend‘, $bulk, $writeConcern);
echo ‘<pre>‘;
print_r($res);

 

以上是关于php7 安装mongodb4.0.4的扩展以及操作mongodb的方法的主要内容,如果未能解决你的问题,请参考以下文章

windows下php7.1安装redis扩展以及redis测试使用全过程

centos 下编译安装php7.3的redis扩展

centos8 安装php7.2以及php-fpm

PHP7安装intl扩展和linux安装icu

php7.2 安装mcrypt扩展

php7安装redis扩展和memcache扩展