Sharded Environments In sharded environments, data processing of map/reduce operations runs in parallel on all shards.
即: map/reduce操作会并行运行在所有的shards上。
下面我们就用之前这篇文章中白搭建的环境来构造mapreduce查询:
return { count : total };
注意:上面的情况目前出现在了我的测试环境下,如下图:
就需要改成 return count;
下面是测试代码,首先是按帖子id来查询相应数量(基于分组查询实例方式):
public partial class getfile : System.Web.UI.Page
{
public Mongo Mongo { get; set; }
public IMongoDatabase DB
{
get
{
return this.Mongo["dnt_mongodb"];
}
}
///
/// Sets up the test environment. You can either override this OnInit to add custom initialization.
///
public virtual void Init()
{
string ConnectionString = "Server=10.0.4.85:27017;ConnectTimeout=30000;ConnectionLifetime=300000;MinimumPoolSize=512;MaximumPoolSize=51200;Pooled=true";
if (String.IsNullOrEmpty(ConnectionString))
throw new ArgumentNullException("Connection string not found.");
this.Mongo = new Mongo(ConnectionString);
this.Mongo.Connect();
}
string mapfunction = "function(){\n" +
" if(this._id=='548111') { emit(this._id, 1); } \n" +
"};";
string reducefunction = "function(key, current ){" +
" var count = 0;" +
" for(var i in current) {" +
" count+=current;" +
" }" +
" return count ;\n" +
"};";
protected void Page_Load(object sender, EventArgs e)
{
Init();
var mrb = DB["posts1"].MapReduce();//attach_gfstream.files
int groupCount = 0;
using (var mr = mrb.Map(mapfunction).Reduce(reducefunction))
{
foreach (Document doc in mr.Documents)
{
groupCount = int.Parse(doc["value"].ToString());
}
}
this.Mongo.Disconnect();
}
}