コード例 #1
0
        public async Task <BuildVersion[]> SelectAllVersions(int limit = -1, int skip = 0)
        {
            IAggregateFluent <BsonDocument> query = _buildCollection.Aggregate().Group(new BsonDocument("_id",
                                                                                                        new BsonDocument
            {
                new BsonElement(nameof(BuildVersion.Major), $"${nameof(Build.MajorVersion)}"),
                new BsonElement(nameof(BuildVersion.Minor), $"${nameof(Build.MinorVersion)}")
            })).Sort(new BsonDocument
            {
                new BsonElement($"_id.{nameof(BuildVersion.Major)}", -1),
                new BsonElement($"_id.{nameof(BuildVersion.Minor)}", -1)
            }).Skip(skip);

            if (limit > 0)
            {
                query = query.Limit(limit);
            }

            List <BsonDocument> grouping = await query.ToListAsync();

            return((from g in grouping
                    select new BuildVersion
            {
                Major = (uint)g["_id"].AsBsonDocument[nameof(BuildVersion.Major)].AsInt32,
                Minor = (uint)g["_id"].AsBsonDocument[nameof(BuildVersion.Minor)].AsInt32
            }).ToArray());
        }
コード例 #2
0
        public static IAggregateFluent <T> QueryLimit <T>(this IAggregateFluent <T> cursor, ClrQuery query)
        {
            if (query.Take < long.MaxValue)
            {
                cursor = cursor.Limit((int)query.Take);
            }

            return(cursor);
        }
コード例 #3
0
        public async Task <List <Build> > SelectBuildsByStringSearch(string term, int limit = -1)
        {
            IAggregateFluent <Build> query = _buildCollection.Aggregate().Match(b => b.FullBuildString != null).Match(b => b.FullBuildString != "").Match(b => b.FullBuildString.ToLower().Contains(term.ToLower()));

            if (limit > 0)
            {
                query = query.Limit(limit);
            }

            return(await query.ToListAsync());
        }
コード例 #4
0
        public async Task <string[]> SelectAllLabs(int limit = -1, int skip = 0)
        {
            IAggregateFluent <BsonDocument> query = _buildCollection.Aggregate().Group(new BsonDocument("_id", $"${nameof(Build.Lab)}")).Sort(new BsonDocument("_id", 1)).Skip(skip);

            if (limit > 0)
            {
                query = query.Limit(limit);
            }

            List <BsonDocument> grouping = await query.ToListAsync();

            return((from g in grouping
                    where !g["_id"].IsBsonNull
                    select g["_id"].AsString).ToArray());
        }
