目录插入数据查询数据更新数据删除数据 PHP7里面使用如下库,操作比较复杂 php7连接mongoDB语法如下: //参数规则: monGodb://账号:密码@IP:端口/数据库 $manager = new MongoDBD
目录
PHP7里面使用如下库,操作比较复杂
//参数规则: monGodb://账号:密码@IP:端口/数据库
$manager = new MongoDBDriverManager("mongodb://php:123456@localhost:27017/php");
//1.连接MongoDB
$manager = new MongoDBDriverManager("mongodb://php:123456@localhost:27017/php");
//2.创建一个BulkWrite对象
$bulk = new MongoDBDriverBulkWrite();
$bulk->insert(["name" => "bashlog", "age" => 26, "email" => "bashlog@foxmail.com"]);
$bulk->insert(["name" => "itbsl", "age" => 12, "email" => "itbsl@foxmail.com"]);
//3.执行插入
$manager->executeBulkWrite("php.stu", $bulk);
查看插入情况
//1.连接MongoDB
$manager = new MongoDBDriverManager("mongodb://php:123456@localhost:27017/php");
//2.创建一个Query对象
$filter = ["age" => ["$gt" => 5]];
$options = [
"sort" => ["age" => -1]
];
$query = new MongoDBDriverQuery($filter, $options);
$cursor = $manager->executeQuery("php.stu", $query);
foreach ($cursor as $document) {
var_dump($document);
}
//1.规则:mongodb://账号:密码@IP:端口/数据库
$manager = new MongoDBDriverManager("mongodb://php:123456@localhost:27017/php");
//2.创建一个BulkWrite对象
$bulk = new MongoDBDriverBulkWrite();
$bulk->update(
["age" => 12],
["$set" => ["name" => "kitty", "age" => 122]],
["multi" => false, "upsert" => false]
);
$writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite("php.stu", $bulk, $writeConcern);
//1.规则:mongodb://账号:密码@IP:端口/数据库
$manager = new MongoDBDriverManager("mongodb://php:123456@localhost:27017/php");
//2.创建一个BulkWrite对象
$bulk = new MongoDBDriverBulkWrite();
//limit为1时,删除第一条匹配的数据
//limit为0时,删除所有匹配数据
$bulk->delete(["age" => 122], ["limit" => 1]);
$writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite("php.stu", $bulk, $writeConcern);
--结束END--
本文标题: PHP7操作MongoDB
本文链接: https://lsjlt.com/news/7578.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0