mongoDB简介

 

1 页:

 

MongoDB Coming

2 页:大纲

大纲

简介

Why Mongo

Mongo 特性

Mongo 用法

Mongo 架构

MysqlMongo

Mongo java

Java DSL

3 页:简介

简介

MongoDB不是在实验室设计出来的。利用自己开发大型,高可用性和健壮性系统的经验,我们开发了MongoDB。我们并不是从零开始,而是发现 哪些地方有问题,然后尝试解决它。所以在我眼中,MongoDB让你从使用MySQL的基于关系的数据模型转变为基于文档的模型,从而获得如快速的嵌入式 文档,易管理,使用无模式数据库进行敏捷开发,易于横向扩展和伸缩(因为join不再重要)等大量特性。使用关系型数据时有很多东西工作的很好,比如索 引,动态查询和动态更新等,这些在MongoDB中也没怎么变化。比如,在MongoDB里设计索引应该和你在MySQLOracle里一样,你有选择 在一个内嵌的域上建索引的能力。

    – Eliot Horowitz, 10gen公司首席技术官和联合创始人

4 页:Why Mongo

Why Mongo

面向文档

文档(对象)和编程语言的数据类型很好的对应

嵌入式文档和数组减少了join的必要

动态类型(无schema)使模式演变非常容易

没有join和多文档事务从而获得高性能和易伸缩性

高性能

没有join和事务使得读写操作很快

可以索引嵌入式文档和数组

可选的异步写操作

高可用性

复制服务器自动和主节点故障转移

易伸缩

最终一致性读操作分布到复制服务器上

自动分片(数据跨服务器自动分区)

读写操作分布在不同的分片上

没有join和事物使得分布式查询简单而高效

富查询语言

存储的JavaScript

Aggregation:支持Map Reduce模型

限长集合

大文件,大数据量存储引擎

管理简单

5 页:Who Use

Who Use

http://www.mongodb.org/display/DOCS/Production+Deployments

6 页:Mongo 用法—文档

Mongo 用法文档

文档是MongoDB的基本存储单元

BSON

{"greeting" : "Hello, world!", "greeting" : "Hello, MongoDB!"}

每个文档都会有一个特殊的key, "_id",

7 页:Mongo 用法—Collection

Mongo 用法—Collection

集合可以看成是关系数据库的表,但是是没有schema限制的。

    {"greeting" : "Hello, world!"}

    {"foo" : 5}

命名:UTF-8,不能是“”,不能含有\0$system打头的集合是系统集合。如blog.posts

那我们只需要一个集合就够了吗?不够

程序开发起来很复杂

速度慢:会扫描很多无关的document

索引

使用时也需要按业务划分

8 页:Subcollections

Subcollections

blog.postsblog.authors,两者没关系,并且和blog也没关系

便于组织管理,Web Console

9 页:Database

Database

MongoDB多数据库

数据库用来归类Collection

Database是相互独立的,独立的文件存储

命名:Collection规则,小写,最长64字节

10 页:Mongo Shell

Mongo Shell

JavaScript shellshell执行命令的地方

插入:db.foo.insert({"bar" : "baz"})

批量插入:性能

删除:db.mailing.list.remove({"opt-out" : true})

更新: var joe = db.users.findOne({"name" : "joe"});

     joe.relationships = {"friends" : joe.friends, "enemies" : joe.enemies};

    {

    "friends" : 32,

    "enemies" : 2

     }

    db.users.update({"name" : "joe"}, joe)

 

Upsert

查询:db.people.find()

    db.people.findOne({"name" : "joe", "age" : 20});

11 页:数据类型

数据类型

Null{"x" : null}

Boolean{"x" : true}

32-bit Integer

64-bit 浮点数:{"x" : 3.14}

String

object id12-byte ID

Datemilliseconds,无TimeZone javaString

regular expression{"x" : /foobar/i}

Codejavascript code {"x" : function() { /* ... */ }}

binary data String bytes

maximum value

minimum value

Undefined{"x" : undefined}

Array{"x" : ["a", "b", "c"]}

embedded document{"x" : {"foo" : "bar"}}

12 页:Modifiers(原子操作)

Modifiers(原子操作)

$set{ $set : { field : value } }

$unset :删除,{ $unset : { field : 1} }

$inc { $inc : { field : value } }

$push

$pushAll { $pushAll : { field : value_array } }

$pull{ $pull : { field : _value } }

$pullAll

$addToSet:不存在则添加。

$pop:删除数组的第一个或最后一个元素。{ $pop : { field : 1 } }

$rename{ $rename : { old_field_name : new_field_name } }

$bit – 位操作,integer类型。{$bit : { field : {and : 5}}}

$ 偏移操作符:

> t.find() { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ] }

> t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true )

