Esempio n. 1
0
        /// <summary>
        ///     Helper method for building the Count pipeline
        /// </summary>
        /// <returns></returns>
        private BsonDocument BuildAndRunCountPipeline(BsonDocument matchStage, BsonDocument sortStage)
        {
            var countPipeline = new[]
            {
                matchStage,
                sortStage,
                new BsonDocument("$count", "count")
            };

            return(_moviesCollection.Aggregate(PipelineDefinition <Movie, BsonDocument> .Create(countPipeline))
                   .FirstOrDefault());
        }
        private async Task <IAsyncCursor <BsonDocument> > AggregateInternal(
            string clusterId, string dbName, string collectionName, MongoAggregateRequest body)
        {
            var collection = _configuration.GetCollection(clusterId, dbName, collectionName);
            var stages     = body.Stages?.Select(s => s.ToBsonDocument().Preprocess());

            var pipelineDefinition = PipelineDefinition <BsonDocument, BsonDocument> .Create(stages);

            var aggregateOptions = new AggregateOptions {
            };

            return(await collection.AggregateAsync(pipelineDefinition, aggregateOptions));
        }
Esempio n. 3
0
        public void Facets()
        {
            var collection = new MongoClient("mongodb://localhost:27017/admin").GetDatabase("firstdb").GetCollection <Book>("book");
            var car1       = new Book()
            {
                Name = "b1",
                Tags = new System.Collections.Generic.Dictionary <string, string>()
                {
                    { "Edition", "Blah" },
                    { "Published", "2018" }
                }
            };

            collection.InsertOne(car1);

            var pipelinex = collection.Aggregate()
                            .Match(b => b.Name == "b1")
                            .Project("{Tags: { $objectToArray: \"$Tags\" }}")
                            .Unwind("Tags")
                            .SortByCount <BsonDocument>("$Tags");

            var outputx = pipelinex.ToList();
            var json    = outputx.ToJson(new JsonWriterSettings {
                Indent = true
            });


            var project     = PipelineStageDefinitionBuilder.Project <Book, BsonDocument>("{Tags: { $objectToArray: \"$Tags\" }}");
            var unwind      = PipelineStageDefinitionBuilder.Unwind <BsonDocument, BsonDocument>("Tags");
            var sortByCount = PipelineStageDefinitionBuilder.SortByCount <BsonDocument, BsonDocument>("$Tags");

            var pipeline = PipelineDefinition <Book, AggregateSortByCountResult <BsonDocument> > .Create(new IPipelineStageDefinition[] { project, unwind, sortByCount });

            // string based alternative version
            //var pipeline = PipelineDefinition<Book, BsonDocument>.Create(
            //    "{ $project :{ Tags: { $objectToArray: \"$Tags\" } } }",
            //    "{ $unwind : \"$Tags\" }",
            //    "{ $sortByCount : \"$Tags\" }");

            var facetPipeline = AggregateFacet.Create("categorizedByTags", pipeline);

            var aggregation = collection.Aggregate().Match(b => b.Name == "b1").Facet(facetPipeline);

            var listx = aggregation.ToList();
            // var outputxy = listx.Facets.ToJson(new JsonWriterSettings { Indent = true });
            var output = aggregation.Single().Facets.ToJson(new JsonWriterSettings {
                Indent = true
            });

            Console.WriteLine(output);
        }
Esempio n. 4
0
        /// <inheritdoc />
        /// <summary>
        /// Переопределение цепочки для подсчёта стоимости всех заказов и их количества для hf,jnybrf
        /// </summary>
        /// <param name="includeDeleted"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        protected override IAggregateFluent <Employee> GetAggregationFluent(bool includeDeleted = false,
                                                                            FilterDefinition <Employee> filter = null)
        {
            //Вложенный pipeline для pipeline-let lookup-а заказов в аналитике филиалов
            var pipeline = PipelineDefinition <Order, Order> .Create(new[]
            {
                PipelineStageDefinitionBuilder.Match <Order>(new BsonDocument("$expr",
                                                                              new BsonDocument("$or",
                                                                                               new BsonArray
                {
                    new BsonDocument("$eq",
                                     new BsonArray
                    {
                        "$InCourier",
                        "$$kindid"
                    }),
                    new BsonDocument("$eq",
                                     new BsonArray
                    {
                        "$Obtainer",
                        "$$kindid"
                    }),
                    new BsonDocument("$eq",
                                     new BsonArray
                    {
                        "$WasherCourier",
                        "$$kindid"
                    }),
                    new BsonDocument("$eq",
                                     new BsonArray
                    {
                        "$Distributor",
                        "$$kindid"
                    }),
                    new BsonDocument("$eq",
                                     new BsonArray
                    {
                        "$OutCourier",
                        "$$kindid"
                    })
                })))
            }
                                                                     );

            return(base.GetAggregationFluent(includeDeleted, filter)
                   .Lookup <Order, Order, List <Order>, Employee>(
                       this.Collection.Database.GetCollection <Order>("orders"), new BsonDocument("kindid", "$_id"), pipeline,
                       "Orders")
                   .Project <Employee>(ProjectDefinition));
        }
        public UnifiedUpdateManyOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter  = null;
            UpdateOptions                   options = null;
            IClientSessionHandle            session = null;
            UpdateDefinition <BsonDocument> update  = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = argument.Value.AsBsonDocument;
                    break;

                case "hint":
                    options ??= new UpdateOptions();
                    options.Hint = argument.Value;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                case "update":
                    switch (argument.Value)
                    {
                    case BsonDocument:
                        update = argument.Value.AsBsonDocument;
                        break;

                    case BsonArray:
                        update = PipelineDefinition <BsonDocument, BsonDocument> .Create(argument.Value.AsBsonArray.Cast <BsonDocument>());

                        break;

                    default:
                        throw new FormatException($"Invalid BulkWrite Update model update argument: '{argument.Value}'.");
                    }
                    break;

                default:
                    throw new FormatException($"Invalid UpdateManyOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedUpdateManyOperation(session, collection, filter, update, options));
        }
