
s = db.system.js;
s.remove({});
assert.eq( 0 , s.count() , "setup - A" );

s.save( { _id : "x" , value : "3" } );
assert.isnull( db.getLastError() , "setup - B" );
assert.eq( 1 , s.count() , "setup - C" );

s.remove( { _id : "x" } );
assert.eq( 0 , s.count() , "setup - D" );
s.save( { _id : "x" , value : "4" } );
assert.eq( 1 , s.count() , "setup - E" );

assert.eq( 4 , s.findOne( { _id : "x" } ).value , "E2 " );

assert.eq( 4 , s.findOne().value , "setup - F" );
s.update( { _id : "x" } , { $set : { value : 5  } } );
assert.eq( 1 , s.count() , "setup - G" );
assert.eq( 5 , s.findOne().value , "setup - H" );

assert.eq( 5 , db.eval( "return x" ) , "exec - 1 " );

s.update( { _id : "x" } , { $set : { value : 6  } } );
assert.eq( 1 , s.count() , "setup2 - A" );
assert.eq( 6 , s.findOne().value , "setup - B" );

assert.eq( 6 , db.eval( "return x" ) , "exec - 2 " );



s.insert( { _id : "bar" , value : function( z ){ return 17 + z; } } );
assert.eq( 22 , db.eval( "return bar(5);"  ) , "exec - 3 " );

assert( s.getIndexKeys().length > 0 , "no indexes" );
assert( s.getIndexKeys()[0]._id , "no _id index" );

assert.eq( "undefined" , db.eval( function(){ return typeof(zzz); } ) , "C1" );
s.save( { _id : "zzz" , value : 5 } )
assert.eq( "number" , db.eval( function(){ return typeof(zzz); } ) , "C2" );
s.remove( { _id : "zzz" } );
assert.eq( "undefined" , db.eval( function(){ return typeof(zzz); } ) , "C3" );

@@ -1,1423 +1,486 @@
-%0As = db.system.js;%0As.remove(%7B%7D);%0Aassert.eq( 0 , s.count() , %22setup - A%22 );%0A%0As.save( %7B _id : %22x%22 , value : %223%22 %7D );%0Aassert.isnull( db.getLastError() , %22setup - B%22 );%0Aassert.eq( 1 , s.count() , %22setup - C%22 );%0A%0As.remove( %7B _id : %22x%22 %7D );%0Aassert.eq( 0 , s.count() , %22setup - D%22 );%0As.save( %7B _id : %22x%22 , value : %224%22 %7D );%0Aassert.eq( 1 , s.count() , %22setup - E%22 );%0A%0Aassert.eq( 4 , s.findOne( %7B _id : %22x%22 %7D ).value , %22E2 %22 );%0A%0Aassert.eq( 4 , s.findOne().value , %22setup - F%22 );%0As.update( %7B _id : %22x%22 %7D , %7B $set : %7B value : 5  %7D %7D );%0Aassert.eq( 1 , s.count() , %22setup - G%22 );%0Aassert.eq( 5 , s.findOne().value , %22setup - H%22 );%0A%0Aassert.eq( 5 , db.eval( %22return x%22 ) , %22exec - 1 %22 );%0A%0As.update( %7B _id : %22x%22 %7D , %7B $set : %7B value : 6  %7D %7D );%0Aassert.eq( 1 , s.count() , %22setup2 - A%22 );%0Aassert.eq( 6 , s.findOne().value , %22setup - B%22 );%0A%0Aassert.eq( 6 , db.eval( %22return x%22 ) , %22exec - 2 %22 );%0A%0A%0A%0As.insert( %7B _id : %22bar%22 , value : function( z )%7B return 17 + z; %7D %7D );%0Aassert.eq( 22 , db.eval( %22return bar(5);%22  ) , %22exec - 3 %22 );%0A%0Aassert( s.getIndexKeys().length %3E 0 , %22no indexes%22 );%0Aassert( s.getIndexKeys()%5B0%5D._id , %22no _id index%22 );%0A%0Aassert.eq( %22undefined%22 , db.eval( function()%7B return typeof(zzz); %7D ) , %22C1%22 );%0As.save( %7B _id : %22zzz%22 , value : 5 %7D )%0Aassert.eq( %22number%22 , db.eval( function()%7B return typeof(zzz); %7D ) , %22C2%22 );%0As.remove( %7B _id : %22zzz%22 %7D );%0Aassert.eq( %22undefined%22 , db.eval( function()%7B return typeof(zzz); %7D ) , %22C3%22 );%0A
+// examples of storing functions for later use%0A// from storefunc.js%0As = db.system.js;%0A%0As.insert( %7B _id : %22bar%22 , value : function( z )%7B return 17 + z; %7D %7D );%0Aassert.eq( 22 , db.eval( %22return bar(5);%22  ) , %22exec - 3 %22 );%0A%0Aassert( s.getIndexKeys().length %3E 0 , %22no indexes%22 );%0Aassert( s.getIndexKeys()%5B0%5D._id , %22no _id index%22 );%0A%0A// check the %22jstests%22 dir of the mongodb source for some exciting examples of %0A// stuff being done - such as mr*.js for mapreduce, and group* for%0A// grouping

document some examples