C# MapReduce从IIdGenerator查找最大整数Id

C# MapReduce从IIdGenerator查找最大整数Id,c#,mongodb,C#,Mongodb,我因理解mapreduce而心碎。 所以,我请求你的帮助 我有这样的实体: public class ConstraintSpec { [BsonId] public int Id { get; set; } public bool OrderRequired { get; set; } public ActionTypeConstraintSpec[] ActionTypeConstraintSpecs { get; set; } } 我正在试着写我的id生成

我因理解mapreduce而心碎。 所以,我请求你的帮助

我有这样的实体:

public class ConstraintSpec
{
    [BsonId]
    public int Id { get; set; }
    public bool OrderRequired { get; set; }
    public ActionTypeConstraintSpec[] ActionTypeConstraintSpecs { get; set; }
}
我正在试着写我的id生成器

以下是我的“不工作”MapReduce js代码:

Map = "map = function () {" +
              "emit(this._id)}";

        Reduce = "reduce = function (key, values) {" +
                     "var max = this[0];" +
                     "var len = this.length;" +
                     "for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];" +
                     "return max;" +
                 "}";
但什么都不管用


求求你,救命

你想用map reduce实现什么,你需要为哪个字段找到最大值?在映射函数中,您需要发出您试图聚合/比较的值;您当前仅发出一个密钥this.id。假定每个文档的_id是唯一的,这将阻止调用reduce函数(如果为特定键发出单个文档,则不会调用reduce)。键必须是要在其上聚合数据的字段的名称

例如,在shell中:

输入数据:

{_id: 1, name: "test1", number: 5}
{_id: 2, name: "test1", number: 8}
{_id: 3, name: "test2", number: 3}
{_id: 4, name: "test2", number: 11}
地图功能:

function () {
    emit{this.name, this.number}
}
    function(key, values) {
        var max = 0;
        values.forEach(function(value) {
           if(value > max){
              max = value;
           }
        });
        return max;
    }
减少功能:

function () {
    emit{this.name, this.number}
}
    function(key, values) {
        var max = 0;
        values.forEach(function(value) {
           if(value > max){
              max = value;
           }
        });
        return max;
    }
输出数据:

{_id: "test1", value: 8}
{_id: "test2", value: 11}

如果要生成唯一的升序id值,可以使用一个集合,其中包含一个id值为的文档,并使用findAndModify with$inc运算符递增并返回该文档。