private object ExecuteMapReduce(MongoQueryObject queryObject) { var options = new MapReduceOptionsBuilder(); options.SetQuery(new QueryDocument(queryObject.Query)); options.SetFinalize(new BsonJavaScript(queryObject.FinalizerFunction)); options.SetLimit(queryObject.NumberToLimit); if (queryObject.Sort != null) { options.SetSortOrder(new SortByDocument(queryObject.Sort)); } if (queryObject.NumberToSkip != 0) { throw new InvalidQueryException("MapReduce queries do not support Skips."); } var mapReduce = _collection.MapReduce( new BsonJavaScript(queryObject.MapFunction), new BsonJavaScript(queryObject.ReduceFunction), options); var executor = GetExecutor(typeof(BsonDocument), queryObject.Projector, queryObject.Aggregator, true); return(executor.Compile().DynamicInvoke(queryObject.Collection.Database[mapReduce.CollectionName].FindAll())); }
/// <summary> /// mapReduce得到结果 /// </summary> /// <param name="collectionName"></param> /// <param name="query"></param> /// <param name="finalizeJs"></param> /// <param name="mapJs"></param> /// <param name="reduceJs"></param> /// <param name="limit"></param> /// <param name="sortBy"></param> /// <returns></returns> public MapReduceResult MapReduceToResult(string collectionName, IMongoQuery query, string finalizeJs, string mapJs, string reduceJs, int limit, IMongoSortBy sortBy) { MongoCollection collection = GetMongoCollection(collectionName); MapReduceOptionsBuilder builder = new MapReduceOptionsBuilder(); builder.SetFinalize(finalizeJs); if (limit > 0) { builder.SetLimit(limit); } if (sortBy != null) { builder.SetSortOrder(sortBy); } MapReduceOutput output = new MapReduceOutput(); output.Mode = MapReduceOutputMode.Inline; builder.SetOutput(output); if (query == null || query == MongoDB.Driver.Builders.Query.Null) { return(collection.MapReduce(mapJs, reduceJs, builder)); } else { return(collection.MapReduce(query, mapJs, reduceJs, builder)); } }