Modeling with MongoDB

Simple and flexible data modeling with MongoDB and Node.JS

Let's suppose

post  = {} // Document
posts = [] // Collection

// Querying
published = posts.map(function(o){
  return o.published
})
  

And, the best part is ...

{
  title    : 'Striking news'
  text     :  'Zerg on Tarsonis!'
  comments : {
    {text : "Can't believe it!"}
    {text : "Sad news ..."}
  }
}
  

Raw Data

post = {
  comments: []
}

comment = {}
  

How to use?

JS, Node.JS and Mongo Model

Why JS and Node.JS?



Not saying Node is pretty cool by itself.

Model or Raw Data?

 

What kind of pants are the best?

Do You need?

Model or Raw Data?

Model and Raw Data

CoffeeScript and Fibers are optional


You can use Model with plain old JavaScript and Callbacks

Model

class global.Post extends Model
  @collection 'posts'
  @embedded   'comments'

class global.Comment extends Model
  

CRUD

Dynamic schema, flexible queries.

CRUD

post = new Post
  text: 'Zerg on Tarsonis!'

post.comments.push new Comment
  text: "Can't believe it!"

post.save()
  

CRUD

Post.first(status: 'published')

Post.
  find(status: 'published').
  sort(createdAt: -1).
  limit(25).
  all()
  

CRUD with Scopes

class global.Post extends Model
  @latest: ->
    @find(status: 'published').
    @sort(createdAt: -1).
    @limit(25)

Post.latest().paginate(params).all()
  

CRUD

post = Post.first()

post.comments.push new Comment
  text: 'Sad news ...'

post.save()
  

CRUD with Modifiers

post = Post.first()
post.update $set:
  title: 'Striking news!'

Post.update {_id: postId}, $set:
  title: 'Striking news!'
  

CRUD

post.delete()

Post.delete status: 'draft'
  

Associations

1-to-1, 1-to-N, N-to-M

Associations

class global.Post extends Model
  comments: ->
    Comment.find postId: @_id

class global.Comment extends Model
  post: ->
    Post.first _id: @postId
  

Associations CRUD

post = Post.create
  text: 'Zerg found on Tarsonis!'

post.comments().create
  text: "Can't believe it!"
  

Associations CRUD

post = Post.first()

post.comments().limit(25).all()
post.comments().count()
  

Callbacks

before / after of create, update, validate and delete.

Callbacks

class global.Post extends Model
  @after 'delete', (callback) ->
    @comments().delete callback
  

Validations

Use errors to store messages, valid to check and validate to define rules.

Validations

class global.Post extends Model
  @validate (callback) ->
    unless @text
      @errors.add
        text: "can't be empty"
    callback()
  



Familiar? Takes some time

Try it

https://github.com/al6x/mongo-model

Use executable documentation to see it in action.

MongoDB

http://mongodb.org
Try it in Browser
 

Mongo Model

https://github.com/alexeypetrushin/mongo-model
Documentation
Examples
 

Me

Alexey Petrushin
alexey.petrushin at gmail
http://petrush.in/about/resume

Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome or Safari browser. Firefox 10 (to be released soon) will also handle it.