zoukankan      html  css  js  c++  java
  • MongoDB学习——持续更新

    參考MongoDB权威指南,学习阶段。大家多多交流问题。持续更新本文

    MongoDB的长处
    MongoDB具有丰富的数据模型,是面向文档的数据库。

    easy扩展。能够在多台server之间切割数据。

    开发人员仅仅需专注于编写应用。假设须要更大的数据。仅仅需在集群中加入新机器,然后让数据库来处理剩下的事情。

    具有丰富的功能,比方索引,存储JavaScript,聚合,固定集合。文件存储。

    不支持联接(join)和复杂的多行事物。

    卓越的性能是MongoDB的主要目标,默认的存储引擎使用了内存映射文件。将内存管理工作交给操作系统。

    简便的管理,MongoDB尽量让server自己管理数据库。除了启动数据库server之外。差点儿没有什么必要的管理操作。

    假设主server挂掉,会自己主动切换到备server上。

    MongoDB的下载和安装
    首先去MongoDB官网,依据系统型号选择要下载的版本号。这里以centos64位为例
        #下载
    
        > wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.4.tgz
        > tar -zxvf mongodb-linux-x86_64-3.0.4.tgz
        > mv mongodb-linux-x86_64-3.0.4 /usr/local/mongodb
        > cd /usr/local/mongodb
        > mkdir db
        > mkdir logs
    
        > vim bin/mongodb.conf
    
        #加入下面内容
    
        dbpath=/usr/local/mongodb/db
        logpath=/usr/local/mongodb/logs/mongodb.log
        port=27017
        fork=true
    
        #又一次绑定mongodb的配置文件地址和訪问IP
    
        > /usr/local/mongodb/bin/mongod --bind_ip localhost -f /usr/local/mongodb/bin/mongodb.conf
    
        #启动MongoDB
        > /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/bin/mongodb.conf
    
        #測试是否开启成功进入mongodb的shell模式
        > /usr/local/mongodb/bin/mongo
        > show dbs 
        #没有异常的话安装启动成功了。
    MongoDB的基本概念

    文档是MongoDB中数据的基本单元,很相似关系型数据库中的行。

    相似地。集合能够看做是没有模式的表。

    MongoDB的单个实例能够容纳多个独立的数据库。每个都有自己的集合和权限。

    MongoDB自带间接可是功能强大的JavaScript shell,这个工具对管理MongoDB实例和操作数据作用很大。

    每个文档都有一个特殊的键”_id”,它在文档所处的集合中是唯一的。

    MongoDB shell操作
    执行shell
        #进入shell模式
        > /usr/local/mongodb/bin/mongo
    
        #执行简单的计算
        > x=200
        200
        > x/5
        40
    
        #还可充分利用JavaScript的标准库
        > Math.sin(Math.PI / 2)
        1
        > "Hello World!".replace("World","MongoDB")
        Hello MongoDB!
    
        #shell (CRUD)
        #创建tets数据库
        > use test;
        > db
        test
    
        #创建一个用户
        > user={"name":"zhangsan","age":18};
        { "name" : "zhangsan", "age" : 18 }
        > db.users.insert(user);
        WriteResult({ "nInserted" : 1 })
    
        #查询
        #find会返回集合里的全部记录,在shell中最多显示20条数据,findOne会显示一条数据
        > db.users.findOne();
        {
        "_id" : ObjectId("55b59ee00882afbc7416fed4"),
        "name" : "zhangsan",
        "age" : 18
        }
    
        #更新
        #改动name=zhangsan用户的年龄为20,加入个性别sex=1
        > user.sex=1
        1
        > user.age=20
        20
        > db.users.update({"name":"zhangsan"},user);
        WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
        > db.users.findOne();
        {
        "_id" : ObjectId("55b59ee00882afbc7416fed4"),
        "name" : "zhangsan",
        "age" : 20,
        "sex" : 1
        }
    
        #删除
        > db.users.remove({"name":"zhangsan"});
        WriteResult({ "nRemoved" : 1 })
    
        #shell帮助文档
        > help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce
    
        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell
    
        #了解函数源码。比方update方法。输入的时候不带括号
        > db.foo.update
    

  • 相关阅读:
    团队冲刺第五天
    团队冲刺第四天
    学习进度条---第八周
    团队冲刺第三天
    团队冲刺第二天
    团队冲刺第一天
    学习进度条--第七周
    课堂练习--第7周(两人结对)
    学习进度条--第六周
    第一次 在Java课上的编程
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7276944.html
Copyright © 2011-2022 走看看