Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Created January 21, 2011 17:50
Show Gist options
  • Save wilmoore/790074 to your computer and use it in GitHub Desktop.
Save wilmoore/790074 to your computer and use it in GitHub Desktop.
Schema Design
Schema Design
> db.posts.find()
post = {
author: "Herge"
date: new Date(),
text: "Destination Moon",
tags: ['comic", 'adventure"]}
Notes
* ID must be unique, but can be anything you'd like...
mongo will gen a default ID if not specified?
* Composite keys are OK
Secondary index for "author"
// 1 means ascending, -1 means descending
> db.posts.ensureIndex({author: 1})
> db.posts.find({author: 'Herge'})
// examine the query plan
> db.blogs.find({author: 'Herge'}).explain()
Notes
* Check "cursor" type -- if BtreeCursor then you are using
an index, otherwise (BasicCursor), you are running a full table scan.
Query Operators
Conditional Operators:
$ne, $in, $nin, $mod, $all, $size, $exists, $type
$lt, $lte, $gt, $gte, $ne
Regular expressions:
// posts where author starts with h
> db.posts.find({ author: /^h/i })
Extending the Schema
new_comment = {author: "kyle", date: new Date(), text: "great book"}
Map Reduce (batch processing, aggregation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment