コード例 #1
0
        /// <summary>
        /// 根据管道获取数据列表
        /// </summary>
        /// <param name="stageList"></param>
        /// <returns></returns>
        public async Task <List <BsonDocument> > GetListAggregateAsync(IList <IPipelineStageDefinition> stageList)
        {
            PipelineDefinition <BsonDocument, BsonDocument> pipeline = new PipelineStagePipelineDefinition <BsonDocument, BsonDocument>(stageList);
            var result = await collection.AggregateAsync(pipeline);

            return(await result.ToListAsync());
        }
コード例 #2
0
 public Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(
     PipelineDefinition <T, TResult> pipeline,
     AggregateOptions?options            = null,
     CancellationToken cancellationToken = default
     )
 {
     return(collection.AggregateAsync(pipeline, options, cancellationToken));
 }
コード例 #3
0
 public override Task <IAsyncCursor <TResult> > ToCursorAsync(CancellationToken cancellationToken)
 {
     if (_session == null)
     {
         return(_collection.AggregateAsync(_pipeline, _options, cancellationToken));
     }
     else
     {
         return(_collection.AggregateAsync(_session, _pipeline, _options, cancellationToken));
     }
 }
コード例 #4
0
        public Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(
            PipelineDefinition <T, TResult> pipeline,
            AggregateOptions?options            = null,
            CancellationToken cancellationToken = default)
        {
            if (TryGetSession(out IClientSessionHandle? session))
            {
                return(AggregateAsync(session, pipeline, options, cancellationToken));
            }

            return(_collection.AggregateAsync(pipeline, options, cancellationToken));
        }
コード例 #5
0
        internal override Task ExecuteAsync <TInput>(IClientSessionHandle session, IMongoCollection <TInput> collection, AggregateOptions options, CancellationToken cancellationToken)
        {
            var pipeline = CreatePipeline <TInput>();

            if (session == null)
            {
                return(collection.AggregateAsync(pipeline, options, cancellationToken));
            }
            else
            {
                return(collection.AggregateAsync(session, pipeline, options, cancellationToken));
            }
        }
コード例 #6
0
        public Task <IAsyncCursor <DailyStatsAggregation> > GetAggregatedDailyStats()
        {
            var pipeFilter = BsonDocument.Parse(@"{
                ""$project"": {
                    ""date"": { $dateToString: { ""date"": ""$date"", ""format"": ""%Y-%m-%d"", ""timezone"": ""GMT"" } },
                    ""totalMinutesTracked"": ""$totalMinutesTracked"",
                    ""minutesAtHome"": ""$locationTracking.minutesAtHome"",
                    ""boundingBoxDiagonal"": ""$boundingBoxDiagonal""
                }
            }");
            var pipeGroup  = BsonDocument.Parse(@"{
                ""$group"": {
                    ""_id"": ""$date"",
                    ""count"": { ""$sum"": 1 },
                    ""avgMinutesTracked"": { ""$avg"": ""$totalMinutesTracked"" },
                    ""totalMinutesTracked"": { ""$sum"": ""$totalMinutesTracked"" },
                    ""avgMinutesAtHome"": { ""$avg"": ""$minutesAtHome"" },
                    ""totalMinutesAtHome"": { ""$sum"": ""$minutesAtHome"" },
                    ""avgBoundingBoxDiagonal"": { ""$avg"": ""$boundingBoxDiagonal"" }
                }
            }");
            var pipeSort   = BsonDocument.Parse(@"{
                ""$sort"": {
                    ""_id"": 1
                }
            }");

            return(DailyStats.AggregateAsync <DailyStatsAggregation>(new[] { pipeFilter, pipeGroup, pipeSort }));
        }
コード例 #7
0
 protected override List <BsonDocument> ExecuteAndGetResult(IMongoDatabase database, IMongoCollection <BsonDocument> collection, bool async)
 {
     if (collection == null)
     {
         if (async)
         {
             var cursor = database.AggregateAsync <BsonDocument>(_stages, _options).GetAwaiter().GetResult();
             return(cursor.ToListAsync().GetAwaiter().GetResult());
         }
         else
         {
             return(database.Aggregate <BsonDocument>(_stages, _options).ToList());
         }
     }
     else
     {
         if (async)
         {
             var cursor = collection.AggregateAsync <BsonDocument>(_stages, _options).GetAwaiter().GetResult();
             return(cursor.ToListAsync().GetAwaiter().GetResult());
         }
         else
         {
             return(collection.Aggregate <BsonDocument>(_stages, _options).ToList());
         }
     }
 }
コード例 #8
0
        public async Task <String> GetCommonLevelBetweenPlayers()
        {
            var pipeline = new BsonDocument[] {
                new BsonDocument {
                    { "$project", new BsonDocument("Level", 1) }
                },
                new BsonDocument {
                    { "$group", new BsonDocument {
                          { "_id", "$Level" }, { "Count", new BsonDocument("$sum", 1) }
                      } }
                },
                new BsonDocument {
                    { "$sort", new BsonDocument("Count", -1) }
                },
                new BsonDocument {
                    { "$limit", 3 }
                }
            };
            var result = await Collection.AggregateAsync <BsonDocument>(pipeline);

            BsonValue value;
            BsonValue count;
            var       resList = result.ToList();

            resList[0].TryGetValue("Count", out count);
            resList[0].TryGetValue("_id", out value);
            return("Most common level is " + value + " with count of " + count);
        }
コード例 #9
0
 protected override async Task <List <BsonDocument> > ExecuteAndGetResultAsync(IMongoCollection <BsonDocument> collection)
 {
     using (var cursor = await collection.AggregateAsync <BsonDocument>(_stages, _options))
     {
         return(await cursor.ToListAsync());
     }
 }
コード例 #10
0
        public async Task <List <dynamic> > CountVotes(string electionId)
        {
            var match = new BsonDocument
            {
                {
                    "$match",
                    new BsonDocument
                    {
                        { "electionId", electionId }
                    }
                }
            };

            var group = new BsonDocument
            {
                {
                    "$group",
                    new BsonDocument
                    {
                        { "_id",
                          new BsonDocument
                          {
                              { "candidateId", "$candidateId" },
                              { "electionId", "$electionId" }
                          } },
                        { "constituencyId",
                          new BsonDocument
                          {
                              { "$first", "$constituencyId" }
                          } },
                        { "count",
                          new BsonDocument
                          {
                              { "$sum", 1 }
                          } }
                    }
                }
            };

            var project = new BsonDocument
            {
                {
                    "$project",
                    new BsonDocument
                    {
                        { "_id", 0 },
                        { "candidateId", "$_id.candidateId" },
                        { "constituencyId", "$constituencyId" },
                        { "electionId", "$_id.electionId" },
                        { "totalVotes", "$count" }
                    }
                }
            };

            var pipeline = new[] { match, group, project };
            var result   = await _votes.AggregateAsync <dynamic>(pipeline);

            return(result.ToList());
        }
コード例 #11
0
        internal override Task ExecuteAsync <TInput>(IMongoCollection <TInput> collection, AggregateOptions options, CancellationToken cancellationToken)
        {
            var pipeline = new BsonDocumentStagePipelineDefinition <TInput, TOutput>(
                _stages,
                _outputSerializer);

            return(collection.AggregateAsync(pipeline, options, cancellationToken));
        }
コード例 #12
0
        public async Task <OperationResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            try
            {
                using var cursor = _session == null
                    ? await _collection.AggregateAsync(_pipeline, _options, cancellationToken)
                    : await _collection.AggregateAsync(_session, _pipeline, _options, cancellationToken);

                var result = await cursor.ToListAsync(cancellationToken);

                return(OperationResult.FromResult(new BsonArray(result)));
            }
            catch (Exception exception)
            {
                return(OperationResult.FromException(exception));
            }
        }
コード例 #13
0
        public override async Task <OperationResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            var pipelineDefinition = new BsonDocumentStagePipelineDefinition <BsonDocument, BsonDocument>(_pipeline, BsonDocumentSerializer.Instance);

            try
            {
                using var cursor = _session == null
                    ? await _collection.AggregateAsync(pipelineDefinition, _options, cancellationToken)
                    : await _collection.AggregateAsync(_session, pipelineDefinition, _options, cancellationToken);

                var result = await cursor.ToListAsync(cancellationToken);

                return(OperationResult.FromResult(new BsonArray(result)));
            }
            catch (Exception exception)
            {
                return(OperationResult.FromException(exception));
            }
        }
コード例 #14
0
        private async Task <int> GetTotalAsync(IMongoCollection <BsonDocument> parts,
                                               string parentId, string tag)
        {
            #region MongoDB query
            // db.getCollection("parts").aggregate(
            //   [
            //     {
            //       "$match" : {
            //         "typeId" : "net.fusisoft.hierarchy",
            //         "$and" : [
            //           {
            //             "content.parentId" : "..."
            //           },
            //           {
            //             "$and" : [
            //               {
            //                 "content.tag" : null
            //               }
            //             ]
            //           }
            //         ]
            //       }
            //     },
            //     {
            //       "$count" : "count"
            //     }
            //   ],
            //   {
            //     "allowDiskUse" : false
            //   }
            // );
            #endregion

            AggregateOptions aggOptions = new AggregateOptions()
            {
                AllowDiskUse = false
            };

            PipelineDefinition <BsonDocument, BsonDocument> pipeline = new BsonDocument[]
            {
                BuildMatchStage(parentId, tag),
                new BsonDocument("$count", "count")
            };

            using (var cursor = await parts.AggregateAsync(pipeline, aggOptions))
            {
                await cursor.MoveNextAsync();

                BsonDocument first = cursor.Current.FirstOrDefault();
                return(first?["count"].AsInt32 ?? 0);
            }
        }
コード例 #15
0
        /// <summary>
        ///     Execute an aggregation.
        /// </summary>
        /// <param name="operation">The operation</param>
        /// <typeparam name="TProjectionModel">The model for the projection</typeparam>
        /// <typeparam name="TResult">The final result type of the operation</typeparam>
        /// <returns></returns>
        /// <exception cref="NullCallistoOperationException"></exception>
        public async Task <TResult> Aggregate <TProjectionModel, TResult>(ICallistoAggregation <T, TProjectionModel, TResult> operation)
            where TProjectionModel : class
        {
            Helper.PreExecutionCheck(operation);
            if (operation.ClientSessionHandle is null)
            {
                using (IAsyncCursor <TProjectionModel> cursor = await _collection.AggregateAsync(operation.PipelineDefinition,
                                                                                                 operation.AggregateOptions,
                                                                                                 operation.CancellationToken))
                {
                    return(operation.ResultFunction(cursor));
                }
            }

            using (IAsyncCursor <TProjectionModel> cursor = await _collection.AggregateAsync(operation.ClientSessionHandle,
                                                                                             operation.PipelineDefinition,
                                                                                             operation.AggregateOptions,
                                                                                             operation.CancellationToken))
            {
                return(operation.ResultFunction(cursor));
            }
        }
コード例 #16
0
        /// <summary>
        /// Executes the aggregate operation and returns a cursor.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>An asynchronous enumerable.</returns>
        public Task <IAsyncCursor <TResult> > ToCursorAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var options = new AggregateOptions <TResult>
            {
                AllowDiskUse     = _options.AllowDiskUse,
                BatchSize        = _options.BatchSize,
                MaxTime          = _options.MaxTime,
                ResultSerializer = _resultSerializer,
                UseCursor        = _options.UseCursor
            };

            return(_collection.AggregateAsync(_pipeline, options, cancellationToken));
        }
コード例 #17
0
        async public Task <List <ExtendedOrder> > getAdminOrderPage(string kw)
        {
            var queryDefinition = new BsonDocument[]
            {
                new BsonDocument("$lookup",
                                 new BsonDocument
                {
                    { "from", "houses" },
                    { "localField", "houseId" },
                    { "foreignField", "_id" },
                    { "as", "house" }
                }),
                new BsonDocument("$unwind",
                                 new BsonDocument
                {
                    { "path", "$house" },
                    { "preserveNullAndEmptyArrays", true }
                }),
                new BsonDocument("$match",
                                 new BsonDocument
                {
                    { "house.name", new BsonRegularExpression(kw, "i") }
                }
                                 )
            };

            var ordersView = await _orders.AggregateAsync(
                PipelineDefinition <Order, ExtendedOrder>
                .Create(
                    queryDefinition
                    )
                );


            List <ExtendedOrder> list = await ordersView.ToListAsync();

            return(list);
        }
コード例 #18
0
ファイル: Program.cs プロジェクト: asveredyuk/LogExplorer
        /// <summary>
        /// Function for tasks, makes map for given trace part
        /// </summary>
        /// <param name="pipeline"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        static async Task <Dictionary <string, long> > TaskFunc(PipelineDefinition <LogTrace, BsonDocument> pipeline, IMongoCollection <LogTrace> data)
        {
            var bsonRes = (await data.AggregateAsync(pipeline)).ToList();
            var traces  = bsonRes.Select(t => ConvertTrace(t).OrderBy(q => q.index));

            Dictionary <string, long> submap = new Dictionary <string, long>();

            foreach (var trace in traces)
            {
                MakeRelations(trace, submap);
                //MergeIn(dict);
            }

            return(submap);
        }
コード例 #19
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>());
        }
コード例 #20
0
        public async Task <ProductsStatistics> GetStatistics()
        {
            var pipeline = new BsonDocument[]
            {
                new BsonDocument
                {
                    {
                        "$group",
                        new BsonDocument
                        {
                            { "_id", BsonNull.Value },
                            {
                                "averageProductPrice",
                                new BsonDocument
                                {
                                    { "$avg", "$Price" }
                                }
                            },
                            {
                                "numberOfAllProducts",
                                new BsonDocument
                                {
                                    { "$sum", "$Quantity" }
                                }
                            },
                            {
                                "numberOfDifferentProducts",
                                new BsonDocument
                                {
                                    { "$sum", 1 }
                                }
                            }
                        }
                    }
                }
            };

            var resultBson = (await _collection.AggregateAsync <BsonDocument>(pipeline)).Single();
            var result     = Utilities.ToDynamic(resultBson);

            return(new ProductsStatistics
            {
                NumberOfAllProducts = result.numberOfAllProducts,
                NumberOfDifferentProducts = result.numberOfDifferentProducts,
                AverageProductPrice = result.averageProductPrice
            });
        }
コード例 #21
0
        public async Task <List <ParkModel> > FindNear(double longitude, double latitude, double threshold)
        {
            var pipeline = new List <BsonDocument>()
            {
                new BsonDocument("$geoNear", new BsonDocument {
                    { "near", new BsonDocument
                      {
                          { "type", "Point" },
                          { "coordinates", new BsonArray {
                            longitude, latitude
                        } }
                      } },
                    { "distanceField", "distance" },
                    { "maxDistance", threshold },
                    { "spherical", true }
                })
            };

            return((await _collection.AggregateAsync((PipelineDefinition <ParkModel, ParkModel>)pipeline)).ToList());
        }
コード例 #22
0
        internal async Task <IEnumerable <BsonDocument> > ExecuteAggregation(
            string collectionName,
            string name,
            CancellationToken cancellationToken)
        {
            PipelineDefinition <BsonDocument, BsonDocument> pipeline =
                AggregationPipelineFactory.Create(name);

            IMongoCollection <BsonDocument> collection = Database
                                                         .GetCollection <BsonDocument>(collectionName);

            IAsyncCursor <BsonDocument> cursor = await collection.AggregateAsync(
                pipeline,
                options : null,
                cancellationToken);

            List <BsonDocument> documents = await cursor.ToListAsync(cancellationToken);

            return(documents);
        }
コード例 #23
0
        // public methods
        public override Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            const string matchOperatorName = "$match";

            var filterStage = new DelegatedPipelineStageDefinition <TDocument, TDocument>(
                matchOperatorName,
                (s, sr) =>
            {
                var renderedFilter = _filter.Render(s, sr);
                return(new RenderedPipelineStageDefinition <TDocument>(matchOperatorName, new BsonDocument(matchOperatorName, renderedFilter), s));
            });

            var filterPipeline   = new PipelineStagePipelineDefinition <TDocument, TDocument>(new[] { filterStage });
            var combinedPipeline = new CombinedPipelineDefinition <TDocument, TDocument, TResult>(
                filterPipeline,
                pipeline);

            var optimizedPipeline = new OptimizingPipelineDefinition <TDocument, TResult>(combinedPipeline);

            return(_wrappedCollection.AggregateAsync(optimizedPipeline, options, cancellationToken));
        }
コード例 #24
0
        async Task <List <Post> > GetFeed(IMongoCollection <Feed> collection, string userId, int skip, int limit)
        {
            PipelineDefinition <Feed, Feed> pipeline = new[]
            {
                new BsonDocument {
                    { "$match", new BsonDocument {
                          { nameof(Feed.UserId), userId }
                      } }
                },
                new BsonDocument {
                    { "$unwind", "$Posts" }
                },
                new BsonDocument {
                    { "$sort", new BsonDocument {
                          { "Posts.Meta.Created", -1 }
                      } }
                },
                new BsonDocument {
                    { "$skip", skip }
                },
                new BsonDocument {
                    { "$limit", limit }
                },
                new BsonDocument {
                    { "$group", new BsonDocument {
                          { "_id", BsonNull.Value }, { "Posts", new BsonDocument("$push", "$Posts") }
                      } }
                },
                new BsonDocument {
                    { "$project", new BsonDocument {
                          { "_id", 0 }, { "Posts", 1 }
                      } }
                }
            };

            return((await collection.AggregateAsync(pipeline)).FirstOrDefault().Posts);
        }
コード例 #25
0
        public override Task <IAsyncCursor <TResult> > ToCursorAsync(CancellationToken cancellationToken)
        {
            var pipeline = new PipelineStagePipelineDefinition <TDocument, TResult>(_stages);

            return(_collection.AggregateAsync(pipeline, _options, cancellationToken));
        }
コード例 #26
0
ファイル: MongoAccess.cs プロジェクト: youlin1210/MySample
        public async Task <IAsyncCursor <TDocument> > AggregateAsync <TDocument>(string tableName, PipelineDefinition <TDocument, TDocument> pipeline, AggregateOptions options = null) where TDocument : class
        {
            IMongoCollection <TDocument> collection = db.GetCollection <TDocument>(tableName);

            return(await collection.AggregateAsync(pipeline, options));
        }
コード例 #27
0
 public Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(PipelineDefinition <T, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_Repository.AggregateAsync(pipeline, options, cancellationToken));
 }
コード例 #28
0
        public async Task <Puzzle> GetOneRandomlyAsync(List <int> excludedIds, string variant, int?userId, double nearRating)
        {
            Dictionary <string, object> filterDict = new Dictionary <string, object>()
            {
                ["approved"] = true,
                ["inReview"] = false,
                ["_id"]      = new Dictionary <string, object>()
                {
                    ["$nin"] = excludedIds
                }
            };

            if (variant != "Mixed")
            {
                filterDict.Add("variant", new Dictionary <string, object>()
                {
                    ["$eq"] = variant
                });
            }
            if (userId.HasValue)
            {
                filterDict.Add("author", new Dictionary <string, object>()
                {
                    ["$ne"] = userId.Value
                });

                filterDict.Add("reviewers", new Dictionary <string, object>()
                {
                    ["$nin"] = new int[] { userId.Value }
                });
            }

            bool shouldGive1500ProvisionalPuzzle = randomProvider.RandomPositiveInt(6) == 5;

            if (shouldGive1500ProvisionalPuzzle)
            {
                filterDict.Add("rating.value", 1500d);

                Puzzle sel = await puzzleCollection.Find(new BsonDocument(filterDict)).FirstOrDefaultAsync();

                if (sel != null)
                {
                    return(sel);
                }

                filterDict.Remove("rating.value");
            }

            BsonDocument matchDoc = new BsonDocument(new Dictionary <string, object>()
            {
                ["$match"] = filterDict
            });

            BsonDocument sampleDoc = new BsonDocument(new Dictionary <string, object>()
            {
                ["$sample"] = new Dictionary <string, object>()
                {
                    ["size"] = 30
                }
            });

            List <Puzzle> selected = await(await puzzleCollection.AggregateAsync(
                                               PipelineDefinition <Puzzle, Puzzle> .Create(
                                                   matchDoc,
                                                   sampleDoc
                                                   )
                                               )).ToListAsync();

            if (!selected.Any())
            {
                return(null);
            }

            return(selected.Aggregate((x, y) => Math.Abs(x.Rating.Value - nearRating) < Math.Abs(y.Rating.Value - nearRating) ? x : y));
        }
コード例 #29
0
 public Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default)
 => _collection.AggregateAsync(_getSession(), pipeline, options, cancellationToken);
コード例 #30
0
        public override Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var filteredPipeline = CreateFilteredPipeline(pipeline);

            return(_wrappedCollection.AggregateAsync(filteredPipeline, options, cancellationToken));
        }