Skip to content

Commit 237bf6c

Browse files
map/reduce test with scope
1 parent c671fa1 commit 237bf6c

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/test/com/mongodb/JavaClientTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,36 @@ public void testMapReduceInline(){
416416
assertEquals( 1 , m.get( "d" ).intValue() );
417417

418418
}
419-
419+
420+
@Test
421+
public void testMapReduceInlineWScope(){
422+
DBCollection c = _db.getCollection( "jmr2" );
423+
c.drop();
424+
425+
c.save( new BasicDBObject( "x" , new String[]{ "a" , "b" } ) );
426+
c.save( new BasicDBObject( "x" , new String[]{ "b" , "c" } ) );
427+
c.save( new BasicDBObject( "x" , new String[]{ "c" , "d" } ) );
428+
429+
Map<String, Object> scope = new HashMap<String, Object>();
430+
scope.put("exclude", "a");
431+
432+
MapReduceCommand mrc = new MapReduceCommand( c, "function(){ for ( var i=0; i<this.x.length; i++ ){ if(this.x[i] != exclude) emit( this.x[i] , 1 ); } }" ,
433+
"function(key,values){ var sum=0; for( var i=0; i<values.length; i++ ) sum += values[i]; return sum;}" , null, MapReduceCommand.OutputType.INLINE, null);
434+
mrc.setScope( scope );
435+
436+
MapReduceOutput out = c.mapReduce( mrc );
437+
Map<String,Integer> m = new HashMap<String,Integer>();
438+
for ( DBObject r : out.results() ){
439+
m.put( r.get( "_id" ).toString() , ((Number)(r.get( "value" ))).intValue() );
440+
}
441+
442+
assertEquals( 3 , m.size() );
443+
assertEquals( 2 , m.get( "b" ).intValue() );
444+
assertEquals( 2 , m.get( "c" ).intValue() );
445+
assertEquals( 1 , m.get( "d" ).intValue() );
446+
447+
}
448+
420449
String _testMulti( DBCollection c ){
421450
String s = "";
422451
for ( DBObject z : c.find().sort( new BasicDBObject( "_id" , 1 ) ) ){

0 commit comments

Comments
 (0)