当前位置:网站首页>PHP 操作mangoDb
PHP 操作mangoDb
2022-08-05 09:41:00 【不知道反正就是挺叼】
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// 插入数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['id' => 1, 'name'=>'测试', 'url' => 'csdn.com']);
$manager->executeBulkWrite('test.jihe1', $bulk);
// 查询
$filter = ['name' => '测试'];
$options = [
'projection' => ['_id' => 0],
'sort' => ['name' => -1],
];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('test.jihe', $query);
foreach ($cursor as $document) {
print_r($document);
}
// 编辑
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['id' => 1],
['$set' => ['name' => '测试1234']],
['multi' => false, 'upsert' => false] #multi表示只更新一条数据,upsert表示如果不存在update的记录,不进行插入操作
);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.jihe', $bulk, $writeConcern);
// 删除
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['id' => 1], ['limit' => 1]); // limit 为 1 时,删除第一条匹配数据
$bulk->delete(['id' => 2], ['limit' => 0]); // limit 为 0 时,删除所有匹配数据
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.jihe', $bulk, $writeConcern);
?>
边栏推荐
猜你喜欢
Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
Tanabata romantic date without overtime, RPA robot helps you get the job done
express hot-reload
5.部署web项目到云服务器
19. Server-side session technology Session
js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
2.4G无线收发模块的应用
【LeetCode】623. Add a row to the binary tree
并发之CAS
交换机端口的三种类型详解与hybrid端口实验
随机推荐
IO stream articles -- based on io stream to realize folder copy (copy subfolders and files in subfolders) full of dry goods
微服务 技术栈
如何实现按键的短按、长按检测?
周报2022-8-4
正则表达式replaceFirst()方法具有什么功能呢?
正则表达式replaceAll()方法具有什么功能呢?
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
七夕浪漫约会不加班,RPA机器人帮你搞定工作
hcip BGP 增强实验
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
PAT Grade B-B1020 Mooncake(25)
dotnet OpenXML parsing PPT charts Getting started with area charts
5. Deploy the web project to the cloud server
这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
无题十四
Microservice Technology Stack
2022-08-01 Review the basic binary tree and operations
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的
Open Source Summer | How OpenHarmony Query Device Type (eTS)