Esempio n. 6
0
        /// <summary>
        /// Return top N ordered records
        /// </summary>
        /// <param name="top">Top N records to return</param>
        /// <param name="orderBy">Name of the atribute</param>
        /// <param name="ascendingOrDescending">1 = Ascending, -1 = Descending</param>
        /// <returns></returns>
        public async Task <IEnumerable <T> > ReadTopAsync(int top, string orderBy, OrderByDirection ascendingOrDescending)
        {
            var sortBy             = @"{
    $sort: {
        " + orderBy + @": " + ((int)ascendingOrDescending).ToString() + @"
    }
}";
            var pipelineDefinition = PipelineDefinition <T, T>
                                     .Create(sortBy)
                                     .Limit(top);

            var asyncCursor = await _mongoCollection.AggregateAsync <T>(pipelineDefinition);

            return(asyncCursor.ToEnumerable <T>());
        }
        public void Lookup_expressive_should_generate_the_correct_lookup_when_using_lambdas()
        {
            RequireServer.Check().Supports(Feature.AggregateLet);

            var subject = CreateSubject()
                          .Lookup <Person, NameMeaning, NameMeaning, IEnumerable <NameMeaning>, LookedUpPerson>(
                CreateCollection <NameMeaning>(),
                new BsonDocument("name", "value"),
                PipelineDefinition <NameMeaning, NameMeaning> .Create("{}"),
                person => person.Meanings);

            var expectedLookup = BsonDocument.Parse("{ $lookup : { from : 'NameMeaning', let : { 'name' : 'value' }, pipeline : [ { } ], as : 'Meanings' } }");

            AssertLast(subject, expectedLookup);
        }
Esempio n. 8
0
    /// <summary>
    /// Returns entity count.
    /// </summary>
    /// <returns></returns>
    public async Task <int> GetCountAsync(Expression <Func <TEntity, bool> > filterExpression = null)
    {
        var filter = filterExpression ?? Builders <TEntity> .Filter.Empty;

        var countFacet = AggregateFacet.Create("totalDataCount", PipelineDefinition <TEntity, AggregateCountResult> .Create(new[]
        {
            PipelineStageDefinitionBuilder.Count <TEntity>()
        }));

        var aggregateFacetResult = await _collection.Aggregate().Match(filter).Facet(countFacet).ToListAsync().ConfigureAwait(false);

        var count = aggregateFacetResult.First().Facets.First(x => x.Name == "totalDataCount").Output <AggregateCountResult>()?.FirstOrDefault()?.Count ?? 0;

        return((int)count);
    }
