MongoDB之Map-Reduce — Mongo Shell版和C#版(下)

來源:互聯網
上載者:User

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.