继上一篇,这一篇主要为Mongo Map-Reduce的C#版实现,如果大家对C# Driver不是太熟悉,没关系,MongoDB官网有很好的入门文章,你会很快掌握C#如何编写Mongo程序,而不用天天对着Console来输入命令了,Getting Started with the CSharp Driver 。
下面回到正题,
二. C#版本:
1. 构造实体类Record.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestApp
{
/// <summary>
/// The Class of Record
/// </summary>
public class Record
{
/// <summary>
/// The Customer Id
/// </summary>
public int cusid { get; set; }
/// <summary>
/// The Price
/// </summary>
public int price { get; set; }
}
}
2. 编写MongoDBHelper.cs方法:一些公共的操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
namespace TestApp
{
/// <summary>
/// The Class of MongoDBHelper: common operation for MongoDB
/// </summary>
public class MongoDBHelper
{
/// <summary>
/// Get the specific db instance
/// </summary>
/// <param name="connectionString">The connect string</param>
/// <param name="dbName">DB name</param>
/// <returns>The DB instance</returns>
public static MongoDatabase GetDatabase(string connectionString, string dbName)
{
var client = new MongoClient(connectionString);
var server = client.GetServer();
MongoDatabase database = server.GetDatabase(dbName);
return database;
}
}
}
3. 实现Map-Reduce程序:
class Program
{
/// <summary>
/// Insert test data to records collection in db
/// </summary>
/// <param name="records">The records instance</param>
static void InsertTestDataToRecords(MongoCollection<Record> records)
{
records.Insert<Record>(new Record() { cusid = 1, price = 15 });
records.Insert<Record>(new Record() { cusid = 2, price = 30 });
records.Insert<Record>(new Record() { cusid = 2, price = 45 });
records.Insert<Record>(new Record() { cusid = 3, price = 45 });
records.Insert<Record>(new Record() { cusid = 4, price = 5 });
records.Insert<Record>(new Record() { cusid = 5, price = 65 });
records.Insert<Record>(new Record() { cusid = 1, price = 10 });
records.Insert<Record>(new Record() { cusid = 1, price = 30 });
records.Insert<Record>(new Record() { cusid = 5, price = 30 });
records.Insert<Record>(new Record() { cusid = 4, price = 100 });
records.Insert<Record>(new Record() { cusid = 3, price = 10 });
}
static void Main(string[] args)
{
#region Test MongoDB Map-Reduce
// Connection string format: mongodb://[username:password@][host][:port]/[database]
var connectionString = "mongodb://Kevin:123456@localhost:27017/admin";
string dbName = "admin";
var db = MongoDBHelper.GetDatabase(connectionString, dbName);
var records = db.GetCollection<Record>("records");
InsertTestDataToRecords(records);
// Write map and reduce function
string mapFunction = @"function(){
emit(this.cusid, this.price);
};";
string reduceFunction = @"function(cusid, prices){
var total = 0;
total = Array.sum(prices);
return { sum: total };
};";
// Execute map-reduce method
var cusid_prices_results = records.MapReduce(mapFunction, reduceFunction);
// Print results
Console.WriteLine("Print the results of executing MapReduce method:\r\n");
foreach (var item in cusid_prices_results.GetResults())
{
Console.WriteLine(item.ToString());
}
Console.ReadKey();
#endregion
}
}
执行结果:
看到了,结果和上一篇中Mongo Shell版的一样,至此,关于MongoDB之Map-Reduce应用就到这里了,有时间我还会继续更新更多地关于MongoDB的特性和知识,谢谢!