Esempio n. 9
0
        /// <summary>
        /// Finds movies by cast members
        /// </summary>
        /// <param name="cast">The name of a cast member</param>
        /// <param name="page">The page to return</param>
        /// <param name="cancellationToken">Allows the UI to cancel an asynchronous request. Optional.</param>
        /// <returns>A MoviesByCastProjection object</returns>
        public async Task <MoviesByCastProjection> GetMoviesCastFacetedAsync(string cast, int page = 0,
                                                                             CancellationToken cancellationToken = default)
        {
            // I match movies by cast members
            var matchStage = new BsonDocument("$match",
                                              new BsonDocument("cast",
                                                               new BsonDocument("$in",
                                                                                new BsonArray {
                cast
            })));

            //I limit the number of results
            var limitStage = new BsonDocument("$limit", DefaultMoviesPerPage);

            //I sort the results by the number of reviewers, descending
            var sortStage = new BsonDocument("$sort",
                                             new BsonDocument("tomatoes.viewer.numReviews", -1));

            // In conjunction with limitStage, I enable pagination
            var skipStage = new BsonDocument("$skip", DefaultMoviesPerPage * page);

            // I build the facets
            var facetStage = BuildFacetStage();

            // I am the pipeline that runs all of the stages
            var pipeline = new[]
            {
                matchStage,
                sortStage,
                // add the remaining stages in the correct order
                skipStage,
                limitStage,
                facetStage
            };

            // I run the pipeline you built
            var result = await _moviesCollection
                         .Aggregate(PipelineDefinition <Movie, MoviesByCastProjection> .Create(pipeline))
                         .FirstOrDefaultAsync(cancellationToken);

            // We build another pipeline here to count the number of movies that match _without_ the
            // limit, skip, and facet stages
            var count = BuildAndRunCountPipeline(matchStage, sortStage);

            result.Count = (int)count.Values.First();

            return(result);
        }
        public IEnumerable <T> GetLatestStatusRecordForEachLocation()
        {
            // get a list of the latest status replies for monitor, location, and name
            var pipeline = PipelineDefinition <T, T> .Create(
                "{ $sort: { LastStatusUpdateTime: -1 } }",
                "{ $addFields: { LocationAndMonitor: {$concat: [\"$MonitorName\",\"-\",\"$LocationId\",\"-\", \"$DisplayName\"]} } }",
                "{ $group: { \"_id\": \"$LocationAndMonitor\", \"latest\": { $first: \"$$ROOT\" } } }",
                "{ $replaceRoot: { newRoot: \"$latest\" } }",
                "{ $sort: { LocationId: 1, MonitorName: 1, DisplayName: 1 } }",
                "{ $project: {\"LocationAndMonitor\": 0} }"
                );

            var results = _replies.Aggregate(pipeline);

            return(results.ToList());
        }
        public IEnumerable <Alarm> GetByDevice(string deviceId, DeviceAlarmFilter filter, int rowsPerPage, int pageNumber)
        {
            IEnumerable <BsonDocument> pipeline = filter.SkipedAlarmsPipeline(rowsPerPage, pageNumber);

            PipelineDefinition <Site, BsonDocument> pipelineDefinition = PipelineDefinition <Site, BsonDocument> .Create(pipeline);

            return(Iterate <IList <Alarm> >(pipelineDefinition, (e, alarms) =>
            {
                if (alarms == null)
                {
                    alarms = new List <Alarm>();
                }
                alarms.Add(JsonConvert.DeserializeObject <BsonAlarmRoot>(e).Alarms);
                return alarms;
            }));
        }
        public async Task <int> GetHighestTagNumberAsync()
        {
            var result = await MongoCollection
                         .AggregateAsync(
                PipelineDefinition <Resident, BsonDocument> .Create(
                    new BsonDocument("$unwind", "$tags"),
                    new BsonDocument(
                        "$group", new BsonDocument
            {
                { "_id", "$_id" },
                { "max", new BsonDocument("$max", "$tags") }
            }),
                    new BsonDocument("$sort", new BsonDocument("max", -1)),
                    new BsonDocument("$limit", 1)));

            return(result.Single()["max"].AsInt32);
        }
Esempio n. 13
0
        public IList <Alarm> GetCrossAlarmsByDevice(string deviceId, DateTime start, DateTime end)
        {
            IEnumerable <BsonDocument> pipelineDef = GetCrossAlarmPipeline(deviceId, start, end);
            PipelineDefinition <Site, BsonDocument> pipelineDefinition = PipelineDefinition <Site, BsonDocument> .Create(pipelineDef);

            return(BsonIterator.Iterate(collection, pipelineDefinition, (BsonDocument e, IList <Alarm> items) =>
            {
                if (items == null)
                {
                    items = new List <Alarm>();
                }

                items.Add(BsonSerializer.Deserialize <SingleBsonItem <Alarm> >(e).Item);

                return items;
            }));
        }
