Skip to main content

My OSCON 2012 Monday

Posted by haroldcarr on July 17, 2012 at 1:07 PM PDT

OSCON Monday July 16, 2012

live streaming


1 9:00am Building applications with MongoDB: An introduction

Steve Francia

slides: http://www.slideshare.net/spf13/oscon-2012

excerpts:

IDEAL

  • horizontal scaling
    • cloud compatible, commodity hardware
  • fast
  • easy development
    • data model, schema agility (N.B., : mean schema is your code!)

mongoDB

  • key/value good but
  • non-relational (no joins) makes horizontal scaling practical
  • document models are good
  • run anywhere virtualized: cloud, metal
  • written in C++
  • data serialized to BSON
  • extensive use of mem-mapped files (read-through/write-through memory caching)
  • speed: memcached > mongoDB > RDBS

document model

  • instead of separate tables, embed data inside docs they "belong" to
  • RDBS: index -> MRIs, XRays, Invoices, …
    • normalized data gives : efficiency, validity, …
  • Document: Patient (nested)

terminology


RDBMS Mongo
table/view collection
row document
index index
join embedded document
foreign key reference
partition shard

start with an object (or array, hash, dict, …)

  • no create table, no create collection
place1 = {
    name : "10gen HQ",
address : "dfdfd",
    city : "New York",
     zip : "10011",
    tags : [ "business", "awesome" ]
}

insert record

  • can't do SQL injection
db.places.insert(place1)

query

db.places.findOne({ zip : "10011", tags: "awesome"})
db.places.find({tags: "business"})

nested documents

  • array of objects
  • as deep as you want with consistent performance
{ _id : ObjectId("4c4ba5c...."),
    name : "10gen HQ",
address : "dfdfd",
    city : "New York",
     zip : "10011",
    tags : [ "business", "awesome" ]
comments : [ { author : "Fred",
               date   : "Sat ...",
               text   : "best ..."
             } ]
}

ObjectId

  • | timestamp | mac address of machine create on | process id | incremented value |

updates

  • mongodb atomic operator
    • $push : query/update
      • instead of query result to your app, then update (potential write conflict with other users)
    • $set, $unset, $rename, $push, $pop, $pull, $addToSet, $in
db.places.update(
  { name : "10gen HQ"},
  { $push :
    { comments :
      { author : "...",
          date : "...",
          text : "..."
      }}})

index nested documents

db.posts.ensureIndex({"comments.author":1})
db.posts.find({'comments.auther':'Fred'})

regular expressions

db.posts.find({'comments.author':/^Fr/})

index multiple values

db.post.ensureIndex({tags:1})

geospatial

  • $near, $within, …
db.posts.ensureIndex({"location": "2d"})
db.posts.find({"location":{$near:[22,42]}})

cursors

$cursor = $c->find(array("foo" => "bar"));
foreach ($cursor as $id => $value) {
    echo "$id: ";
    var_dump("value");
}

paging

page_num = 3;
results_per_page = 10;
db.collection.find().sort...

running mongoDB

cd /tmp
curl http://downloads.mongodb.org/osx/mongodb-osx-x86_64-x.y.z.tgz > mongo.tgz
tar -zxvf mongo.tgz

export PATH=/tmp/mongodb-osx-x86_64-2.0.6/bin:$PATH

mkdir  /tmp/mondodbdir
mongod --dbpath /tmp/mondodbdir &

curl -L http://j.mp/OSCONvenues | mongoimport -d milieu -c venues
mongo

use milieu
db.venues.count()
db.venues.findOne()
db.venues.ensureIndex({'location.geo':'2d'})
db.venues.getIndexes()

2 1:30pm Getting Started with OpenStack

Monty Taylor

slides: https://openstack-ci.github.com/publications/tutorial

left after 20 minutes of him messing with CLI …


3 1:30pm Scala Koans

… went here instead - but left at 2:15 when everyone was still trying to just get software installed …

… hallway conversations worked!