예제 #1
0
        public List <T> GetMany(FilterDefinition <T> filter, SortDefinition <T> sorter, ProjectionDefinition <T> projection, ref PagerInfo pagerInfo)
        {
            IMongoCollection <T> myCollection = GetCollection();

            AggregateOptions opts = new AggregateOptions()
            {
                AllowDiskUse = true,
                BatchSize    = int.MaxValue,
                MaxTime      = TimeSpan.FromMinutes(10)
            };

            IAggregateFluent <T> aggregate = GetAggregateFluent(opts);

            if (filter == null)
            {
                filter = Builders <T> .Filter.Empty;
            }
            aggregate = aggregate.Match(filter);

            if (sorter != null)
            {
                aggregate.Sort(sorter);
            }
            if (projection != null)
            {
                aggregate = aggregate.Project <T>(projection);
            }

            pagerInfo.Total = myCollection.CountAsync(filter).GetAwaiter().GetResult();

            List <T> result = aggregate.Match(filter).Sort(sorter).Skip(pagerInfo.Page).Limit(pagerInfo.PageSize)
                              .ToListAsync <T>().GetAwaiter().GetResult();

            return(result);
        }
        /// <summary>
        /// Appends a descending sort stage to the pipeline.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <param name="field">The field to sort by.</param>
        /// <returns>
        /// The fluent aggregate interface.
        /// </returns>
        public static IOrderedAggregateFluent <TResult> SortByDescending <TResult>(this IAggregateFluent <TResult> aggregate, Expression <Func <TResult, object> > field)
        {
            Ensure.IsNotNull(aggregate, "aggregate");
            Ensure.IsNotNull(field, "field");

            return((IOrderedAggregateFluent <TResult>)aggregate.Sort(
                       new DirectionalSortDefinition <TResult>(new ExpressionFieldDefinition <TResult>(field), SortDirection.Descending)));
        }
예제 #3
0
        protected IAggregateFluent <TEntity> CreateAggregate(FilterDefinition <TEntity> filter, SortDefinition <TEntity> sort, ReadPreference readPreference = null)
        {
            IAggregateFluent <TEntity> fluent = IMongoCollectionExtensions.Aggregate(this.GetCollection(readPreference), null).Match(filter);

            if (sort != null)
            {
                fluent = fluent.Sort(sort);
            }
            return(fluent);
        }
예제 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="sort"></param>
        /// <param name="readPreference"></param>
        /// <returns></returns>
        protected IAggregateFluent <TEntity> CreateAggregate(FilterDefinition <TEntity> filter, SortDefinition <TEntity> sort, ReadPreference readPreference = null)
        {
            IAggregateFluent <TEntity> aggregateFluent = this.GetCollection(readPreference).Aggregate(null);

            aggregateFluent = aggregateFluent.Match(filter);
            if (sort != null)
            {
                aggregateFluent = aggregateFluent.Sort(sort);
            }
            return(aggregateFluent);
        }
예제 #5
0
        /// <summary>
        /// Appends a descending sort stage to the pipeline.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="field">The field to sort by.</param>
        /// <returns>The fluent aggregate interface.</returns>
        public static IOrderedAggregateFluent <TDocument, TResult> SortByDescending <TDocument, TResult>(this IAggregateFluent <TDocument, TResult> source, Expression <Func <TResult, object> > field)
        {
            Ensure.IsNotNull(source, "source");
            Ensure.IsNotNull(field, "field");

            var helper = new BsonSerializationInfoHelper();

            helper.RegisterExpressionSerializer(field.Parameters[0], source.Collection.Settings.SerializerRegistry.GetSerializer <TResult>());
            var sortDocument = new SortByBuilder <TResult>(helper).Descending(field).ToBsonDocument();

            source = source.Sort(sortDocument);

            return(new AggregateFluent <TDocument, TResult>(source.Collection, source.Pipeline, source.Options, source.ResultSerializer));
        }
예제 #6
0
        //public async Task<IEnumerable<TestCase>> AllTestsByDate(string date, string endDate, string sort, int? limit)
        //{
        //    try
        //    {
        //        var aggregate = _context.Notes.Aggregate()
        //            .Match(r => r.Date >= DateTime.Parse(date) && r.Date <= DateTime.Parse(endDate));
        //        var results = await aggregate.ToListAsync();
        //        var sorting = await aggregate.Sort(sort).ToListAsync();
        //        return results;
        //    }
        //    catch (Exception ex)
        //    {
        //        // log or manage the exception
        //        throw ex;
        //    }
        //}

        public async Task <IEnumerable <object> > GetAllTestsByDate(string date, string endDate, string sort, int limit)
        {
            IAggregateFluent <TestCase> aggregate = null;

            try
            {
                if (string.IsNullOrEmpty(date) && string.IsNullOrEmpty(endDate))
                {
                    aggregate = _context.Notes.Aggregate()
                                .Match(r => r.Date == DateTime.Now.Date);
                }
                if (!string.IsNullOrEmpty(date) && string.IsNullOrEmpty(endDate))
                {
                    aggregate = _context.Notes.Aggregate()
                                .Match(r => r.Date == DateTime.Parse(date));
                }
                if (!string.IsNullOrEmpty(date) && !string.IsNullOrEmpty(endDate))
                {
                    aggregate = _context.Notes.Aggregate()
                                .Match(r => r.Date >= DateTime.Parse(date).Date&& r.Date <= DateTime.Parse(endDate).Date);
                }
                if ((limit != 0))
                {
                    aggregate = aggregate.Limit(Convert.ToInt32(limit));
                }

                if (!string.IsNullOrEmpty(sort))
                {
                    aggregate = aggregate.Sort(Builders <TestCase> .Sort.Descending(x => x.Result));
                    // aggregate = aggregate.Sort(Builders<TestCase>.Sort.Descending(sort));
                    // aggregate = aggregate.Sort(Builders<TestCase>.Sort.Ascending(sort));
                }


                var results = await aggregate.ToListAsync();

                return(results);
            }
            catch (Exception ex)
            {
                // log or manage the exception
                throw ex;
            }
        }
예제 #7
0
        /// <summary>
        /// Applies filtering sorting and projections on the <see cref="IExecutable{T}.Source"/>
        /// </summary>
        /// <returns>A aggregate fluent including the configuration of this executable</returns>
        public IAggregateFluent <T> BuildPipeline()
        {
            IAggregateFluent <T> pipeline = _aggregate;

            if (Sorting is not null)
            {
                pipeline = pipeline.Sort(Sorting.ToSortDefinition <T>());
            }

            if (Filters is not null)
            {
                pipeline = pipeline.Match(Filters.ToFilterDefinition <T>());
            }

            if (Projection is not null)
            {
                pipeline = pipeline.Project <T>(Projection.ToProjectionDefinition <T>());
            }

            return(pipeline);
        }
예제 #8
0
 public static IAggregateFluent <T> QuerySort <T>(this IAggregateFluent <T> cursor, ClrQuery query)
 {
     return(cursor.Sort(query.BuildSort <T>()));
 }