public static BsonDocument ToGroupDocument(this IMongoGroupBy fieldsGroupBy, string collectionName)
        {
            var _groupBy = new BsonDocument();

            if (!string.IsNullOrWhiteSpace(collectionName))
            {
                _groupBy.Add(collectionName, string.Format("${0}", collectionName));
            }
            else
            {
                foreach (string _name in fieldsGroupBy.ToBsonDocument().Names)
                {
                    _groupBy.Add(new BsonElement(_name, string.Format("${0}", _name)));
                }
            }


            var _sums = new BsonDocument("_id", _groupBy);

            foreach (string _name in fieldsGroupBy.ToBsonDocument().Names)
            {
                _sums.Add(_name, new BsonDocument("$sum", string.Format("${0}", _name)));
            }

            return(new BsonDocument("$group", _sums));
        }
Beispiel #2
0
        public override IEnumerable <BsonDocument> Group(IMongoQuery query, IMongoGroupBy keys, BsonDocument initial, BsonJavaScript reduce, BsonJavaScript finalize)
        {
            var sw = new Stopwatch();

            sw.Start();
            var result = base.Group(query, keys, initial, reduce, finalize);

            sw.Stop();

            var commandStringBuilder = new StringBuilder(1024);

            commandStringBuilder.AppendFormat("db.{0}.group({{key, reduce, initial", Name);

            if (query != null)
            {
                commandStringBuilder.Append(", cond");
            }

            if (initial != null)
            {
                commandStringBuilder.Append(", initial");
            }

            if (finalize != null)
            {
                commandStringBuilder.Append(", finalize");
            }

            commandStringBuilder.Append("})");

            commandStringBuilder.AppendFormat("\nkey = {0}", keys.ToBsonDocument());

            commandStringBuilder.Append("\nreduce = javascript");

            commandStringBuilder.AppendFormat("\ninitial = {0}", initial.ToBsonDocument());

            if (query != null)
            {
                commandStringBuilder.AppendFormat("\ncond = {0}", query.ToBsonDocument());
            }

            if (initial != null)
            {
                commandStringBuilder.AppendFormat("\ninitial = {0}", initial.ToBsonDocument());
            }

            if (finalize != null)
            {
                commandStringBuilder.Append("\nfinalize = javascript");
            }

            string commandString = commandStringBuilder.ToString();

            ProfilerUtils.AddMongoTiming(commandString, sw.ElapsedMilliseconds, ExecuteType.Read);

            return(result);
        }