/* Copyright 2010 10gen Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Text; using NUnit.Framework; using MongoDB.Bson; using MongoDB.Bson.DefaultSerializer; using MongoDB.Driver; using MongoDB.Driver.Builders; namespace MongoDB.DriverOnlineTests.Jira.CSharpMapReduce { [TestFixture] public class CSharpMapReduceTests { [Test] public void MapReduce() { var server = MongoServer.Create("mongodb://localhost/?safe=true"); var database = server["onlinetests"]; var collection = database["mapReduce"]; collection.RemoveAll(); // ref: http://cookbook.mongodb.org/patterns/count_tags/ var tags = new List() { "MongoDB", "Map/Reduce", "Recipe" }; var postDocument = new BsonDocument { { "title", "A blog post" }, {"author", "Kristina" }, { "content", "..." }, { "tags", new BsonArray(tags.Select(t => t)) }, }; collection.Save(postDocument); var map = "map = function() { " + " if (!this.tags) { " + "return; " + " } " + " for (index in this.tags) { " + " emit(this.tags[index], 1); " + " } " + "}; "; var reduce = "reduce = function(previous, current) { " + " var count = 0; " + " for (index in current) { " + " count += current[index]; " + " } " + " return count; " + "}; "; var result = collection.MapReduce(map, reduce); Assert.IsTrue(result.Duration != null); } } }