当前位置: 首页>>代码示例>>C#>>正文


C# MongoCollection.Aggregate方法代码示例

本文整理汇总了C#中MongoCollection.Aggregate方法的典型用法代码示例。如果您正苦于以下问题:C# MongoCollection.Aggregate方法的具体用法?C# MongoCollection.Aggregate怎么用?C# MongoCollection.Aggregate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MongoCollection的用法示例。


在下文中一共展示了MongoCollection.Aggregate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        public IEnumerable<BsonDocument> Run(MongoCollection<Rental> rentals)
        {
            var priceRange = new BsonDocument(
                "$subtract",
                new BsonArray
                {
                    "$Price",
                    new BsonDocument(
                        "$mod",
                        new BsonArray{"$Price",500}
                        )
                });
            var grouping =new BsonDocument(
                "$group",
                new BsonDocument
                {
                    {"_id",priceRange},
                    {"count",new BsonDocument("$sum",1)}
                });
            var sort=new BsonDocument(
                "$sort",
                new BsonDocument("_id",1)
                );

            var args=new AggregateArgs
            {
                Pipeline=new []{grouping,sort}
            };

            return rentals.Aggregate(args);
        }
开发者ID:nywebman,项目名称:MongoDBdotNet,代码行数:31,代码来源:QueryPriceDistribution.cs

示例2: GetStatisticItem

        private StatisticItem GetStatisticItem(DateTime startStroke, DateTime endStroke, MongoCollection<Session> sessions)
        {
            var match = GetMatchOperator(startStroke, endStroke);
            var project = GetProjectOperator(startStroke);
            var project1 = GetProjectOperator1();
            var group = GetGroupOperator();
            var pipeline = new[] { match, project, group, project1 };
            var queryResult = sessions.Aggregate(pipeline);
            var items = queryResult.ResultDocuments.ToList();

            var doc = queryResult.ResultDocuments.Select(BsonSerializer.Deserialize<StatisticItem>).FirstOrDefault();
            var count = doc == null ? 0 : doc.Count;
            var result = new StatisticItem { DateTime = startStroke, Count = count };
            return result;
        }
开发者ID:namesecured,项目名称:MapReduce,代码行数:15,代码来源:AggregationTests.cs

示例3: GroupByCount

        /// <summary>
        /// Calculate the amount of documents
        /// </summary>
        /// <param name="name">Field name to group by</param>
        /// <returns>Document list represented by "Key" and "Value" inside aggregate result object</returns>
        public AggregateResult GroupByCount(string name, MongoCollection collection, BsonDocument sort, BsonDocument limit, BsonDocument skip, BsonDocument match, bool countGroups =false)
        {
            // prepare pipeline operations
            var group = new BsonDocument
            {
                {
                    "$group",
                    new BsonDocument
                    {
                        {
                            "_id", new BsonDocument
                            {
                                {
                                    "Key","$" + name
                                }
                            }
                        },
                        {
                            "Value", new BsonDocument
                            {
                                {
                                    "$sum", 1
                                }
                            }
                        }
                    }
                }
            };

            var project = new BsonDocument
            {
                {
                    "$project",
                    new BsonDocument
                    {
                        {"_id", 0}, // remove reference to _id
                        {"Key","$_id.Key"}, // set reference to "_id.Name" as "UserName"
                        {"Value", 1}, // dont  change "Count"
                    }
                }
            };

            List<BsonDocument> pipeline = new List<BsonDocument>{ };
            if (match != null)
                pipeline.Add(match);

            pipeline.Add(group);
            if (countGroups)
            {
                var total = new BsonDocument
                {
                    {
                        "$group",
                        new BsonDocument
                        {
                            {
                                "_id", 0
                            },
                            {
                                "Total", new BsonDocument
                                {
                                    {
                                        "$sum", 1
                                    }
                                }
                            }
                        }
                    }
                };

                pipeline.Add(total);
            }
            else
            {
                pipeline.Add(project);

                if (sort != null)
                {
                    pipeline.Add(sort);
                }
                pipeline.Add(skip);
                pipeline.Add(limit);
            }
            collection.EnsureIndex(IndexKeys.Ascending(name));
            return collection.Aggregate(pipeline);
        }
开发者ID:xoperator,项目名称:GoKapara,代码行数:91,代码来源:ActivityDataProvider.cs


注:本文中的MongoCollection.Aggregate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。