Mongophilly cool-features-2011-04-26
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
394
On Slideshare
394
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
3
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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. 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