Esempio n. 14
0
        public void Facet_with_3_facets_should_return_expected_result()
        {
            RequireServer.Check();
            EnsureTestData();
            var collection = __database.GetCollection <BsonDocument>(__collectionNamespace.CollectionName);
            var subject    = collection.Aggregate();
            var name1      = "categorizedByTags";
            var pipeline1  = PipelineDefinition <BsonDocument, BsonDocument> .Create(
                "{ $unwind : \"$tags\" }",
                "{ $sortByCount : \"$tags\" }");

            var name2     = "categorizedByYears";
            var pipeline2 = PipelineDefinition <BsonDocument, BsonDocument> .Create(
                "{ $match : { year : { $exists : 1 } } }",
                "{ $bucket : { groupBy : \"$year\", boundaries: [ 1900, 1920, 1950 ] } }");

            var name3     = "categorizedByYears(Auto)";
            var pipeline3 = PipelineDefinition <BsonDocument, BsonDocument> .Create(
                "{ $bucketAuto : { groupBy: '$year', buckets: 4 } }");

            var facet1 = AggregateFacet.Create(name1, pipeline1);
            var facet2 = AggregateFacet.Create(name2, pipeline2);
            var facet3 = AggregateFacet.Create(name3, pipeline3);

            var result = subject.Facet(facet1, facet2, facet3).Single();

            result.Facets.Select(f => f.Name).Should().Equal("categorizedByTags", "categorizedByYears", "categorizedByYears(Auto)");
            result.Facets[0].Output <BsonDocument>().Should().BeEquivalentTo(
                BsonDocument.Parse("{ _id: 'Expressionism', count: 2 }"),
                BsonDocument.Parse("{ _id: 'painting', count: 2 }"),
                BsonDocument.Parse("{ _id: 'ukiyo-e', count: 1 }"),
                BsonDocument.Parse("{ _id: 'woodblock', count: 1 }"),
                BsonDocument.Parse("{ _id: 'Surrealism', count: 1 }"),
                BsonDocument.Parse("{ _id: 'woodcut', count: 1 }"),
                BsonDocument.Parse("{ _id: 'oil', count: 1 }"),
                BsonDocument.Parse("{ _id: 'satire', count: 1 }"),
                BsonDocument.Parse("{ _id: 'caricature', count: 1 }"));
            result.Facets[1].Output <BsonDocument>().Should().BeEquivalentTo(
                BsonDocument.Parse("{ _id: 1900, count: 1 }"),
                BsonDocument.Parse("{ _id: 1920, count: 2 }"));
            result.Facets[2].Output <BsonDocument>().Should().BeEquivalentTo(
                BsonDocument.Parse("{ _id: { min: null, max: 1902 }, count: 1 }"),
                BsonDocument.Parse("{ _id: { min: 1902, max: 1925 }, count: 1 }"),
                BsonDocument.Parse("{ _id: { min: 1925, max: 1926 }, count: 1 }"),
                BsonDocument.Parse("{ _id: { min: 1926, max: 1926 }, count: 1 }"));
        }
        public IEnumerable <Alarm> Get(DeviceAlarmFilter filter, int rowsPerPage, int pageNumber)
        {
            filter.DeviceId = string.Empty;
            IEnumerable <BsonDocument> pipeline = filter.SkipedAlarmsPipeline(rowsPerPage, pageNumber);

            PipelineDefinition <Site, BsonDocument> pipelineDefinition = PipelineDefinition <Site, BsonDocument> .Create(pipeline);

            return(BsonIterator.Iterate(collection, pipelineDefinition, (BsonDocument e, IList <Alarm> alarms) =>
            {
                if (alarms == null)
                {
                    alarms = new List <Alarm>();
                }
                alarms.Add(BsonSerializer.Deserialize <BsonAlarmRoot>(e).Alarms);
                return alarms;
            }));
        }
Esempio n. 16
0
        public void Facet_typed_with_3_facets_should_add_the_expected_stage()
        {
            var collection = __database.GetCollection <Exhibit>(__collectionNamespace.CollectionName);
            var subject    = collection.Aggregate();
            var name1      = "categorizedByTags";
            var pipeline1  = PipelineDefinition <Exhibit, CategorizedByTag> .Create(
                "{ $unwind : \"$tags\" }",
                "{ $sortByCount : \"$tags\" }");

            var facet1    = AggregateFacet.Create(name1, pipeline1);
            var name2     = "categorizedByYears";
            var pipeline2 = PipelineDefinition <Exhibit, CategorizedByYear> .Create(
                "{ $match : { year : { $exists : 1 } } }",
                "{ $bucket : { groupBy : \"$year\", boundaries: [ 1900, 1920, 1950 ] } }");

            var facet2    = AggregateFacet.Create(name2, pipeline2);
            var name3     = "categorizedByYears(Auto)";
            var pipeline3 = PipelineDefinition <Exhibit, CategorizedByYearAuto> .Create(
                "{ $bucketAuto : { groupBy: '$year', buckets: 4 } }");

            var facet3 = AggregateFacet.Create(name3, pipeline3);

            var result = subject.Facet <Exhibit, CategorizedByTagsAndYearsAndYearsAutoResults>(facet1, facet2, facet3);

            var stage = result.Stages.Single();
            var serializerRegistry = BsonSerializer.SerializerRegistry;
            var inputSerializer    = serializerRegistry.GetSerializer <Exhibit>();
            var renderedStage      = stage.Render(inputSerializer, serializerRegistry);

            renderedStage.Document.Should().Be(
                @"{
                    $facet : {
                        categorizedByTags : [
                            { $unwind : '$tags' },
                            { $sortByCount : '$tags' }
                        ],
                         categorizedByYears: [
                            { $match : { year : { $exists : 1 } } },
                            { $bucket :  { groupBy : '$year', boundaries:  [ 1900, 1920, 1950 ] } }
                        ],
                        'categorizedByYears(Auto)': [
                            { $bucketAuto : { groupBy: '$year', buckets: 4 } }
                        ]
                   }
                }");
        }
        public Dictionary <string, IEnumerable <Freeze> > GetByDevice(IEnumerable <string> devicesIds = null, DateTime?from = null)
        {
            PipelineDefinition <Freeze, BsonDocument> pipelineDefinition = PipelineDefinition <Freeze, BsonDocument> .Create(GetPipeline(devicesIds, from));

            return(BsonIterator.Iterate(collection, pipelineDefinition, (BsonDocument e, Dictionary <string, IEnumerable <Freeze> > items) =>
            {
                if (items == null)
                {
                    items = new Dictionary <string, IEnumerable <Freeze> >();
                }

                var item = BsonSerializer.Deserialize <BsonGroup <string, IEnumerable <Freeze> > >(e);
                items.Add(item.Id, item.Accumulator);

                return items;
            }));
        }
