Guest

MongoDB Taxonomy Schema

By: a guest on May 16th, 2011  |  syntax: JavaScript  |  size: 1.05 KB  |  hits: 361  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. db.article.insert({title:'wealth news article', taxonomy:[{key:'section', term:'News', slug:'news'}, {key:'topics', term:'Wealth', slug:'wealth'}]})
  2. db.article.insert({title:'retirement news article', taxonomy:[{key:'section', term:'News', slug:'news'}, {key:'topics', term:'Retirement', slug:'retirement'}]})
  3.  
  4. db.article.insert({title:'wealth blog article', taxonomy:[{key:'section', term:'Blogs', slug:'blogs'}, {key:'topics', term:'Wealth', slug:'wealth'}]})
  5. db.article.insert({title:'retirement blog article', taxonomy:[{key:'section', term:'Blogs', slug:'blogs'}, {key:'topics', term:'Retirement', slug:'retirement'}]})
  6.  
  7. db.article.ensureIndex({'taxonomy.key':1,'taxonomy.term':1})
  8. db.article.ensureIndex({'taxonomy.key':1,'taxonomy.slug':1})
  9.  
  10. db.article.find({taxonomy:{$elemMatch:{'key':'section', 'term':'News'}}}).explain()
  11. db.article.find({taxonomy:{$elemMatch:{'key':'section', 'slug':'news'}}}).explain()
  12. db.article.find({taxonomy:{$all:[{$elemMatch:{'key':'section', 'slug':'news'}},{$elemMatch:{'key':'topics', 'slug':'retirement'}}]}}).explain()