Modeling with MongoDB
Simple and flexible data modeling with MongoDB and Node.JS
Simple and flexible data modeling with MongoDB and Node.JS
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 ..."} } }
post = { comments: [] } comment = {}
JS, Node.JS and Mongo Model
Not saying Node is pretty cool by itself.
What kind of pants are the best?
Model or Raw Data?
CoffeeScript and Fibers are optional
You can use Model with plain old JavaScript and Callbacks
class global.Post extends Model @collection 'posts' @embedded 'comments' class global.Comment extends Model
Dynamic schema, flexible queries.
post = new Post text: 'Zerg on Tarsonis!' post.comments.push new Comment text: "Can't believe it!" post.save()
Post.first(status: 'published') Post. find(status: 'published'). sort(createdAt: -1). limit(25). all()
class global.Post extends Model @latest: -> @find(status: 'published'). @sort(createdAt: -1). @limit(25) Post.latest().paginate(params).all()
post = Post.first() post.comments.push new Comment text: 'Sad news ...' post.save()
post = Post.first() post.update $set: title: 'Striking news!' Post.update {_id: postId}, $set: title: 'Striking news!'
post.delete() Post.delete status: 'draft'
1-to-1, 1-to-N, N-to-M
class global.Post extends Model comments: -> Comment.find postId: @_id class global.Comment extends Model post: -> Post.first _id: @postId
post = Post.create text: 'Zerg found on Tarsonis!' post.comments().create text: "Can't believe it!"
post = Post.first() post.comments().limit(25).all() post.comments().count()
before / after of create, update, validate and delete.
class global.Post extends Model @after 'delete', (callback) -> @comments().delete callback
Use errors to store messages, valid to check and validate to define rules.
class global.Post extends Model @validate (callback) -> unless @text @errors.add text: "can't be empty" callback()
Familiar? Takes some time
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.