public static List <UpcomingScheme> GetDocuments(MongoClient client, string databaseName, string collectionName) { List <UpcomingScheme> schemes = new List <UpcomingScheme>(); IMongoCollection <UpcomingScheme> upcomingCollection = ConnectDataBase(client, databaseName, collectionName); IAggregateFluent <UpcomingScheme> aggregateObject = upcomingCollection.Aggregate(); schemes = aggregateObject.ToList(); AlConsole.WriteLine(DB_INFO_C, $"Requested {databaseName, MaxIndex} | {collectionName, MaxIndex} |"); return(schemes); }
private List <FunctionRankResult> GetResultAfterQueryExecution(IAggregateFluent <BsonDocument> finalRank) { List <FunctionRankResult> result = new List <FunctionRankResult>(); if (finalRank.Any()) { var resultList = finalRank.ToList(); if (resultList != null) { result = BsonSerializer.Deserialize <List <FunctionRankResult> >(resultList.ToJson()); } } return(result); }
/// <summary> /// Get the ten less visited destinations per day /// </summary> /// <param name="month"></param> /// <returns></returns> public List <BsonDocument> SecondQuery(int day) { IAggregateFluent <BsonDocument> aggregate = _collection.Aggregate() .Match(new BsonDocument { { "DayofMonth", day.ToString() } }) .Group(new BsonDocument { { "_id", "$DestCityName" }, { "count", new BsonDocument("$sum", 1) } }) .Sort(new BsonDocument { { "count", 1 } }) .Limit(10); return(aggregate.ToList()); }
/// <summary> /// Gets the number of elements in the database /// </summary> /// <returns></returns> public List <BsonDocument> CountLines() { IAggregateFluent <BsonDocument> aggregate = _collection.Aggregate() .Group(new BsonDocument { { "_id", "$token" }, { "count", new BsonDocument("$sum", 1) } }) .Sort(new BsonDocument { { "count", -1 } }) .Limit(10); List <BsonDocument> results = aggregate.ToList(); foreach (BsonDocument obj in results) { Console.WriteLine(obj.ToString()); } return(results); }
public List <object> SkipTake <T>(IAggregateFluent <T> result, int skip, int take, out long total) { if (take > 0) { //var sortStage = result.Stages.FirstOrDefault(x => x.OperatorName == "$sort"); //var stages = result.Stages.Where(x => x.OperatorName != "$sort"); //var resultForTotal = new PipelineStagePipelineDefinition<TDocument, T>(stages); total = result.Count().FirstOrDefault()?.Count ?? 0; result = result.Skip(skip).Limit(take); } else { total = 0; } var list = result.ToList(); return(list.Cast <object>().ToList()); }
/// <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 List <TResult> Aggregate <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.ToList(default(CancellationToken))); }
private List <Object> GetResultAfterQueryExecution(IAggregateFluent <BsonDocument> finalQuery) { var result = new List <Object>(); try { if (finalQuery.Any()) { var resultList = finalQuery.ToList(); foreach (var i in resultList) { result.Add(BsonSerializer.Deserialize <object>(i)); } } } catch (Exception ex) { } return(result); }
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); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collection"></param> /// <param name="filter"></param> /// <param name="projection"></param> private void Apply <T>(IMongoCollection <T> collection, FilterDefinition <T> filter, ProjectionDefinition <T> projection) { filter = CreateFilters <T>(Filter, filter); if (IsAggregate) { var sortB = Builders <T> .Sort; SortDefinition <T> sort = null; foreach (var item in Order) { switch (item.Direction.ToString()) { case "-": { if (sort == null) { sort = sortB.Descending(item.Property); } else { sort = sort.Descending(item.Property); } break; } case "+": { if (sort == null) { sort = sortB.Ascending(item.Property); } else { sort = sort.Ascending(item.Property); } break; } } } var Group = BsonDocumentWrapper.Parse(JsonConvert.SerializeObject(GroupBy)); IAggregateFluent <T> query = null; var options = new AggregateOptions(); if (sort != null) { query = collection.Aggregate().Match(filter).Sort(sort).Group <T>(Group).Project <T>(BsonDocumentWrapper.Parse("{'id':'$_id','count':'$count'}")); } else { query = collection.Aggregate().Match(filter).Group <T>(Group).Project <T>(BsonDocumentWrapper.Parse("{'id':'$_id','count':'$count'}")); } Data = query.ToList <T>(); } else { IFindFluent <T, T> query = null; if (filter != null) { query = collection.Find(filter); } else { query = collection.Find(x => true); } if (projection != null) { query = query.Project <T>(projection); } var sortB = Builders <T> .Sort; SortDefinition <T> sort = null; foreach (var item in Order) { switch (item.Direction.ToString()) { case "-": { if (sort == null) { sort = sortB.Descending(item.Property); } else { sort = sort.Descending(item.Property); } break; } case "+": { if (sort == null) { sort = sortB.Ascending(item.Property); } else { sort = sort.Ascending(item.Property); } break; } } } if (sort != null) { query.Sort(sort); } PageSize = PageSize ?? 1; RecordCount = query.Count(); if (PageSize != int.MaxValue) { PageCount = (RecordCount / PageSize); PageCount += (PageCount < ((float)RecordCount / (float)PageSize)) ? 1 : 0; Data = query.Skip((PageIndex - 1) * PageSize).Limit(PageSize).ToList <T>(); } else { PageCount = 1; Data = query.ToList <T>(); } } }