A short introduction to MongoDB
             Russell Smith
/usr/bin/whoami
•   Russell Smith

•   Consultant for UKD1 Limited

•   Specialising in helping companies going through rapid growth

•   Help with code, architecture, infrastructure, devops, sysops, capacity
    planning, etc

•   <3 gearman, mongodb, neo4j, mysql, kohana, riaksearch, php, debian
What is MongoDB


•   A scalable, high-performance, open source, document-oriented
    database.

•   Stores JSON like documents

•   Indexible on any attributes (like MySQL)
Getting started...


•   Try out the interactive shell at www.mongodb.org (then click try!)

•   Download and install - http://www.mongodb.org/downloads
Basics
•   Common things you do in MySQL or another RDBMS;

•   Insert

•   Select (aka find)

•   Update

•   Delete (aka remove)

•   Index
Create


•   Creating ‘tables’, or in MongoDB terms collections, is not usually
    necessary

•   Ditto for databases
Insert


•   db.test.insert({hello: ‘world’});

•   This insert the document {hello: ‘world’} into the test collection
Let’s check it

•   In MongoDB we find documents rather than SELECTing them...

•   db.test.find()

•   this is the equivalent of SELECT * FROM test;

•   it will return the document we previously inserted
Updates


•   Let’s update the document to say from Russell

•   db.test.update(<documents which match>, <set to x>)

•   db.test.update({hello: ‘world’}, {hello: ‘from Russell’})
Delete?


•   db.test.remove(<where documents match>)

•   Lets remove the last document

•   db.test.remove({hello: ‘from Russell’})
Drop


•   If you no longer want an entire collection, you can drop it which
    removes all the data and indexes;

•   db.test.drop()
Indexes

•   For performance it’s usually a good idea to index your collections.

•   How and which columns depends on what queries you are doing - you
    can get help with this by using ‘explain’ much like in MySQL.

•   db.test.ensureIndex({hello:1})
Further reading
•   I’ve only brushed on the details, but this should be enough to get you
    interested / started with MongoDB. Some of the missing stuff;

•   Updates can also push, set, increment, decrement, etc - http://bit.ly/
    gEfKOr

•   Indexes can be across multiple keys, 2D (geo) and asc / desc - http://
    bit.ly/hpK68Q

•   SQL -> Mongo chart - http://bit.ly/ig1Yfj

London MongoDB User Group April 2011

  • 1.
    A short introductionto MongoDB Russell Smith
  • 2.
    /usr/bin/whoami • Russell Smith • Consultant for UKD1 Limited • Specialising in helping companies going through rapid growth • Help with code, architecture, infrastructure, devops, sysops, capacity planning, etc • <3 gearman, mongodb, neo4j, mysql, kohana, riaksearch, php, debian
  • 3.
    What is MongoDB • A scalable, high-performance, open source, document-oriented database. • Stores JSON like documents • Indexible on any attributes (like MySQL)
  • 4.
    Getting started... • Try out the interactive shell at www.mongodb.org (then click try!) • Download and install - http://www.mongodb.org/downloads
  • 5.
    Basics • Common things you do in MySQL or another RDBMS; • Insert • Select (aka find) • Update • Delete (aka remove) • Index
  • 6.
    Create • Creating ‘tables’, or in MongoDB terms collections, is not usually necessary • Ditto for databases
  • 7.
    Insert • db.test.insert({hello: ‘world’}); • This insert the document {hello: ‘world’} into the test collection
  • 8.
    Let’s check it • In MongoDB we find documents rather than SELECTing them... • db.test.find() • this is the equivalent of SELECT * FROM test; • it will return the document we previously inserted
  • 9.
    Updates • Let’s update the document to say from Russell • db.test.update(<documents which match>, <set to x>) • db.test.update({hello: ‘world’}, {hello: ‘from Russell’})
  • 10.
    Delete? • db.test.remove(<where documents match>) • Lets remove the last document • db.test.remove({hello: ‘from Russell’})
  • 11.
    Drop • If you no longer want an entire collection, you can drop it which removes all the data and indexes; • db.test.drop()
  • 12.
    Indexes • For performance it’s usually a good idea to index your collections. • How and which columns depends on what queries you are doing - you can get help with this by using ‘explain’ much like in MySQL. • db.test.ensureIndex({hello:1})
  • 13.
    Further reading • I’ve only brushed on the details, but this should be enough to get you interested / started with MongoDB. Some of the missing stuff; • Updates can also push, set, increment, decrement, etc - http://bit.ly/ gEfKOr • Indexes can be across multiple keys, 2D (geo) and asc / desc - http:// bit.ly/hpK68Q • SQL -> Mongo chart - http://bit.ly/ig1Yfj

Editor's Notes