var connectionString = "mongodb://localhost/?safe=true";
|
var server = MongoServer.Create(connectionString);
|
var inputDatabase = server.GetDatabase("inputdatabase", new MongoCredentials("user", "password"));
|
var inputCollection = inputDatabase.GetCollection("inputcollection");
|
|
// this is Example 1 on p. 87 of MongoDB: The Definitive Guide
|
// by Kristina Chodorow and Michael Dirolf
|
|
inputCollection.RemoveAll();
|
inputCollection.Insert(new BsonDocument { { "A", 1 }, { "B", 2 } });
|
inputCollection.Insert(new BsonDocument { { "B", 1 }, { "C", 2 } });
|
inputCollection.Insert(new BsonDocument { { "X", 1 }, { "B", 2 } });
|
|
var query = Query.Exists("B");
|
|
var map =
|
"function() {\n" +
|
" for (var key in this) {\n" +
|
" emit(key, {count : 1});\n" +
|
" }\n" +
|
"}\n";
|
|
var reduce =
|
"function(key, emits) {\n" +
|
" total = 0;\n" +
|
" for (var i in emits) {\n" +
|
" total += emits[i].count;\n" +
|
" }\n" +
|
" return {count : total};\n" +
|
"}\n";
|
|
var outputDatabase = server.GetDatabase("outputdatabase", new MongoCredentials("user", "password"));
|
using (outputDatabase.RequestStart())
|
{
|
var options = MapReduceOptions.SetOutput(new MapReduceOutput { DatabaseName = "outputdatabase", CollectionName = "outputcollection" });
|
var result = inputCollection.MapReduce(query, map, reduce, options);
|
}
|