当前位置:网站首页>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!
leetcode: 529. Minesweeper Game
The technological achievements of Shanghai Konan were selected into the "2021 Shanghai Network Security Industry Innovation Research Achievement Catalog" by the Municipal Commission of Economy and Inf
营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
leetcode: 529. 扫雷游戏
轩辕实验室丨欧盟EVITA项目预研 第一章(四)
ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
seata源码解析:TM RM 客户端的初始化过程
Dry goods!Generative Model Evaluation and Diagnosis
Redis源码解析:Redis Cluster
随机推荐
Redis源码解析:Redis Cluster
蚁剑webshell动态加密连接分析与实践
How ali cloud storage database automatically to speed up the loading speed of www.cxsdkt.cn how to set up the case?
开源一夏|OpenHarmony如何查询设备类型(eTS)
Advanced usage of C language
深度学习21天——卷积神经网络(CNN):天气识别(第5天)
周报2022-8-4
Creo 9.0 基准特征:基准点
Oracle临时表空间作用
Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
leetcode 剑指 Offer 10- I. 斐波那契数列
seata源码解析:TM RM 客户端的初始化过程
无题一
Dry goods!Generative Model Evaluation and Diagnosis
js 图形操作一(兼容pc、移动端实现 draggable属性 拖放效果)
PAT Grade B-B1020 Mooncake(25)
我的杂记链接
IDEA执行Test操作导致数据插入时出现了重复数据
ffmpeg drawtext 添加文本水印
PAT乙级-B1021 个位数统计(15)