博客
关于我
PHP mongoDB 操作
阅读量:793 次
发布时间:2023-02-27

本文共 2460 字,大约阅读时间需要 8 分钟。

MongoDB操作笔记

1. 连接数据库

连接MongoDB数据库时,可以使用以下方式:

$conn = new Mongo();// 连接本地主机,默认端口$conn = new Mongo("172.21.15.69");// 连接远程主机$conn = new Mongo("xiaocai.loc:10086");// 连接指定端口的远程主机$conn = new Mongo("xiaocai.loc", array("replicaSet" => true));// 持久连接$conn = new Mongo("mongodb://sa:123@localhost");// 连接多个服务器$conn = new Mongo("mongodb://localhost:27017,localhost:27018");// 使用域套接字$conn = new Mongo("mongodb://admin_miss:miss@localhost:27017/test");

详细信息可参考 PHP手册 - 连接MongoDB

2. 选择数据库与表

选择数据库和集合时,可以使用以下方式:

$db = $conn->mydb;// 或者$db = $conn->selectDB("mydb");$collection = $db->column;// 或者$collection = $conn->mydb->column;

注意:数据库和集合在不存在时会自动创建。

3. 插入文档

向集合中插入数据:

$array = array("column_name" => "col" . rand(100, 999), "column_exp" => "xiaocai");$result = $collection->insert($array);

插入成功返回 true

安全插入数据:

$result = $collection->insert($array, true);

返回数组,包含 errnok

完整格式:

insert(array $a, array $options = array())

参数包括 safefsynctimeout,默认值均为 false

4. 更新文档

更新文档时,可以使用以下方式:

$where = array("column_name" => "col123");$newdata = array("column_exp" => "GGGGGGG", "column_fid" => 444);$result = $collection->update($where, array("$set" => $newdata));

替换更新:

$newdata = array("column_exp" => "HHHHHHHHH", "column_fid" => 123);$result = $collection->update($where, $newdata);

批量更新:

$newdata = array("column_exp" => "multiple", "91u" => 684435);$result = $collection->update($where, array("$set" => $newdata), array("multiple" => true));

删除节点:

$result = $collection->update($where, array("$unset" => "column_exp"));

5. 删除文档

删除文档时,可以使用以下方式:

$collection->remove(array("column_name" => "col399"));$collection->remove();

删除指定 ObjectId

$id = new MongoId("4d638ea1d549a02801000011");$collection->remove(array("_id" => (object)$id));

6. 查询文档

查询文档时,可以使用以下方式:

echo "count: " . $collection->count();

带条件查询:

echo "count: " . $collection->count(array("type" => array("$gt" => 50, "$lte" => 74)));

查询多条记录:

$cursor = $collection->find();foreach ($cursor as $id => $value) {    echo "$id: " . var_dump($value) . "\n";}

查询一条记录:

$cursor = $collection->findOne();

限制查询结果:

$cursor = $collection->find()->limit(5)->skip(0);

排序查询结果:

$cursor = $collection->find()->sort(array("age" => -1, "type" => 1));

7. 索引管理

创建索引:

$collection->ensureIndex(array("age" => 1, "type" => -1));$collection->ensureIndex(array("age" => 1, "type" => -1), array("background" => true));$collection->ensureIndex(array("age" => 1, "type" => -1), array("unique" => true));

8. 文档聚类

MongoDB 不支持文档聚类功能,以上内容仅为操作指南。

转载地址:http://qevfk.baihongyu.com/

你可能感兴趣的文章
OSChina 技术周刊第十期,每周技术抢先看!
查看>>
OSError: no library called “cairo-2“ was foundno library called “cairo“ was foundno library called
查看>>
Osgi环境配置
查看>>
OSG学习:几何体的操作(二)——交互事件、Delaunay三角网绘制
查看>>
OSG学习:几何对象的绘制(三)——几何元素的存储和几何体的绘制方法
查看>>
OSG学习:几何对象的绘制(二)——简易房屋
查看>>
OSG学习:几何对象的绘制(四)——几何体的更新回调:旋转的线
查看>>
OSG学习:场景图形管理(一)——视图与相机
查看>>
OSG学习:场景图形管理(三)——多视图相机渲染
查看>>
OSG学习:场景图形管理(二)——单窗口多相机渲染
查看>>
OSG学习:场景图形管理(四)——多视图多窗口渲染
查看>>
OSG学习:新建C++/CLI工程并读取模型(C++/CLI)——根据OSG官方示例代码初步理解其方法
查看>>
Sql 随机更新一条数据返回更新数据的ID编号
查看>>
OSG学习:空间变换节点和开关节点示例
查看>>
OSG学习:纹理映射(一)——多重纹理映射
查看>>
OSG学习:纹理映射(七)——聚光灯
查看>>
OSG学习:纹理映射(三)——立方图纹理映射
查看>>
OSG学习:纹理映射(二)——一维/二维/简单立方图纹理映射
查看>>
OSG学习:纹理映射(五)——计算纹理坐标
查看>>
OSG学习:纹理映射(六)——灯光
查看>>