mongodb简介

1.面向文档的存储引擎,可以支持结构化数据
2.全面索引支持,可以任意属性上建立索引(只需索引,其他用集群完成)
3.数据库本身内置的复制与高可用
4.数据库本身支持的自动化分片集群
5.丰富的文档的查询功能
6.原子化操作
7.支持map/reduce
8.gridfs
修改器
非结构化数据
不能确定表列结构的数据
文档
{“foo”:3,"greeting":"hello,world"}
数值和字符串
{“foo”:3}
{“foo”:"3"}
{“Foo”:3}
{“foo”:3}
key不能重复
{"greeting":"hello world","greeting":"hello,mongodb"}

安装
 vi /etc/yum.repos.d/10gen.repo
 [10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0

[root@localhost ~]# yum info mongo-10gen-server
Loaded plugins: fastestmirror, refresh-packagekit, security
10gen                                                    |  951 B     00:00     
10gen/primary                                            |  44 kB     00:13     
10gen                                                                   274/274
base                                                     | 3.7 kB     00:00     
base/primary_db                                          | 4.6 MB     00:29     
extras                                                   | 3.4 kB     00:00     
extras/primary_db                                        |  34 kB     00:00     
updates                                                  | 3.4 kB     00:00     
updates/primary_db                                       | 3.3 MB     00:16     
Available Packages
Name        : mongo-10gen-server
Arch        : x86_64
Version     : 2.4.14
Release     : mongodb_1
Size        : 12 M
Repo        : 10gen
Summary     : mongo server, sharding server, and support scripts
URL         : http://www.mongodb.org
License     : AGPL 3.0
Description : Mongo (from "huMONGOus") is a schema-free document-oriented
            : database.
            :
            : This package provides the mongo server software, mongo sharding
            : server softwware, default configuration files, and init.d scripts.
[root@localhost ~]# yum install mongo-10gen-server
[root@localhost ~]# cat /etc/mongod.conf
[root@localhost ~]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2693
child process started successfully, parent exiti启动比较慢
 yum install mongo-10gen
[root@localhost Desktop]# mongo
MongoDB shell version: 2.6.11
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group

> x=200
200
> x/5
40

> function fact(n){
... if(n<=1) return 1;
... return n*fact(n-1);
... }
> fact(5);
120
切换use
创建
post={"title":"myblogpost","content":"hereis my blog post","date":new Date()}
db.blog.insert(post)
db.blog.find();
读数据
> db.blog.findOne();
{
    "_id" : ObjectId("56a206636efa22a91f284356"),
    "title" : "myblogpost",
    "content" : "hereis my blog post",
    "date" : ISODate("2016-01-22T10:37:10.890Z")
}
修改
> post.comment=[]
[ ]
> db.blog.update({title:"Myblog post"},post)
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.blog.find()
{ "_id" : ObjectId("56a206636efa22a91f284356"), "title" : "myblogpost", "content" : "hereis my blog post", "date" : ISODate("2016-01-22T10:37:10.890Z") }
>
> 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
shell支持的数据类型
空值,布尔,64浮点,字符串,对象id,日期,正则表达,代码,数组,内嵌文档,未定义类型
insert文档
x=[{"name";“hello”},{"name":"123"}]
db.blog.insert(x)
插入时检查mongodb会检查是否包含_id,如果文档没有制定_id,mongodb会为其创建
对于多个文档,最好批量操作
优点
更少的连接数
更少的信息头检测
对带插入的灵活控制
默认情况下,插入文档时mongodb仅检查是否包含_id和大小是否超过16m
缺点可能插入无效数据
monggodb插入时不执行代码,所以不会注入攻击
删除
db.foo.remove();删除集合
db.foo.remove({"bar":"baz"});删除指定记录
db.drop_collection(‘foo’)删除集合,记录过多的时候
upsert模式
db。users.update({“name”:"joe"},joe,true)
multi模式
默认情况下update 只会更新第一个匹配到的文档,开启multi模式,才会跟新所有匹配到的文档
指定第四个参数为true可以开启multi模式
db.users.update({"name":"joe",joe,true,true})



区分大小写
$inc增加或者减少数字的值,键不存在时会自动创建     数字
$set 设置键的值,键不存在时自动创建
$push 将元素追加到数组末尾,数组不存在时自动创建   数组
$pushAll $push的批量操作                           数组
$addToSet 同$pushAll,但会自动过滤重复元素         数组
$pop $pop :{key:1} -- 从数组末尾移除元素           数组
     $pop :{key:1} -- 从数组开头移除元素           数组
$pull 从数组中移除所有匹配的元素                   数组
$pullAll  $pull 的批量操作版本                     数组
$rename  修改指定键的键名                          键
$bit   对整形键值执行位操作与,或等                数字
db.hits.insert({"url":"www.datagru.cn","pv":102});
db.hits.insert({"url":"f.datagru.cn","pv":108});
db.hits.insert({"url":"www.itpub","pv":155});
db.hits.find();

你可能感兴趣的:(mongodb简介)