Esempio n. 18
0
        /// <summary>
        ///     Finds all movies that match the provided genres, cast, directors, and countries
        /// </summary>
        /// <returns>A simpler list of Movies</returns>
        public async Task <IReadOnlyList <Movie> > GetMoviesFilteredOrderedPaged(string[] genres,
                                                                                 string[] countries,
                                                                                 string[] directors,
                                                                                 string[] cast,
                                                                                 string sortKey    = DefaultSortKey,
                                                                                 int sortDirection = DefaultSortOrder,
                                                                                 int pageSize      = DefaultMoviesPerPage,
                                                                                 int page          = 1,
                                                                                 CancellationToken cancellationToken = default)
        {
            var pipeline = new List <BsonDocument>();

            if (genres != null && genres.Length > 0)
            {
                pipeline.Add(new BsonDocument("$match", new BsonDocument("genres", new BsonDocument("$in", new BsonArray(genres)))));
            }

            if (countries != null && countries.Length > 0)
            {
                pipeline.Add(new BsonDocument("$match", new BsonDocument("countries", new BsonDocument("$in", new BsonArray(countries)))));
            }

            if (directors != null && directors.Length > 0)
            {
                pipeline.Add(new BsonDocument("$match", new BsonDocument("directors", new BsonDocument("$in", new BsonArray(directors)))));
            }

            if (cast != null && cast.Length > 0)
            {
                pipeline.Add(new BsonDocument("$match", new BsonDocument("cast", new BsonDocument("$in", new BsonArray(cast)))));
            }

            pipeline.Add(new BsonDocument("$sort", new BsonDocument(sortKey, sortDirection)));

            pipeline.Add(new BsonDocument("$skip", pageSize * (page - 1)));

            pipeline.Add(new BsonDocument("$limit", pageSize));


            // Run the pipeline built
            var result = await _moviesCollection.Aggregate(PipelineDefinition <Movie, Movie> .Create(pipeline))
                         .ToListAsync(cancellationToken);

            return(result);
        }
Esempio n. 19
0
        public async Task <Envelope <T> > GetEnvelopeAsync <T>(IMongoCollection <T> collection, FilterDefinition <T> filter, PageParameters pageParams)
        {
            var countFacet = AggregateFacet.Create("count",
                                                   PipelineDefinition <T, AggregateCountResult>
                                                   .Create(new[] {
                PipelineStageDefinitionBuilder.Count <T> ()
            }));

            var dataFacet = AggregateFacet.Create("data",
                                                  PipelineDefinition <T, T>
                                                  .Create(new[] {
                (pageParams.IsAscend) ? PipelineStageDefinitionBuilder.Sort(Builders <T> .Sort.Ascending(pageParams.SortBy)):
                PipelineStageDefinitionBuilder.Sort(Builders <T> .Sort.Descending(pageParams.SortBy)),
                PipelineStageDefinitionBuilder.Skip <T> ((pageParams.PageNumber - 1) * pageParams.PageSize),
                PipelineStageDefinitionBuilder.Limit <T> (pageParams.PageSize)
            }));
            var aggregation = await collection.Aggregate()
                              .Match(filter)
                              .Facet(countFacet, dataFacet)
                              .ToListAsync();

            var count = (aggregation.FirstOrDefault()
                         .Facets.FirstOrDefault(x => x.Name == "count")
                         .Output <AggregateCountResult>()
                         .FirstOrDefault() ?? new AggregateCountResult(0)).Count;

            var data = aggregation.FirstOrDefault()
                       .Facets.FirstOrDefault(x => x.Name == "data")
                       .Output <T>()
                       .ToList();
            var totalPages      = (int)Match.Ceiling(count / (double)pageParams.PageSize);
            var hasPreviousPage = pageParams.PageNumber > 1;
            var hasNextPage     = pageParams.PageNumber < totalPages;
            var envelope        = new Envelope <T>()
            {
                Items     = data,
                TotalSize = count
                            TotalPages = totalPages
                                         HasPreviousPage = HasPreviousPage
                                                           HasNextPage = hasNextPage
            };

            return(envelope);
        }
    }
Esempio n. 20
0
        /// <summary>
        /// Get all distinct values of given field in logrecord
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="success"></param>
        /// <returns></returns>
        public IEnumerable <string> GetDistinctFieldValues(string fieldName, out bool success)
        {
            var data              = GetTracesCollection();
            int testNum           = 1000;
            int maxDistinctValues = 100;

            var dfvpipeline = DFVPipeline(fieldName).ToArray();
            var limit       = LimitPipelineStage(testNum);

            var testPipelineStages = new List <BsonDocument>();

            testPipelineStages.Add(limit);
            testPipelineStages.AddRange(dfvpipeline);

            var testPipeline = PipelineDefinition <LogTrace, BsonDocument> .Create(testPipelineStages);

            var testRes = data.Aggregate(testPipeline).ToList();

            if (testRes.Count > maxDistinctValues)
            {
                success = false;
                return(testRes.Select(t => t.GetValue("_id").AsString));
            }

            if (testRes.Count == 1)
            {
                if (testRes[0].GetValue("_id").IsBsonNull)
                {
                    success = false;
                    return(null);
                }
            }

            var pipeline = PipelineDefinition <LogTrace, BsonDocument> .Create(dfvpipeline);

            var res = data.Aggregate(pipeline).ToList();

            if (res.Count > maxDistinctValues)
            {
                success = false;
                return(res.Take(maxDistinctValues).Select(t => t.GetValue("_id").AsString));
            }
            success = true;
            return(res.Select(t => t.GetValue("_id").AsString));
        }
Esempio n. 21
0
        public static int GetMaxId <T>(IMongoCollection <T> collection)
        {
            var maxStage = new BsonDocument(
                "$group",
                new BsonDocument {
                { "_id", 0 },
                { "maxId", new BsonDocument("$max", "$_id") }
            }
                );
            var pipeline = PipelineDefinition <T, BsonDocument>
                           .Create(new BsonDocument[] {
                maxStage
            });

            var result = collection.Aggregate(pipeline).ToList();

            return(result.Count > 0 ? result[0].GetValue("maxId").AsInt32 : 0);
        }
Esempio n. 22
0
        public void Facet_typed_with_1_facet_should_return_expected_result()
        {
            RequireServer.Check();
            EnsureTestData();
            var collection = __database.GetCollection <Exhibit>(__collectionNamespace.CollectionName);
            var subject    = collection.Aggregate();
            var name1      = "categorizedByTags";
            var pipeline1  = PipelineDefinition <Exhibit, CategorizedByTag> .Create(
                "{ $unwind : \"$tags\" }",
                "{ $sortByCount : \"$tags\" }");

            var facet1 = AggregateFacet.Create(name1, pipeline1);

            var result = subject.Facet <Exhibit, CategorizedByTagsResults>(facet1).Single();

            result.CategorizedByTags.WithComparer(new CategorizedByTagComparer()).Should().BeEquivalentTo(
                new CategorizedByTag {
                Id = "Expressionism", Count = 2
            },
                new CategorizedByTag {
                Id = "painting", Count = 2
            },
                new CategorizedByTag {
                Id = "ukiyo-e", Count = 1
            },
                new CategorizedByTag {
                Id = "woodblock", Count = 1
            },
                new CategorizedByTag {
                Id = "Surrealism", Count = 1
            },
                new CategorizedByTag {
                Id = "woodcut", Count = 1
            },
                new CategorizedByTag {
                Id = "oil", Count = 1
            },
                new CategorizedByTag {
                Id = "satire", Count = 1
            },
                new CategorizedByTag {
                Id = "caricature", Count = 1
            });
        }
        private async IAsyncEnumerable <TResult> ExecuteModelAsync <TResult>(AggregateExecutionModel model, [EnumeratorCancellation] CancellationToken cancellationToken)
        {
            var serializer = model.Serializer as IBsonSerializer <TResult>;
            var pipeline   = PipelineDefinition <TEntity, TResult> .Create(model.Stages, serializer);

            using (var diagnostics = DiagnosticRunner.Start <TEntity>(Connection, model))
            {
                IAsyncCursor <TResult> underlyingCursor;

                try
                {
                    underlyingCursor = await GetCollection().AggregateAsync(pipeline, cancellationToken: cancellationToken);
                }
                catch (Exception exception)
                {
                    diagnostics.Error(exception);
                    throw;
                }

                var hasFirstResult = false;
                while (await underlyingCursor.MoveNextAsync(cancellationToken))
                {
                    if (!hasFirstResult)
                    {
                        hasFirstResult = true;
                        diagnostics.FirstReadResult <TResult>();
                    }

                    var resultBatch = underlyingCursor.Current;
                    foreach (var item in resultBatch)
                    {
                        if (item is TEntity entityItem &&
                            (model.ResultTransformer == null ||
                             model.ResultTransformer.ReturnType == typeof(ValueTask <TEntity>) ||
                             model.ResultTransformer.ReturnType == typeof(Task <TEntity>)))
                        {
                            EntityProcessors.ProcessEntity(entityItem, Connection);
                        }

                        yield return(item);
                    }
                }
            }
        }
        public async Task <Page <TemplateKind> > GetTemplateKindsPage(int page, int pageSize, CancellationToken cancellationToken = default)
        {
            var countFacetName    = "count";
            var entitiesFacetName = "entities";

            var countFacet = AggregateFacet.Create(countFacetName,
                                                   PipelineDefinition <TemplateKind, AggregateCountResult> .Create(new[]
            {
                PipelineStageDefinitionBuilder.Count <TemplateKind>(),
            }));

            var sortDefinition = Builders <TemplateKind> .Sort.Ascending(x => x.TemplateKindKey);

            var entitiesFacet = AggregateFacet.Create(entitiesFacetName,
                                                      PipelineDefinition <TemplateKind, TemplateKind> .Create(new[]
            {
                PipelineStageDefinitionBuilder.Sort(sortDefinition),
                PipelineStageDefinitionBuilder.Skip <TemplateKind>((page - 1) * pageSize),
                PipelineStageDefinitionBuilder.Limit <TemplateKind>(pageSize),
            }));

            var aggregation = await _collection.Aggregate()
                              .Facet(countFacet, entitiesFacet)
                              .ToListAsync(cancellationToken);

            var data = aggregation.First()
                       .Facets.First(x => x.Name == entitiesFacetName)
                       .Output <TemplateKind>();

            var countOutput = aggregation.First()
                              .Facets.First(x => x.Name == countFacetName)
                              .Output <AggregateCountResult>();

            long count = 0;

            if (countOutput.Any())
            {
                count = countOutput.First()
                        .Count;
            }

            return(new Page <TemplateKind>(count, data));
        }
Esempio n. 25
0
    /// <summary>
    /// Returns the nested list in the entity according to <paramref name="entityId"/>.
    ///
    /// <remarks>
    /// <para><b>Remarks ;</b></para>
    ///
    /// <para> We specify the nested list with <paramref name="unwindExpression"/>. </para>
    /// <para> You can send the filter value to the Nested list. See <paramref name="filterExpressionForTEmbedded"/>. </para>
    /// <para> You can get the specific properties. See <paramref name="projectExpression"/>. </para>
    /// </remarks>
    /// </summary>
    /// <typeparam name="TEmbedded"></typeparam>
    /// <param name="entityId"></param>
    /// <param name="unwindExpression"></param>
    /// <param name="filterExpressionForTEmbedded"></param>
    /// <param name="projectExpression"></param>
    /// <returns></returns>
    public async Task <List <TEmbedded> > GetNestedArrayByEntityIdAsync <TEmbedded>(ObjectId entityId,
                                                                                    Expression <Func <TEntity, object> > unwindExpression,
                                                                                    Expression <Func <TEmbedded, bool> > filterExpressionForTEmbedded = null,
                                                                                    List <Expression <Func <TEmbedded, object> > > projectExpression  = null)
    {
        var filter = filterExpressionForTEmbedded ?? Builders <TEmbedded> .Filter.Empty;

        var projectQuery = GetProjectionQuery(unwindExpression, projectExpression);

        var dataFacet = AggregateFacet.Create("matchingDatas", PipelineDefinition <TEmbedded, TEmbedded> .Create(new[]
        {
            PipelineStageDefinitionBuilder.Project <TEmbedded, TEmbedded>(BsonDocument.Parse("{" + projectQuery + "}")),
            PipelineStageDefinitionBuilder.Match(filter)
        }));

        var aggregateFacetResult = await _collection.Aggregate().Match(p => p.Id == entityId).Unwind <TEntity, TEmbedded>(unwindExpression).Facet(dataFacet).ToListAsync().ConfigureAwait(false);

        return(aggregateFacetResult.First().Facets.First(x => x.Name == "matchingDatas").Output <TEmbedded>().ToList());
    }
Esempio n. 26
0
        private static void CreateViewInDb()
        {
            //setFeatureCompatibilityVersion for view
            BsonDocument setFeatureCompatibilityVersionCmd = new BsonDocument {
                { "setFeatureCompatibilityVersion", "3.4" }
            };
            var doc = Task.Run(async() => await DataBase.RunCommandAsync <BsonDocument>(setFeatureCompatibilityVersionCmd));

            var pipeline = PipelineDefinition <BsonDocument, BsonDocument> .Create(
                new BsonDocument {
                { "$match",
                  new BsonDocument {
                      { "company_name", "cellent" }
                  } }
            });

            Task.Run(async() =>
                     await DataBase.CreateViewAsync(CollectionNames.DeveloperView, CollectionNames.Developer, pipeline));
        }
Esempio n. 27
0
        public static async Task <PagedCollection <T> > ApplyOData <T>(this IMongoCollection <T> collection, IODataQuery query)
        {
            var page  = 1;
            var skip  = query.Skip.Normalize(0);
            var limit = query.Limit.Normalize(10, 1000);

            var countFacet = AggregateFacet.Create("count",
                                                   PipelineDefinition <T, AggregateCountResult> .Create(new[]
            {
                PipelineStageDefinitionBuilder.Count <T>()
            }));

            var sort   = CreateODataSort <T>(query, false);
            var filter = CreateODataFilter <T>(query, false);

            var dataFacet = AggregateFacet.Create("data",
                                                  PipelineDefinition <T, T> .Create(new []
            {
                PipelineStageDefinitionBuilder.Sort(sort),
                PipelineStageDefinitionBuilder.Skip <T>(skip),
                PipelineStageDefinitionBuilder.Limit <T>(limit),
            }));

            var aggregation = await collection.Aggregate()
                              .Match(filter)
                              .Facet(countFacet, dataFacet)
                              .ToListAsync();

            var count = aggregation.First()
                        .Facets.First(x => x.Name == "count")
                        .Output <AggregateCountResult>()
                        .First()
                        .Count;

            int totalPages = (int)(count / limit);

            var data = aggregation.First()
                       .Facets.First(x => x.Name == "data")
                       .Output <T>();

            return(PagedCollection <T> .Create(data, page, limit, totalPages, count));
        }
Esempio n. 28
0
        public static async Task <IReadOnlyList <TDocument> > AggregateByPage <TDocument>(
            this IMongoCollection <TDocument> collection,
            FilterDefinition <TDocument> filterDefinition,
            SortDefinition <TDocument> sortDefinition,
            int page,
            int pageSize)
        {
            var countFacet = AggregateFacet.Create("count",
                                                   PipelineDefinition <TDocument, AggregateCountResult> .Create(new[]
            {
                PipelineStageDefinitionBuilder.Count <TDocument>()
            }));

            var dataFacet = AggregateFacet.Create("data",
                                                  PipelineDefinition <TDocument, TDocument> .Create(new[]
            {
                PipelineStageDefinitionBuilder.Sort(sortDefinition),
                PipelineStageDefinitionBuilder.Skip <TDocument>((page - 1) * pageSize),
                PipelineStageDefinitionBuilder.Limit <TDocument>(pageSize),
            }));


            var aggregation = await collection.Aggregate()
                              .Match(filterDefinition)
                              .Facet(countFacet, dataFacet)
                              .ToListAsync();

            var count = aggregation.First()
                        .Facets.First(x => x.Name == "count")
                        .Output <AggregateCountResult>()
                        .First()
                        .Count;

            var totalPages = (int)Math.Ceiling((double)count / pageSize);

            var data = aggregation.First()
                       .Facets.First(x => x.Name == "data")
                       .Output <TDocument>();

            return(data);
        }
Esempio n. 29
0
        public void ShouldFetchCandidateListWhenSectorWiseSearch()
        {
            IMongoDatabase database = GetMongoDatabase();
            IMongoCollection <BsonDocument> candidateCollection = database.GetCollection <BsonDocument>("Candidates");
            IMongoCollection <BsonDocument> companyCollection   = database.GetCollection <BsonDocument>("Companies");

            var sectors = new[] { "java" };

            IAggregateFluent <BsonDocument> candidateAgg = candidateCollection.Aggregate();

            var let      = new BsonDocument("compId", "$PreferredCompanies");
            var operands = new BsonArray();

            operands.Add("$$compId").Add("$_id");

            var expression = new BsonDocument("$expr", new BsonDocument("$eq", operands));

            PipelineDefinition <BsonDocument, BsonDocument> pipeline = PipelineDefinition <BsonDocument, BsonDocument> .Create(new BsonDocument("$match", expression));

            candidateAgg = candidateAgg.Lookup(companyCollection, let, pipeline, "Array");

            candidateAgg = candidateAgg.Unwind("Array");

            FilterDefinition <BsonDocument> sectorFilters = Builders <BsonDocument> .Filter.In("Array.sectors", sectors);

            candidateAgg = candidateAgg.Match(sectorFilters);

            var fields = new BsonDocument
            {
                { "_id", "$_id" },
                { "CompanyId", new BsonDocument {
                      { "$first", "$CompanyId" }
                  } }
            };

            candidateAgg = candidateAgg.Group(fields);

            IEnumerable <BsonDocument> resultList = candidateAgg.ToList();

            Assert.NotNull(resultList);
        }
Esempio n. 30
0
        private IEnumerable <TResult> ExecuteModel <TResult>(AggregateExecutionModel model)
        {
            var serializer = model.Serializer as IBsonSerializer <TResult>;
            var pipeline   = PipelineDefinition <TEntity, TResult> .Create(model.Stages, serializer);

            using (var diagnostics = DiagnosticRunner.Start <TEntity>(Connection, model))
            {
                IEnumerable <TResult> underlyingResult;

                try
                {
                    underlyingResult = GetCollection().Aggregate(pipeline).ToEnumerable();
                }
                catch (Exception exception)
                {
                    diagnostics.Error(exception);
                    throw;
                }

                using (var enumerator = underlyingResult.GetEnumerator())
                {
                    var hasFirstResult = false;
                    while (enumerator.MoveNext())
                    {
                        if (!hasFirstResult)
                        {
                            hasFirstResult = true;
                            diagnostics.FirstReadResult <TResult>();
                        }

                        var item = enumerator.Current;
                        if (item is TEntity entityItem)
                        {
                            EntityProcessors.ProcessEntity(entityItem, Connection);
                        }
                        yield return(item);
                    }
                }
            }
        }