public void Test3Keys()
        {
            var    groupBy  = GroupBy.Keys("a", "b", "c");
            string expected = "{ \"a\" : 1, \"b\" : 1, \"c\" : 1 }";

            Assert.AreEqual(expected, groupBy.ToJson());
        }
Ejemplo n.º 2
0
        public void GroupByIMongoGroupByTest()
        {
            const string CollectionName = "group-by-IMongoGroupBy-test";

            Repository.DropCollection(CollectionName);
            Repository.CreateCollection(CollectionName);
            Repository.RemoveAll();

            Repository.Insert(new BsonDocument("x", 1));
            Repository.Insert(new BsonDocument("x", 1));
            Repository.Insert(new BsonDocument("x", 2));
            Repository.Insert(new BsonDocument("x", 3));
            Repository.Insert(new BsonDocument("x", 3));
            Repository.Insert(new BsonDocument("x", 3));


            var          initial = new BsonDocument("count", 0);
            const string reduce  = "function(doc, prev) { prev.count += 1; }";
            var          results = Repository.Group(Query.Null, GroupBy.Keys("x"), initial, reduce, null).ToArray();

            results.Length.Should().Be(3);

            results[0]["x"].ToInt32().Should().Be(1);
            results[0]["count"].ToInt32().Should().Be(2);

            results[1]["x"].ToInt32().Should().Be(2);
            results[1]["count"].ToInt32().Should().Be(1);

            results[2]["x"].ToInt32().Should().Be(3);
            results[2]["count"].ToInt32().Should().Be(3);
        }
        public void Test1Key()
        {
            var    groupBy  = GroupBy.Keys("a");
            string expected = "{ \"a\" : 1 }";

            Assert.AreEqual(expected, groupBy.ToJson());
        }
        public void Test3Keys_Typed()
        {
            var groupBy = GroupBy <Test> .Keys(x => x.A, x => x.B, x => x.C);

            string expected = "{ \"a\" : 1, \"b\" : 1, \"c\" : 1 }";

            Assert.AreEqual(expected, groupBy.ToJson());
        }
        public void Test1Key_Typed()
        {
            var groupBy = GroupBy <Test> .Keys(x => x.A);

            string expected = "{ \"a\" : 1 }";

            Assert.AreEqual(expected, groupBy.ToJson());
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Runs the group command on this collection.
 /// </summary>
 /// <param name="query">The query (usually a QueryDocument or constructed using the Query builder).</param>
 /// <param name="key">The name of the field to group on.</param>
 /// <param name="initial">Initial value passed to the reduce function for each group.</param>
 /// <param name="reduce">A JavaScript function that is called for each matching document in a group.</param>
 /// <param name="finalize">A JavaScript function that is called at the end of the group command.</param>
 /// <returns>A list of results as BsonDocuments.</returns>
 public virtual IEnumerable <BsonDocument> Group(
     IMongoQuery query,
     string key,
     BsonDocument initial,
     BsonJavaScript reduce,
     BsonJavaScript finalize)
 {
     return(Group(query, GroupBy.Keys(key), initial, reduce, finalize));
 }
Ejemplo n.º 7
0
        public void Group()
        {
            IUserRepository repo   = new UserRepository();
            var             result = repo.Group(new GroupArgs
            {
                KeyFields      = GroupBy.Keys("type"),//设置了 KeyFields 则无需设置 KeyFunction
                Query          = Query.GTE("cTime", DateTime.Now.AddDays(-3)),
                Initial        = new BsonDocument("count", 0),
                ReduceFunction = "function(doc,per){return per.count+=1;}",
            }).ToArray();

            Assert.Equal(2, result.Length);
        }