0
Map/Reduce, geospatial search, and other Cool                     Features                                    Richard M Kr...
Things I’ll cover          Array tricks          The findAndModify command   Map/Reduce, geospatial search, and other Cool ...
Array tricks   Suppose your collection looked like this:   >   db . a r r a y s . f i n d ( )   {   ” i d ” : 1 , ” t a g ...
Array tricks, continued   ....then consider the following queries:   db .   arrays       .   f i n d ({ t a g s :    ’a ’}...
Array tricks, continued continued   As of v1.5.1 (and so v1.6) you can project slices of arrays with the   $slice operator...
Array tricks, continued continued continued   And you can also update portions of documents matched by the   update’s sele...
findAndModify  db . c o u n t e r s . s a v e ( { i d : ’ some i d ’ , v a l u e : 0 } ) ;  function get (){         r e t ...
findAndModify, continued   db . r e s o u r c e s . s a v e (     { f r e e : 90 ,          buckets : [ { use : 10 , c l i ...
Upcoming SlideShare
Loading in...5
×

Mongophilly cool-features-2011-04-26

317

Published on

Cool Features at MongoPhilly.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
317
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Mongophilly cool-features-2011-04-26"

  1. 1. Map/Reduce, geospatial search, and other Cool Features Richard M Kreuter 10gen Inc. richard@10gen.com April 26, 2011Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
  2. 2. Things I’ll cover Array tricks The findAndModify command Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
  3. 3. Array tricks Suppose your collection looked like this: > db . a r r a y s . f i n d ( ) { ” i d ” : 1 , ” t a g s ” : [ ” a ” , ”b ” , ” c ” ] } { ” i d ” : 2 , ” t a g s ” : [ ”b ” , ” c ” , ”d” ] } { ” i d ” : 3 , ” t a g s ” : [ ” c ” , ”d ” , ” e ” ] } Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
  4. 4. Array tricks, continued ....then consider the following queries: db . arrays . f i n d ({ t a g s : ’a ’}) // matches 1 db . arrays . f i n d ({ t a g s : ’c ’}) // matches 1 , 2 , 3 db . arrays . f i n d ({ t a g s : { $in : [ ’ a ’ , ’ e ’ ] } } ) // 1 and 3 db . arrays . f i n d ({ t a g s : {$all : [ ’ a ’ , ’ e ’ ] } } ) // no ma db . arrays . f i n d ({ t a g s : {$all : [ ’ c ’ , ’ d ’ ] } } ) // match Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
  5. 5. Array tricks, continued continued As of v1.5.1 (and so v1.6) you can project slices of arrays with the $slice operator // Will return { id :1 , tags : [” a ”]} db . a r r a y s . f i n d ({ id :1} , { tags : { $ s l i c e :1}}) // Will return { id :1 , tags : [” c ”]} db . a r r a y s . f i n d ({ id : 1 } , { t a g s : { $ s l i c e : −1}}) // Will return { id :1 , tags : [” b” , ”c ”]} db . a r r a y s . f i n d ({ id :1} , { tags : { $ s l i c e :[1 ,2]}}) Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
  6. 6. Array tricks, continued continued continued And you can also update portions of documents matched by the update’s selector with the positional operator, $: > db . a r r a y s . u p d a t e ( { t a g s : ’ b ’ } , { $ s e t : { ’ t a g s . $ ’ : ’X’ } } , false , true ); > db . a r r a y s . f i n d ( ) ; { ” i d ” : 1 , ” t a g s ” : [ ” a ” , ”X” , ” c ” ] } { ” i d ” : 2 , ” t a g s ” : [ ”X” , ” c ” , ”d” ] } { ” i d ” : 3 , ” t a g s ” : [ ” c ” , ”d ” , ” e ” ] } Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
  7. 7. findAndModify db . c o u n t e r s . s a v e ( { i d : ’ some i d ’ , v a l u e : 0 } ) ; function get (){ r e t u r n db . c o u n t e r s . f i n d A n d M o d i f y ( { q u e r y : { i d : ’ some i d ’ } , update :{ $inc :{ v a l u e :1}}}) } > get () { ” i d ” : ” some i d ” , ” v a l u e ” : 0 } > get () { ” i d ” : ” some i d ” , ” v a l u e ” : 1 } > get () { ” i d ” : ” some i d ” , ” v a l u e ” : 2 } > get () { ” i d ” : ” some i d ” , ” v a l u e ” : 3 } Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
  8. 8. findAndModify, continued db . r e s o u r c e s . s a v e ( { f r e e : 90 , buckets : [ { use : 10 , c l i e n t : ” f r e d ” } , { use : 0 , c l i e n t : n u l l } , . . . ] }); function allocate bucket ( quantity , c l i e n t ) { r e t u r n db . r e s o u r c e s . f i n d A n d M o d i f y ( { q u e r y : { ’ b u c k e t s . use ’ : 0 , f r e e : { $gt : q u a n t i t y } } , u p d a t e : { $ i n c : { f r e e : −q u a n t i t y } , $set : { ’ b u c k e t s . $ . use ’ : q u a n t i t y , ’ buckets . $ . c l i e n t ’ : c l i e n t } } }) Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×