Skip to content

Instantly share code, notes, and snippets.

@NicolasPelletier
Created September 10, 2013 17:57
Show Gist options
  • Save NicolasPelletier/6513077 to your computer and use it in GitHub Desktop.
Save NicolasPelletier/6513077 to your computer and use it in GitHub Desktop.
This test demonstrate a problem with mapReduce in mongodb 1.3.19.
/*jslint indent: 4, node: true, nomen:true, maxlen: 125*/
/*global emit */
'use strict';
var DATABASE_URL = 'mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/dbName?replicaSet=rs0',
mongodb = require('mongodb'),
assert = require('assert'),
dbConnectConfig = {db: { w: 1 }};
mongodb.Db.connect(DATABASE_URL, dbConnectConfig, function (err, db) {
if (err) { console.log(err); process.exit(-1); }
// Create a test collection
db.createCollection('test_map_reduce_functions', function (err, collection) {
if (err) { console.log(err); process.exit(-2); }
// Insert some documents to perform map reduce over
collection.insert([{'user_id': 1}, {'user_id': 2}], {w: 1}, function () {
if (err) { console.log(err); process.exit(-3); }
// Map function
var map = function () { emit(this.user_id, 1); },
// Reduce function
reduce = function () { return 1; };
// Peform the map reduce
collection.mapReduce(map, reduce, {out: {replace : 'tempCollection'}}, function (err, collection) {
if (err) { console.log(err); process.exit(-4); }
// Mapreduce returns the temporary collection with the results
collection.findOne({'_id': 1}, function (err, result) {
if (err) { console.log(err); process.exit(-5); }
assert(result.value === 1);
collection.findOne({'_id': 2}, function (err, result) {
if (err) { console.log(err); process.exit(-6); }
assert(result.value === 1);
db.close(true, function () {
console.log('test done!');
});
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment