繼上一篇,這一篇主要為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的特性和知識,謝謝!