> t.find() { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC", "comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" : 7 } ] }

13 页:索引

索引

创建:

db.status.ensureIndex({user : 1, date : -1})

Unique

db.people.ensureIndex({"username" : 1}, {"unique" : true})

Compound Indexes

db.things.ensureIndex({j:1, name:-1});

db.foo.find().explain()

db.people.ensureIndex({“username” : 1}, {“background” : true})backgroud:非阻塞,后台创建

db.collection.dropIndex({x: 1, y: -1})

14 页:Query language

Query language

查询条件

<, <=, >, >=

$all

$exists

$mod

$ne

$in

$nin

$nor

$or

$size

$type

$elemMatch

正则

$not

Cursor方法

count()

limit()

skip()

snapshot()

sort()

Group()

例子:

db.users.find( { x : 3, y : "abc" } ).sort({x:1});

db.users.find({"age" : {"$gte" : 18, "$lte" : 30}})

15 页:Mongo 架构

Mongo 架构

复制:master-slavereplica set,自动恢复

和主库最一致的从库会自动替代成为主库,这都是简单的配置就可做到的。




 

 

 

16 页:Mongo 架构

Mongo 架构

分区:Autoshard,加减机器如此简单,再也不蛋疼。

Mongos:路由程序

Mongod:服务器端程序

 


 

 


17 页:备份

备份

热备份:

mongodump mongorestore

Fsync

Slave Backups

Repair:修复,同时会压缩数据

18 页:监控

监控

Web admin

Mongostat

ServerStatus command

19 页:安全

安全

用户名,密码

用户分为普通用户,管理员

IP限制

20 页:MysqlMongo

MysqlMongo

Mysql分表

Mysql分库

Mysql schema

Mysql master-slave 主从切换

迁移方法:用jackson mapperjsonDB查询成Map

21 页:Mongo java

Mongo java

连接

Mongo m = new Mongo( "localhost" , 27017 );

DB db = m.getDB( "mydb" );

获取集合

DBCollection coll = db.getCollection("testCollection")

查询

query = new BasicDBObject();

query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30

 cur = coll.find(query);

while(cur.hasNext()) { System.out.println(cur.next()); }

Insert

BasicDBObject doc = new BasicDBObject();

doc.put("name", "MongoDB");

doc.put("type", "database");

doc.put("count", 1);

BasicDBObject info = new BasicDBObject(); I

nfo.put("x", 203); info.put("y", 102);

doc.put("info", info);

coll.insert(doc);

22 页:Mongo USE CASES

Mongo USE CASES

Blog

UGC 数据

GridFS存储图片和缩略图

23 页:体会

体会

爱内存

爱索引

爱原子操作

批量插入,异步写入

Java 驱动限制,需要很好的封装

开发效率低

性能貌似不错

24 页:Java DSL

Java dsl

Group

Shard环境不能使用,需要用map reduce模型替代

db.coll.group( {key: { a:true, b:true }, cond: { active:1 }, reduce: function(obj,prev) { prev.csum += obj.c; }, initial: { csum: 0 } });

key: Fields to group by.

reduce

keyf

cond

finalize:

 

java程序员写起来超级崩溃。

25 页:Group 例子

Group 例子

db.test.group(

{ cond: {"invoked_at.d": {$gte: "2009-11", $lt: "2009-12"}} ,

key: {http_action: true} ,

initial: {count: 0, total_time:0} ,

 reduce: function(doc, out){ out.count++; out.total_time+=doc.response_time } ,

 finalize: function(out){ out.avg_time = out.total_time / out.count } } );

26 页:查询框架

Java驱动封装,面向对象的DSL查询框架。不用java开发人员写js代码。

 

public enum Criteria {

    //select 使用PropertyCriteria作为查询条件

    select(CriteriaType.select),

    unselect(CriteriaType.select),

    //group by 使用PropertyCriteria作为查询条件

    groupBy(CriteriaType.groupBy),

    //order by 使用PropertyCriteria作为查询条件

    asc(CriteriaType.orderBy),

    desc(CriteriaType.orderBy),

    //where 使用CriteriaValue作为查询条件

    gt(CriteriaType.where),

    lt(CriteriaType.where),

    eq(CriteriaType.where),

    ne(CriteriaType.where),

    gte(CriteriaType.where),

    lte(CriteriaType.where),

    in(CriteriaType.where),

    //组函数 使用PropertyCriteriaCriteriaValue作为查询条件

   //(当作使用CriteriaValue时,带的值为函数返回值的keynamePropertyCriteriakeynamepropname_functionname

    max(CriteriaType.groupFunction),

    min(CriteriaType.groupFunction),

    /**

     * 暂时不支持,可以用sum除以count代替

     */

    avg(CriteriaType.groupFunction),

    sum(CriteriaType.groupFunction),

    count(CriteriaType.groupFunction),

    //多列组函数 使用PropertyCriteriaCriteriaValue作为查询条件

    //类似groupFunction,但是prop的值是用逗号多个字段名称的stringCriteriaValue的值也是返回值key的逗号拼接

    /**

     * 取最大值,并带出最大值时的其他属性

     */

    maxRow(CriteriaType.multiGroupFunction),

    /**

     * 取最小值,并带出当最小值时的其他属性

     */

    minRow(CriteriaType.multiGroupFunction),

    ;

    public final CriteriaType type;

   

    private Criteria(CriteriaType type) {

        this.type = type;

    }

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值