MongoDB Map减少C#

时间:2014-06-04 13:05:53

标签: mapreduce mongodb-.net-driver

我目前正在使用Mongo中的c#驱动程序进行map-reduce。 我已经让它在JSON如下工作:

{ "_id" : CSUUID("ef53b163-699c-462f-9135-b81bad115635"), "value" : { "firstname" : "Joe", "lastname" : "Bloggs", "groupName" : "System Wide Access" } }

我想要做的是展平这个对象,因为我没有Id和Value字段,我只想要读取模型类中的实际属性。

这是我目前的代码:

        const string mapUserGroupMember = @"function ()
                                            {
                                              var output = {groupId:this.GroupId, firstname:this.Forename, lastname:this.Surname, groupName:null}
                                              emit(this.GroupId, output);                
                                            }";


        const string mapUserGroupName = @"function ()
                                            {
                                              var output = {groupId:this._id, firstname:null, lastname:null, groupName:this.Name}
                                              emit(this._id, output);                
                                            }";

        var reduceF = @"function(key, values) {
                        var results = {firstname:null, lastname:null , groupName:null};

                        values.forEach(function(v){

                                       if(results.firstname ==null){
                                            results.firstname = v.firstname
                                        }
                                        if(results.lastname ==null){
                                            results.lastname = v.lastname
                                        }
                                        if(results.groupName ==null){
                                            results.groupName = v.groupName
                                        }

                                         });
                                        return results;
                                    };";

        var groupMemberCollection = database.GetCollection("UserGroupMemberReadModel");
        var groupNameCollection = database.GetCollection("UserGroupNameReadModel");

        var options = new MapReduceOptionsBuilder();
        options.SetOutput(MapReduceOutput.Reduce("MergedData"));

        var results = groupNameCollection.MapReduce(mapUserGroupName, reduceF, options);
        results = groupMemberCollection.MapReduce(mapUserGroupMember, reduceF, options);

我希望能够致电var collection = database.GetCollection("MergedData").AsQueryable<ReadModel>();

任何帮助都将不胜感激。

0 个答案:

没有答案