关于MongoDB

MongoDB适用场景

  1. 网站的实时数据的处理,很适合实时数据的插入更新和查询
  2. 缓存性能很高,系统重启之后避免持久化缓存层数据源过载
  3. 高伸缩,数十台或数百台服务器的部署 MapReduce(大规模数据集(大于1TB)的并行运算)引擎

设计特征:

特殊:MongoDB 还提供创建基于地理空间的索引的能力{待考察}
强大的聚合函数:count group 支持使用MapReduce 的复杂

安装使用:

  1. 下载mongodb
    lsb_release -a 列出liunx版本信息(ubuntu自己的)
    进入:安装目录执行
    1.导入包管理系统使用的公钥
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

2.为MongoDB创建一个列表文件
根据版本创建/etc/apt/sources.list.d/mongodb-org-3.4.list 列表文件
Ubuntu 14.04

echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

Ubuntu 16.04

echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

3.更新本地包数据库

sudo apt-get update

4.安装最新版本的MongoDB

sudo apt-get install -y mongodb-org

安装完成后解压文件后的MongoDB是已经编译好的文件不需要再次安装
在这里插入图片描述
bsondump:导出bson结构
mongo:客户端相当于(mysql.exe)
mongod:服务端相当于(mysqld.exe)
mongodump:整体数据库导入(二进制相当于mysqldump)
mongoexport:导出容易识别的json或csv文档
mongos:路由器分片 在集群的时候会用到
mongorestore:数据库整体导入

4.启动服务

./bin/mongod --dbpath /path/to/database --logpath /path/to/log --fork --prot 27017
/tmp/mongodb-linux-x86_64-3.0.6/bin
参数解释:
–dbpath:数据存储目录
–logpath:日志存储目录
–port:运行端口 (默认27010)
–fork:后台运行程序)(由于我们nignx不能重启(丢文件))

ps aux| grep mongo(检查程序运行的端口号)
touch down
kill -9 端口号
rm -r down
mongod --启动服务
mongo–启动客户端服务

启动完之后占用:3-4g左右空间可以使用 --smallfiles 来启动

df -h:查看磁盘占用空间
ps -aux | grep 进程名 查看进程占用空间

基本命令

show dbs 显示数据库
use db 使用数据库
新建数据库:use dbtest 使用新的数据库 无数据时候show db 并不显示数据库
删除数据库:use dbtest db.drop.Database();
新建表 :db.table1.insert({“key”,“1”})
查看表: show tables
插入数据:采用类似json格式
db.test.insert({“name”:“xiaepifu”})

查询数据: db.tb1.find();

插入数据也可以将数据协成变量形式:
document=({title: ‘MongoDB 教程’,
description: xxx’,
by: ‘xx’,
url: “x”,
tags: [‘mongodb’, ‘database’, ‘NoSQL’],
likes: 100
})
document
{
“acknowledged” : true,
“insertedId” : ObjectId(“571a218011a82a1d94c02333”)
}
Mangager : 用于连接数据库
BulkWrite: 写进去对应的数据(“dataBase.TableName”,“array”)
Query: 用于查询数据 (“dataBase.TableName”,$query)

运行代码

<?php
/**
 * Created by PhpStorm.
 * User: xiapf
 * Date: 2018/9/28 0028
 * Time: 10:16
 * @property mongo_db $mongo_db
 */

class MongoDBTest extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        //  $this->load->library('mongo_db', '', 'mongo_db');
    }

    /**
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function save()
    {
        $con = new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');
        $db = $con->executeQuery();
        $query = new MongoDB\Driver\Query([]);
        $cursor = $con->executeQuery('test.person', $query);
        $it = new IteratorIterator($cursor);
        $it->rewind();
        while ($doc = $it->current()) {
            print_r($doc);
            $it->next();
            echo '<br/>';
        }
    }

    function insert()
    {
        $bulk = new MongoDB\Driver\BulkWrite;
        $document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => '菜鸟教程'];

        $_id = $bulk->insert($document);

        var_dump($_id);

    }

    function save1()
    {
        $bulk = new MongoDB\Driver\BulkWrite;
        $document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => '菜鸟教程'];
        $_id = $bulk->insert($document);
        var_dump($_id);
        $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
        $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
        $result = $manager->executeBulkWrite('test.runoob', $bulk, $writeConcern);
        var_dump($result);
    }

    /**
     * @throws \MongoDB\Driver\Exception\Exception
     */
    function test()
    {
        $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
        // 插入数据
        $bulk = new MongoDB\Driver\BulkWrite;
        $bulk->insert(['x' => 1, 'name' => 'runoob', 'url' => 'http://www.runoob.com']);
        $bulk->insert(['x' => 2, 'name' => 'Google', 'url' => 'http://www.google.com']);
        $bulk->insert(['x' => 3, 'name' => 'taobao', 'url' => 'http://www.taobao.com']);
        $manager->executeBulkWrite('test.sites', $bulk);

    }
    /**
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function search()
    {
        $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
        $filter = ['x' => ['$gt' => 1]];
        $options = [
            'projection' => ['_id' => 0],
            'sort' => ['x' => -1],
        ];
        // 查询数据
        $query = new MongoDB\Driver\Query($filter, $options);
        $cursor = $manager->executeQuery('test.sites', $query);
        foreach ($cursor as $document) {
            print_r(json_encode($document));
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值