@@ -364,44 +364,54 @@ describe('MapReduce', function() {
364
364
var client = configuration . newClient ( configuration . writeConcernMax ( ) , { poolSize : 1 } ) ;
365
365
client . connect ( function ( err , client ) {
366
366
var db = client . db ( configuration . db ) ;
367
+ const outDb = client . db ( 'outputCollectionDb' ) ;
367
368
368
369
// Create a test collection
369
370
db . createCollection ( 'test_map_reduce_functions' , function ( err , collection ) {
370
- // Insert some documents to perform map reduce over
371
- collection . insert (
372
- [ { user_id : 1 } , { user_id : 2 } ] ,
373
- configuration . writeConcernMax ( ) ,
374
- function ( err ) {
375
- test . equal ( null , err ) ;
376
- // Map function
377
- var map = function ( ) {
378
- emit ( this . user_id , 1 ) ; // eslint-disable-line
379
- } ;
380
- // Reduce function
381
- var reduce = function ( ) {
382
- return 1 ;
383
- } ;
384
-
385
- // Perform the map reduce
386
- collection . mapReduce (
387
- map ,
388
- reduce ,
389
- { out : { replace : 'tempCollection' , db : 'outputCollectionDb' } } ,
390
- function ( err , collection ) {
391
- // Mapreduce returns the temporary collection with the results
392
- collection . findOne ( { _id : 1 } , function ( err , result ) {
393
- test . equal ( 1 , result . value ) ;
394
-
395
- collection . findOne ( { _id : 2 } , function ( err , result ) {
371
+ // create the output collection
372
+ outDb . createCollection ( 'tempCollection' , err => {
373
+ test . equal ( null , err ) ;
374
+
375
+ // Insert some documents to perform map reduce over
376
+ collection . insert (
377
+ [ { user_id : 1 } , { user_id : 2 } ] ,
378
+ configuration . writeConcernMax ( ) ,
379
+ function ( err ) {
380
+ test . equal ( null , err ) ;
381
+ // Map function
382
+ var map = function ( ) {
383
+ emit ( this . user_id , 1 ) ; // eslint-disable-line
384
+ } ;
385
+ // Reduce function
386
+ var reduce = function ( ) {
387
+ return 1 ;
388
+ } ;
389
+
390
+ // Perform the map reduce
391
+ collection . mapReduce (
392
+ map ,
393
+ reduce ,
394
+ { out : { replace : 'tempCollection' , db : 'outputCollectionDb' } } ,
395
+ function ( err , collection ) {
396
+ test . equal ( null , err ) ;
397
+
398
+ // Mapreduce returns the temporary collection with the results
399
+ collection . findOne ( { _id : 1 } , function ( err , result ) {
400
+ test . equal ( null , err ) ;
396
401
test . equal ( 1 , result . value ) ;
397
402
398
- client . close ( done ) ;
403
+ collection . findOne ( { _id : 2 } , function ( err , result ) {
404
+ test . equal ( null , err ) ;
405
+ test . equal ( 1 , result . value ) ;
406
+
407
+ client . close ( done ) ;
408
+ } ) ;
399
409
} ) ;
400
- } ) ;
401
- }
402
- ) ;
403
- }
404
- ) ;
410
+ }
411
+ ) ;
412
+ }
413
+ ) ;
414
+ } ) ;
405
415
} ) ;
406
416
} ) ;
407
417
}
0 commit comments