コード例 #5
0
        /// <summary>
        /// Returns the only document of the aggregate result, or the default value if the result set is empty. Throws an exception if the result set contains more than one document.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// The fluent aggregate interface.
        /// </returns>
        public async static Task <TResult> SingleOrDefaultAsync <TResult>(this IAggregateFluent <TResult> aggregate, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(aggregate, "aggregate");

            using (var cursor = await aggregate.Limit(2).ToCursorAsync(cancellationToken).ConfigureAwait(false))
            {
                if (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
                {
                    return(cursor.Current.SingleOrDefault());
                }
                else
                {
                    return(default(TResult));
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Returns the first document of the aggregate result, or the default value if the result set is empty.
        /// </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="cancellationToken">The cancellation token.</param>
        /// <returns>The fluent aggregate interface.</returns>
        public async static Task <TResult> FirstOrDefaultAsync <TDocument, TResult>(this IAggregateFluent <TDocument, TResult> source, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(source, "source");

            using (var cursor = await source.Limit(1).ToCursorAsync(cancellationToken).ConfigureAwait(false))
            {
                if (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
                {
                    return(cursor.Current.FirstOrDefault());
                }
                else
                {
                    return(default(TResult));
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// Returns the only document of the aggregate result. Throws an exception if the result set does not contain exactly one document.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// The fluent aggregate interface.
        /// </returns>
        /// <exception cref="System.InvalidOperationException">The aggregate sequence is empty.</exception>
        public async static Task <TResult> SingleAsync <TResult>(this IAggregateFluent <TResult> aggregate, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(aggregate, "aggregate");

            using (var cursor = await aggregate.Limit(2).ToCursorAsync(cancellationToken).ConfigureAwait(false))
            {
                if (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
                {
                    return(cursor.Current.Single());
                }
                else
                {
                    throw new InvalidOperationException("The source sequence is empty.");
                }
            }
        }
コード例 #8
0
        public List <TResult> Aggregate <TResult, TID>(FilterDefinition <TEntity> filter, Expression <Func <TEntity, TID> > id, Expression <Func <IGrouping <TID, TEntity>, TResult> > group, Expression <Func <TEntity, object> > sortExp = null, SortType sortType = 0, int limit = 0, int skip = 0, ReadPreference readPreference = null)
        {
            if (filter == null)
            {
                filter = Filter.Empty;
            }
            IAggregateFluent <TResult> fluent2 = IAggregateFluentExtensions.Group(CreateAggregate(filter, CreateSortDefinition(sortExp, sortType), readPreference), id, group);

            if (skip > 0)
            {
                fluent2 = fluent2.Skip(skip);
            }
            if (limit > 0)
            {
                fluent2 = fluent2.Limit(limit);
            }
            return(IAsyncCursorSourceExtensions.ToList(fluent2));
        }
コード例 #9
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;
            }
        }
コード例 #10
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <typeparam name="TID"></typeparam>
        /// <param name="filter"></param>
        /// <param name="id">$group -&gt; _id</param>
        /// <param name="group">$group</param>
        /// <param name="sortExp"></param>
        /// <param name="sortType"></param>
        /// <param name="limit"></param>
        /// <param name="skip"></param>
        /// <param name="readPreference"></param>
        /// <returns></returns>
        public List <TResult> Aggregate <TResult, TID>(FilterDefinition <TEntity> filter, Expression <Func <TEntity, TID> > id, Expression <Func <IGrouping <TID, TEntity>, TResult> > group, Expression <Func <TEntity, object> > sortExp = null, SortType sortType = SortType.Ascending, int limit = 0, int skip = 0, ReadPreference readPreference = null)
        {
            if (filter == null)
            {
                filter = Builders <TEntity> .Filter.Empty;
            }
            IAggregateFluent <TEntity> aggregate       = base.CreateAggregate(filter, base.CreateSortDefinition <TEntity>(sortExp, sortType), readPreference);
            IAggregateFluent <TResult> aggregateFluent = aggregate.Group(id, group);

            if (skip > 0)
            {
                aggregateFluent = aggregateFluent.Skip(skip);
            }
            if (limit > 0)
            {
                aggregateFluent = aggregateFluent.Limit(limit);
            }
            return(aggregateFluent.ToList(default(CancellationToken)));
        }
コード例 #11
0
        public async Task <FrontBuildGroup[]> SelectAllGroups(int limit = -1, int skip = 0)
        {
            IAggregateFluent <BsonDocument> query = _buildCollection.Aggregate().Group(new BsonDocument
            {
                new BsonElement("_id",
                                new BsonDocument
                {
                    new BsonElement(nameof(BuildGroup.Major), $"${nameof(Build.MajorVersion)}"),
                    new BsonElement(nameof(BuildGroup.Minor), $"${nameof(Build.MinorVersion)}"),
                    new BsonElement(nameof(BuildGroup.Build), $"${nameof(Build.Number)}"),
                    new BsonElement(nameof(BuildGroup.Revision), $"${nameof(Build.Revision)}")
                }),
                new BsonElement("date", new BsonDocument("$max", $"${nameof(Build.BuildTime)}")),
                new BsonElement("count", new BsonDocument("$sum", 1))
            }).Sort(new BsonDocument
            {
                new BsonElement($"_id.{nameof(BuildGroup.Major)}", -1),
                new BsonElement($"_id.{nameof(BuildGroup.Minor)}", -1),
                new BsonElement($"_id.{nameof(BuildGroup.Build)}", -1),
                new BsonElement($"_id.{nameof(BuildGroup.Revision)}", -1)
            }).Skip(skip);

            if (limit > 0)
            {
                query = query.Limit(limit);
            }

            List <BsonDocument> grouping = await query.ToListAsync();

            return((from g in grouping
                    select new FrontBuildGroup
            {
                Key = new BuildGroup
                {
                    Major = (uint)g["_id"].AsBsonDocument[nameof(BuildGroup.Major)].AsInt32,
                    Minor = (uint)g["_id"].AsBsonDocument[nameof(BuildGroup.Minor)].AsInt32,
                    Build = (uint)g["_id"].AsBsonDocument[nameof(BuildGroup.Build)].AsInt32,
                    Revision = (uint?)g["_id"].AsBsonDocument[nameof(BuildGroup.Revision)].AsNullableInt32
                },
                LastBuild = g["date"].ToNullableUniversalTime(),
                BuildCount = g["count"].AsInt32
            }).ToArray());
        }
コード例 #12
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <typeparam name="TID"></typeparam>
        /// <param name="filter"></param>
        /// <param name="group"></param>
        /// <param name="sortExp"></param>
        /// <param name="sortType"></param>
        /// <param name="limit"></param>
        /// <param name="skip"></param>
        /// <param name="readPreference"></param>
        /// <returns></returns>
        public Task <List <TResult> > AggregateAsync <TResult, TID>(FilterDefinition <TEntity> filter, ProjectionDefinition <TEntity, TResult> group, Expression <Func <TEntity, object> > sortExp = null, SortType sortType = SortType.Ascending, int limit = 0, int skip = 0, ReadPreference readPreference = null)
        {
            if (filter == null)
            {
                filter = Builders <TEntity> .Filter.Empty;
            }
            IAggregateFluent <TEntity> aggregateFluent  = base.CreateAggregate(filter, base.CreateSortDefinition <TEntity>(sortExp, sortType), readPreference);
            IAggregateFluent <TResult> aggregateFluent2 = aggregateFluent.Group <TResult>(group);

            if (skip > 0)
            {
                aggregateFluent2 = aggregateFluent2.Skip(skip);
            }
            if (limit > 0)
            {
                aggregateFluent2 = aggregateFluent2.Limit(limit);
            }
            return(aggregateFluent2.ToListAsync(default(CancellationToken)));
        }
コード例 #13
0
        public async Task <int[]> SelectAllYears(int limit = -1, int skip = 0)
        {
            IAggregateFluent <BsonDocument> query =
                _buildCollection.Aggregate()
                .Match(Builders <Build> .Filter.Ne(b => b.BuildTime, null))
                .Group(new BsonDocument("_id", new BsonDocument("$year", $"${nameof(Build.BuildTime)}")))
                .Sort(new BsonDocument("_id", -1))
                .Skip(skip);

            if (limit > 0)
            {
                query = query.Limit(limit);
            }

            List <BsonDocument> grouping = await query.ToListAsync();

            return((from g in grouping
                    where !g["_id"].IsBsonNull
                    select g["_id"].AsInt32).ToArray());
        }
コード例 #14
0
        public Task <List <TProjection> > AggregateAsync <TProjection>(Expression <Func <TEntity, bool> > filterExp, ProjectionDefinition <TEntity, TProjection> group, Expression <Func <TEntity, object> > sortExp = null, SortType sortType = SortType.Ascending, int limit = 0, int skip = 0, ReadPreference readPreference = null)
        {
            FilterDefinition <TEntity> filter = null;

            if (filterExp != null)
            {
                filter = Filter.Where(filterExp);
            }
            else
            {
                filter = Filter.Empty;
            }
            IAggregateFluent <TProjection> fluent2 = CreateAggregate(filterExp, CreateSortDefinition(sortExp, sortType), readPreference).Group(group);

            if (skip > 0)
            {
                fluent2 = fluent2.Skip(skip);
            }
            if (limit > 0)
            {
                fluent2 = fluent2.Limit(limit);
            }
            return(IAsyncCursorSourceExtensions.ToListAsync(fluent2));
        }
コード例 #15
0
 public IMongoPagingContainer <TEntity> Take(int take)
 {
     return(new AggregateFluentPagingContainer <TEntity>(_source.Limit(take)));
 }
コード例 #16
0
        /// <summary>
        /// Returns the only document of the aggregate result, or the default value if the result set is empty. Throws an exception if the result set contains more than one document.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// The fluent aggregate interface.
        /// </returns>
        public static Task <TResult> SingleOrDefaultAsync <TResult>(this IAggregateFluent <TResult> aggregate, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(aggregate, nameof(aggregate));

            return(IAsyncCursorSourceExtensions.SingleOrDefaultAsync(aggregate.Limit(2), cancellationToken));
        }
コード例 #17
0
        /// <summary>
        /// Returns the first document of the aggregate result, or the default value if the result set is empty.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// The fluent aggregate interface.
        /// </returns>
        public static TResult FirstOrDefault <TResult>(this IAggregateFluent <TResult> aggregate, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(aggregate, nameof(aggregate));

            return(IAsyncCursorSourceExtensions.FirstOrDefault(aggregate.Limit(1), cancellationToken));
        }
コード例 #18
0
        /// <summary>
        /// Returns the first document of the aggregate result, or the default value if the result set is empty.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// The fluent aggregate interface.
        /// </returns>
        public static Task <TResult> FirstOrDefaultAsync <TResult>(this IAggregateFluent <TResult> aggregate, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(aggregate, "aggregate");

            return(AsyncCursorHelper.FirstOrDefaultAsync(aggregate.Limit(1).ToCursorAsync(cancellationToken), cancellationToken));
        }
コード例 #19
0
        /// <summary>
        /// Returns the only document of the aggregate result, or the default value if the result set is empty. Throws an exception if the result set contains more than one document.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// The fluent aggregate interface.
        /// </returns>
        public static Task <TResult> SingleOrDefaultAsync <TResult>(this IAggregateFluent <TResult> aggregate, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(aggregate, nameof(aggregate));

            return(AsyncCursorHelper.SingleOrDefaultAsync(aggregate.Limit(2).ToCursorAsync(cancellationToken), cancellationToken));
        }