mongodb-introduction

Post on 12-May-2015

2.688 views 1 download

description

NoSQL Taiwan #1 Talk

Transcript of mongodb-introduction

MongoDB Introduction

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

About meRuby Developer

2011: 開始使用 NoSQL 工作。

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

資深工程師

Twitter: tsechingho

Skype: tsechingho1978

GitHub: https://github.com/tsechingho

What & Why

Size matters

Type matters

Place matters

Time matters

Money matters

Person matters

Easy learn, Easy use, Easy design

Quick, Agile

Flexibility

Nice APIs are best &iends

Specification

MongoDB vs RDBMS

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

Key PointsDocument based, Co#ection driven

Embeded, Referenced

Schema &ee

Distributed

Durability

Eventual consistency

Replication

Sharding

pros: easy addition

cons: fault tolerance, migration tolerance

Install

/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

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

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

Syntax

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

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()

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'})

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

ELSEIndex

Map / Reduce

Transaction / ACID

Application

Replica Sets

asynchronous replication

distributing read load

automatic recovery

automatic failover

auto election of primary

primary for writes replacement of master-slave

Demo Code

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

ShardingBalancing

Failover

Scaling model

Shards (Replica Sets)

Config Server

Mongos (Router, Routing Processes)

Sharding Components

Servers layout

Sharding Architecture

Usage CasesSma# nested data set

Comments, Tweets, SMS

Manipulation log

Time careless

CMS

Reproducible data set

API fetched data

Analysis data

Tools

Thought

References