mongodb-introduction

39
MongoDB Introduction 2012/02/16 Tse- Ching Ho 何澤清

description

NoSQL Taiwan #1 Talk

Transcript of mongodb-introduction

Page 1: mongodb-introduction

MongoDB Introduction

2012/02/16Tse-Ching Ho 何澤清

Page 2: mongodb-introduction

About meRuby Developer

2011: 開始使用 NoSQL 工作。

2011.9-present:陽明大學生物資訊所

資深工程師

Twitter: tsechingho

Skype: tsechingho1978

GitHub: https://github.com/tsechingho

Page 4: mongodb-introduction

What & Why

Page 5: mongodb-introduction

Size matters

Page 6: mongodb-introduction

Type matters

Page 7: mongodb-introduction

Place matters

Page 8: mongodb-introduction

Time matters

Page 9: mongodb-introduction

Money matters

Page 10: mongodb-introduction

Person matters

Easy learn, Easy use, Easy design

Quick, Agile

Flexibility

Nice APIs are best &iends

Page 11: mongodb-introduction

Specification

Page 12: mongodb-introduction

MongoDB vs RDBMS

Page 13: mongodb-introduction

FeaturesWritten in: C++

Main point: Retains some &iendly properties of SQL. (Query, index)

License: AGPL (Drivers: Apache)

Protocol: Custom, binary (BSON)

Master/slave replication (auto failover with replica sets)

Sharding built-in

Queries are javascript expressions

Run arbitrary javascript functions server-side

Better update-in-place than CouchDB

Uses memory mapped files for data storage

Performance over features

Journaling (with --journal) is best turned on

On 32bit systems, limited to ~2.5Gb

An empty database takes up 192Mb

GridFS to store big data + metadata (not actua#y an FS)

Has geospatial indexing

Page 14: mongodb-introduction

Key PointsDocument based, Co#ection driven

Embeded, Referenced

Schema &ee

Distributed

Durability

Eventual consistency

Replication

Sharding

pros: easy addition

cons: fault tolerance, migration tolerance

Page 15: mongodb-introduction

Install

Page 16: mongodb-introduction

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

brew insta# mongodb

cp /usr/local/Ce#ar/mongodb/2.0.2-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents/

launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist

Mac

Page 17: mongodb-introduction

Debianecho "deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen" > /etc/apt/sources.list.d/10gen.list

apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

aptitude update

aptitude insta# mongodb-10gen

Page 19: mongodb-introduction

Configuration

vim $(brew --prefix mongodb)/mongod.confvim /etc/mongodb.conf

dbpath = /usr/local/var/mongodblogpath = /usr/local/var/log/mongodbbind_ip = 127.0.0.1

Page 20: mongodb-introduction

Syntax

Page 21: mongodb-introduction

BSON{ "_id" : ObjectId("4d3ed089#60ab534684b7e9"), "title" : "Sir", "name" : { "_id" : ObjectId("4d3ed089#60ab534684b7ff"), "first_name" : "Durran" }, "addresses" : [ { "_id" : ObjectId("4d3ed089#60ab534684b7e0"), "city" : "Berlin", "country" : "Deutschland" } ]}

Page 22: mongodb-introduction

Database Manipulationmongo> help> show dbs> use demo> db.things> db.things.help()> db.things.save({name: 'NoSQL Taiwan'})

> db.things.count()> db.things.dataSize()> db.things.find()> db.things.remove()> db.dropDatabase()

Page 23: mongodb-introduction

CRUDCreate Co#ection> db.things.insert({db: 'mongodb', type:'native'})

Retrieve Co#ection> db.things.find({db: 'mongodb'})> db.things.find({db: 'mongodb'}, {type: false})

Update Co#ection> db.things.update({db: 'mongodb'}, {db: 'redis'})> db.things.update({db: 'redis'}, {$set: {type: 'copy'}})

Delete Co#ection> db.things.remove({db: 'redis'})

Page 24: mongodb-introduction

QueryGeneral find

db.things.find( { x : 3, y : "foo" } );db.things.find({j: {$ne: 3}, k: {$gt: 10} });

Regular Expressionsdb.customers.find( { name : { $regex : 'acme.*corp', $options: 'i' } } );

Conditional Operators<, <=, >, >=$a#$exists$mod$ne$in$nin$nor$or$and$size$type

Page 25: mongodb-introduction

ELSEIndex

Map / Reduce

Transaction / ACID

Page 26: mongodb-introduction

Application

Page 27: mongodb-introduction

Replica Sets

asynchronous replication

distributing read load

automatic recovery

automatic failover

auto election of primary

primary for writes replacement of master-slave

Page 28: mongodb-introduction

Demo Code

https://github.com/tsechingho/mongodb-tutorial

Page 29: mongodb-introduction

ShardingBalancing

Failover

Scaling model

Shards (Replica Sets)

Config Server

Mongos (Router, Routing Processes)

Page 30: mongodb-introduction

Sharding Components

Page 31: mongodb-introduction

Servers layout

Page 32: mongodb-introduction

Sharding Architecture

Page 33: mongodb-introduction

Usage CasesSma# nested data set

Comments, Tweets, SMS

Manipulation log

Time careless

CMS

Reproducible data set

API fetched data

Analysis data

Page 34: mongodb-introduction

Tools

Page 37: mongodb-introduction

Thought

Page 38: mongodb-introduction

References