Advertisement
Guest User

MongoDB Taxonomy Schema

a guest
May 16th, 2011
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement