SlideShare a Scribd company logo
Map/Reduce, geospatial search, and other Cool
                     Features

                                    Richard M Kreuter
                                         10gen Inc.
                                    richard@10gen.com


                                        April 26, 2011




Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
Things I’ll cover




          Array tricks
          The findAndModify command




   Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
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
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
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
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
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
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

More Related Content

What's hot

Tugas Fuskom
Tugas FuskomTugas Fuskom
Tugas Fuskom
yeyen
 
Manual de practicas
Manual de practicasManual de practicas
Manual de practicas
jacqueline de kikas
 
Zadatak
ZadatakZadatak
Zadatak
Zeljko Lukic
 
Membuat Aplikasi dengan C++
Membuat Aplikasi dengan C++Membuat Aplikasi dengan C++
Membuat Aplikasi dengan C++
Achmad Sidik
 

What's hot (6)

Tugas Fuskom
Tugas FuskomTugas Fuskom
Tugas Fuskom
 
Manual de practicas
Manual de practicasManual de practicas
Manual de practicas
 
python-geohex
python-geohexpython-geohex
python-geohex
 
Zadatak
ZadatakZadatak
Zadatak
 
Membuat Aplikasi dengan C++
Membuat Aplikasi dengan C++Membuat Aplikasi dengan C++
Membuat Aplikasi dengan C++
 
Vcs12
Vcs12Vcs12
Vcs12
 

Viewers also liked

Mongophilly shell-2011-04-26
Mongophilly shell-2011-04-26Mongophilly shell-2011-04-26
Mongophilly shell-2011-04-26
kreuter
 
Mongophilly cool-features-2011-04-26
Mongophilly cool-features-2011-04-26Mongophilly cool-features-2011-04-26
Mongophilly cool-features-2011-04-26
kreuter
 
Patterns and antipatterns
Patterns and antipatternsPatterns and antipatterns
Patterns and antipatterns
kreuter
 
Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26
kreuter
 
Stay Up To Date on the Latest Happenings in the Boardroom: Recommended Summer...
Stay Up To Date on the Latest Happenings in the Boardroom: Recommended Summer...Stay Up To Date on the Latest Happenings in the Boardroom: Recommended Summer...
Stay Up To Date on the Latest Happenings in the Boardroom: Recommended Summer...
Stanford GSB Corporate Governance Research Initiative
 
What's Next in Growth? 2016
What's Next in Growth? 2016What's Next in Growth? 2016
What's Next in Growth? 2016
Andrew Chen
 
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
Barry Feldman
 

Viewers also liked (7)

Mongophilly shell-2011-04-26
Mongophilly shell-2011-04-26Mongophilly shell-2011-04-26
Mongophilly shell-2011-04-26
 
Mongophilly cool-features-2011-04-26
Mongophilly cool-features-2011-04-26Mongophilly cool-features-2011-04-26
Mongophilly cool-features-2011-04-26
 
Patterns and antipatterns
Patterns and antipatternsPatterns and antipatterns
Patterns and antipatterns
 
Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26Mongophilly indexing-2011-04-26
Mongophilly indexing-2011-04-26
 
Stay Up To Date on the Latest Happenings in the Boardroom: Recommended Summer...
Stay Up To Date on the Latest Happenings in the Boardroom: Recommended Summer...Stay Up To Date on the Latest Happenings in the Boardroom: Recommended Summer...
Stay Up To Date on the Latest Happenings in the Boardroom: Recommended Summer...
 
What's Next in Growth? 2016
What's Next in Growth? 2016What's Next in Growth? 2016
What's Next in Growth? 2016
 
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
 

Mongophilly cool-features-2011-04-26

  • 1. Map/Reduce, geospatial search, and other Cool Features Richard M Kreuter 10gen Inc. richard@10gen.com April 26, 2011 Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
  • 2. Things I’ll cover Array tricks The findAndModify command Map/Reduce, geospatial search, and other Cool Features — MongoPhilly
  • 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. 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. 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. 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. 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. 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