MongoDB.DriverUnitTests.MongoCollectionTests.TestMapReduce C# (CSharp) Метод

TestMapReduce() приватный Метод

private TestMapReduce ( ) : void
Результат void
        public void TestMapReduce()
        {
            // this is Example 1 on p. 87 of MongoDB: The Definitive Guide
            // by Kristina Chodorow and Michael Dirolf

            _collection.Drop();
            _collection.Insert(new BsonDocument { { "A", 1 }, { "B", 2 } });
            _collection.Insert(new BsonDocument { { "B", 1 }, { "C", 2 } });
            _collection.Insert(new BsonDocument { { "X", 1 }, { "B", 2 } });

            var map =
                "function() {\n" +
                "    for (var key in this) {\n" +
                "        emit(key, {count : 1});\n" +
                "    }\n" +
                "}\n";

            var reduce =
                "function(key, emits) {\n" +
                "    total = 0;\n" +
                "    for (var i in emits) {\n" +
                "        total += emits[i].count;\n" +
                "    }\n" +
                "    return {count : total};\n" +
                "}\n";

            var options = MapReduceOptions.SetOutput("mrout");
            var result = _collection.MapReduce(map, reduce, options);
            Assert.IsTrue(result.Ok);
            Assert.IsTrue(result.Duration >= TimeSpan.Zero);
            Assert.AreEqual(9, result.EmitCount);
            Assert.AreEqual(5, result.OutputCount);
            Assert.AreEqual(3, result.InputCount);
            Assert.IsNotNullOrEmpty(result.CollectionName);

            var expectedCounts = new Dictionary<string, int>
            {
                { "A", 1 },
                { "B", 3 },
                { "C", 1 },
                { "X", 1 },
                { "_id", 3 }
            };

            // read output collection ourselves
            foreach (var document in _database.GetCollection(result.CollectionName).FindAll())
            {
                var key = document["_id"].AsString;
                var count = document["value"]["count"].ToInt32();
                Assert.AreEqual(expectedCounts[key], count);
            }

            // test GetResults
            foreach (var document in result.GetResults())
            {
                var key = document["_id"].AsString;
                var count = document["value"]["count"].ToInt32();
                Assert.AreEqual(expectedCounts[key], count);
            }

            // test GetResultsAs<>
            foreach (var document in result.GetResultsAs<TestMapReduceDocument>())
            {
                Assert.AreEqual(expectedCounts[document.Id], document.Value.Count);
            }
        }