public static void Main(string[] args) { MongoClient mc = new MongoClient("mongodb://localhost:27017"); MongoDatabase mDB = mc.GetServer().GetDatabase("test"); Console.WriteLine("Got MongoDB Database"); MongoCollection mColl = mDB.GetCollection("aggTest"); AggregateArgs aggArgs = new AggregateArgs(); aggArgs.OutputMode = AggregateOutputMode.Cursor; aggArgs.Pipeline = new [] { new BsonDocument { { "$match", new BsonDocument { { "a", new BsonInt32(1) } } } } }; Console.WriteLine("Complete AggArgs "); long rowCount = 0; IEnumerable aggResults = mColl.Aggregate(aggArgs); foreach (BsonDocument doc in aggResults) { Console.WriteLine("Fetching Agg Result Records in Cursor [" + rowCount + "]"); ++rowCount; } Console.WriteLine("Completed Agg result Scan: " + rowCount); }
public override IEnumerable <BsonDocument> Aggregate(AggregateArgs args) { var sw = new Stopwatch(); sw.Start(); var underlyingEnumerable = base.Aggregate(args); sw.Stop(); var profiledEnumerable = new ProfiledEnumerable <BsonDocument>(underlyingEnumerable); sw.Start(); var profiledEnumerator = (ProfiledEnumerator <BsonDocument>)profiledEnumerable.GetEnumerator(); sw.Stop(); profiledEnumerator.EnumerationEnded += (sender, eventArgs) => { var operationsList = args.Pipeline.ToList(); string commandString = string.Format("db.{0}.aggregate(pipeline)\n\npipeline = \n{1}", Name, string.Join("\n", operationsList.Select(operation => string.Format(" {0}", operation)))); ProfilerUtils.AddMongoTiming(commandString, (long)(eventArgs.Elapsed + sw.Elapsed).TotalMilliseconds, ExecuteType.Read); }; return(profiledEnumerable); }
public BsonDocument[] GetDistinctCompanyTypes() { var match = new BsonDocument { { "$match", new BsonDocument { } } }; var group = new BsonDocument { { "$group", new BsonDocument { { "_id", "$SubType" }, { "type", new BsonDocument { { "$first", "$Type" } } } } } }; // "_id", "$SubType", "type", new BsonDocument { "$first", "$Type" } } var pipeline = new AggregateArgs(); pipeline.Pipeline = new[] { match, group }; return(_mongoDbHandler.Collection.Aggregate(pipeline).ToArray()); }
public IAggregateFluent <BsonDocument> Run(IMongoCollection <Rental> rentals) { var priceRange = new BsonDocument( "$subtract", new BsonArray { "$Price", new BsonDocument( "$mod", new BsonArray { "$Price", 500 }) }); var grouping = new BsonDocument( "$group", new BsonDocument { { "_id", priceRange }, { "count", new BsonDocument("$sum", 1) } }); var sort = new BsonDocument( "$sort", new BsonDocument("_id", 1) ); var args = new AggregateArgs { Pipeline = new[] { grouping, sort } }; return(rentals.Aggregate().Group(grouping).Sort(sort)); }
public IEnumerable <BsonDocument> Aggregate <T>(params BsonDocument[] operators) { var args = new AggregateArgs { Pipeline = operators, OutputMode = AggregateOutputMode.Inline }; return(Aggregate <T>(args)); }
public virtual IQueryable <T1> AggregateToOutput <T1>(string aggregationPipeline, string outputCollection, dynamic parameters = null) { AggregateArgs args = ProcessPipeline(aggregationPipeline, parameters); GetCollection(CollectionName).Aggregate(args); return(GetCollection(outputCollection).FindAllAs <T1>().AsQueryable()); }
private void GetMetricValue() { if (DateTime.Now.Subtract(lastPoll).TotalSeconds > _pollingIntervalInSeconds) { _lastDelay = 0; var lastCommitDoc = _commitsCollection.FindAll() .SetSortOrder(SortBy.Descending("_id")) .SetFields(Fields.Include("_id")) .FirstOrDefault(); if (lastCommitDoc == null) { return; } var lastCommit = lastCommitDoc["_id"].AsInt64; //db.checkpoints.aggregate( //[ // {$match : {"Active": true}} // ,{$project : {"Slot" : 1, "Current" : 1}} // ,{$group : {"_id" : "$Slot", "Current" : {$min : "$Current"}}} //]) BsonDocument[] pipeline = { BsonDocument.Parse(@"{$match : {""Active"": true}}"), BsonDocument.Parse(@"{$project : {""Slot"" : 1, ""Current"" : 1}}"), BsonDocument.Parse(@"{$group : {""_id"" : ""$Slot"", ""Current"" : {$min : ""$Current""}}}") }; AggregateArgs args = new AggregateArgs(); args.Pipeline = pipeline; args.AllowDiskUse = true; //important for large file systems var allCheckpoints = _checkpointCollection.Aggregate(args); foreach (BsonDocument metric in allCheckpoints) { var slotName = metric["_id"].AsString; Int64 current; if (!metric["Current"].IsBsonNull) { current = Int64.Parse(metric["Current"].AsString); } else { current = 0; } var delay = lastCommit - current; if (delay > _lastDelay) { _lastDelay = delay; } _lastMetrics[slotName] = new SlotStatus(slotName, delay); } lastPoll = DateTime.Now; } }
public TOutput Aggregate <T, TOutput>(IEnumerable <BsonDocument> pipeline) { var pipe = new AggregateArgs(); pipe.Pipeline = pipeline; var result = CurrentCollection <T>().Aggregate(pipe); return(BsonSerializer.Deserialize <TOutput>(result.ToJson())); }
private AggregateArgs ProcessPipeline(string aggregationPipeline, dynamic parameters) { string pipeline = ReplaceParameters(parameters, aggregationPipeline); AggregateArgs args = new AggregateArgs(); args.AllowDiskUse = true; args.OutputMode = AggregateOutputMode.Cursor; args.Pipeline = BsonSerializer.Deserialize <BsonDocument[]>(pipeline); return(args); }
public virtual IQueryable <T1> Aggregate <T1>(string collectionName, string aggregationPipeline, dynamic parameters = null) { AggregateArgs args = ProcessPipeline(aggregationPipeline, parameters); List <T1> result = new List <T1>(); foreach (BsonDocument doc in GetCollection(collectionName).Aggregate(args)) { result.Add(BsonSerializer.Deserialize <T1>(doc)); } return(result.AsQueryable <T1>()); }
private void AggregateExamples() { AggregateArgs simpleAggregateArgs = new AggregateArgs() { Pipeline = new[] { new BsonDocument("$match", Query <Car> .LTE(c => c.DailyRentalFee, 10).ToBsonDocument()) , new BsonDocument("$match", Query <Car> .GTE(c => c.DailyRentalFee, 3).ToBsonDocument()) , new BsonDocument("$sort", new BsonDocument("DailyRentalFee", 0)) } }; IEnumerable <BsonDocument> documents = CarRentalContext.Cars.Aggregate(simpleAggregateArgs); }
public void AggregateExplain_should_work() { var collection = GetCollection(); var args = new AggregateArgs { Pipeline = new[] { "{ $match : { X : 1 } }" }.Select(s => BsonDocument.Parse(s)) }; var result = collection.AggregateExplain(args); result.Ok.Should().BeTrue(); result.Response.Should().NotBeNull(); }
private IEnumerable <BsonDocument> Aggregate(string collectionName, string aggregationPipeline) { MongoCollection <BsonDocument> collection = Database.GetCollection <BsonDocument>(collectionName); var aggregateArgs = new AggregateArgs(); if (!string.IsNullOrEmpty(aggregationPipeline)) { aggregateArgs.Pipeline = ParseAggregationPipeline(aggregationPipeline); } return(collection.Aggregate(aggregateArgs)); }
public void MatchMatchSort() { InsertPeople(); var matchAndSort = new AggregateArgs { Pipeline = new[] { new BsonDocument("$match", Query.LT("Age", 70).ToBsonDocument()), new BsonDocument("$match", Query.GT("Age", 30).ToBsonDocument()), new BsonDocument("$sort", new BsonDocument("Age", 1)) } }; PrintResults(matchAndSort); }
public BlobStoreInfo GetInfo() { var aggregation = new AggregateArgs() { Pipeline = new[] { BsonDocument.Parse("{$group:{_id:1, size:{$sum:'$length'}, count:{$sum:1}}}") } }; var allInfos = _fs.Values .Select(x => x.Files.Aggregate(aggregation).FirstOrDefault()) .Where(x => x != null) .Select(x => new { size = x["size"].ToInt64(), files = x["count"].ToInt64() }) .ToArray(); return(new BlobStoreInfo( allInfos.Sum(x => x.size), allInfos.Sum(x => x.files) )); }
//[Parameter] //public SwitchParameter Explain { get; set; } protected override void BeginProcessing() { var mc = TargetCollection.Collection as MongoCollection; if (mc == null) { ThrowNotImplementedForFiles("Aggregate"); } var args = new AggregateArgs() { Pipeline = _Pipeline, OutputMode = AggregateOutputMode.Cursor }; if (AllowDiskUse) { args.AllowDiskUse = true; } if (BatchSize > 0) { args.BatchSize = BatchSize; } if (MaxTime.Ticks > 0) { args.MaxTime = MaxTime; } //if (Explain) //{ // WriteObject(new Dictionary(mc.AggregateExplain(args).Response)); // return; //} var result = mc.Aggregate(args); foreach (var document in result) { WriteObject(new Dictionary(document)); } }
public void MatchGroupSort() { InsertPeople(); var aggregation = new AggregateArgs { Pipeline = new[] { new BsonDocument("$match", Query.LT("Age", 70).ToBsonDocument()), new BsonDocument("$group", new BsonDocument { { "_id", new BsonDocument("$subtract", new BsonArray { "$Age", new BsonDocument("$mod", new BsonArray { "$Age", 10 }) }) }, { "averageAge", new BsonDocument("$avg", "$Age") } }), new BsonDocument("$sort", new BsonDocument("_id", 1)) } }; PrintResults(aggregation); }
public IEnumerable <BsonDocument> Aggregate <T>(AggregateArgs args) { return(GetCollection <T>().Aggregate(args)); }
private void PrintResults(AggregateArgs matchAndSort) { QueryTests.Aggregate(matchAndSort) .ToList() .ForEach(Console.WriteLine); }
public virtual IEnumerable <BsonDocument> Aggregation(AggregateArgs args) { return(this.context.GetCollection <TEntity>().Aggregate(args)); }
public IEnumerable <BsonDocument> Aggregate(AggregateArgs aggregateArgs) { return(_storage.Aggregate(aggregateArgs)); }
// // GET: /ChanncelPower/ public ActionResult Index(SeeAboutModel model, int pageid = 1) { List <IMongoQuery> queries = new List <IMongoQuery>(); queries.Add(Query.NE("", new BsonString(""))); //queries.Add(Query.EQ("status", new BsonString("待审核"))); if (model != null) { if (!string.IsNullOrEmpty(model.civetno)) { queries.Add(Query.Or(Query.EQ("civetno", new BsonString(model.civetno)), Query.EQ("user_name", new BsonString(model.civetno)))); } if (!string.IsNullOrEmpty(model.serviceno_uid)) { queries.Add(Query.EQ("_id", new BsonString(model.serviceno_uid))); } } else { model = new SeeAboutModel(); } ViewBag.SearchCondition = model; PagedList <ModChanncelList> result = mongoh.GetCollection <ModChanncelList>().FindByAnd(queries) .MOrderByDescending(m => m.start_date).MToPagedList(pageid, 20); if (result.Count != 0) { //获得本次显示的所有频道号 List <string> ids = new List <string>(); foreach (ModChanncelList item in result) { ids.Add(item._id); } #region 查询统计数据 BsonDocument match = new BsonDocument { { "$match", new BsonDocument { { "serviceno_uid", new BsonDocument("$in", new BsonArray(ids.ToArray())) } } } }; DateTime MonthlyFirstDate = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-01")); List <object> objarray = new List <object>(); objarray.Add("$visited_time"); objarray.Add(new BsonDateTime(MonthlyFirstDate)); BsonDocument group = new BsonDocument { { "$group", new BsonDocument { { "_id", "$serviceno_uid" }, { "MonthlyNum", new BsonDocument { { "$sum", new BsonDocument { { "$cond", new BsonDocument { { "if", new BsonDocument("$gte", new BsonArray(objarray.ToArray())) } , { "then", 1 } , { "else", 0 } } } } } } }, { "TotalNum", new BsonDocument { { "$sum", 1 } } } } } }; AggregateArgs args = new AggregateArgs(); args.Pipeline = new BsonDocument[] { match, group }; var statResult = mongoh.GetCollection <ModUApiUseLogFind>().Aggregate(args).ToList(); #endregion foreach (BsonDocument item in statResult) { string uid = item["_id"].AsString; int monthlyNum = item["MonthlyNum"].AsInt32; int totalNum = item["TotalNum"].AsInt32; ModChanncelList oneDate = (from m in result where m._id == uid select m).FirstOrDefault(); if (oneDate != null) { oneDate.MonthlyNum = monthlyNum; oneDate.TotalNum = totalNum; } } } return(View(result)); }
void Start() { AnnotationData[] dataArr = new AnnotationData[5]; //sampledata for (int i = 0; i < 5; i++) //populate sample data { dataArr[i].timestamp = 54386223 + i; dataArr[i].annotationData = new AnnotationData.ArrayEntry[5]; for (int j = 0; j < 5; j++) { dataArr[i].annotationData[j].xleft = 0.98765 + j; dataArr[i].annotationData[j].xright = 0.98765 + j; dataArr[i].annotationData[j].ytop = 0.98765 + j; dataArr[i].annotationData[j].ybottom = 0.98765 + j; dataArr[i].annotationData[j].label = "chair"; var p = j - 1 + 0.98765; dataArr[i].annotationData[j].prob = p / j; } } dataArr[3].timestamp = dataArr[2].timestamp; var client = new MongoClient(connectionString); var server = client.GetServer(); var database = server.GetDatabase("db"); var entries = database.GetCollection <BsonDocument>("entries"); //loop for receiving data, in this case just iterating through sampledata foreach (var item in dataArr) { List <string> temp = new List <string>(); List <string> seen = new List <string>(); List <string> objs = new List <string>(); foreach (var k in item.annotationData) { temp.Add(k.label); } foreach (var curr in temp) { if (seen.Contains(curr) == true) { continue; } seen.Add(curr); int count = 0; for (int i = 0; i < temp.Count; i++) { string s = temp[i]; if (s == curr) { objs.Add(curr + count); count++; } } } Debug.Log(objs); //$in BsonArray bArr = new BsonArray(objs); var match = new BsonDocument { { "$match", new BsonDocument { { "labels", new BsonDocument { { "$in", bArr } } } } } }; var unwind = new BsonDocument { { "$unwind", "$labels" } }; var group = new BsonDocument { { "$group", new BsonDocument { { "_id", "$id" }, { "matches", new BsonDocument { { "$sum", 1 } } } } } }; var sort = new BsonDocument { { "$sort", new BsonDocument { { "matches", -1 } } } }; var cursor = new BsonDocument { { "cursor", new BsonDocument { } } }; var pipeline = new[] { match, unwind, match, group, sort }; //query = new BsonDocument{{"$in": new BsonArray(new[] {})}} //foreach(var elem in objs): // query["$in"].append(elem.encode("utf-8")) // query = {"labels": query} //cursor = entr.aggregate( // [{"$match": query}, // {"$unwind": "$labels"}, // {"$match": query}, // {"$group": { // "_id":"$_id", // "matches": {"$sum":1} // }}, // {"$sort": {"matches": -1}}] //) AggregateArgs args = new AggregateArgs(); args.Pipeline = pipeline; var aggregate = entries.Aggregate(args); //.Match(c => c.objs.Any(i => c.labels.Contains(i))).Unwind(c => c.labels).Match(c => c.objs.Any(i => c.labels.Contains(i))).Group(new BsonDocument{{"_id", "$_id"}, {"matches", new BsonDocument("$sum", 1)}}).Sort(new BsonDocument{{"matches", -1}}); //.Limit(20) //var examples = aggregate.ResultDocuments; foreach (var example in aggregate) { Console.WriteLine(example); } } }
public IEnumerable <BsonDocument> Aggregate(AggregateArgs aggregateArgs) { ThrowIfOperatingInMemory(); return(_storage.Aggregate(aggregateArgs)); }
public Task <IEnumerable <TrackingStatPerDateEntity> > AggregatePerDateAsync(string subscriptionId, string redirectUrl, DateTime?dateFrom, DateTime?dateTo) { return(Task.Run(() => { var filterPipeline = new List <BsonDocument>(); string statSubscriptionIdPropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.SubscriptionId); string statRedirectUrlPropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.RedirectUrl); string statDatePropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.Date); string statAggDatePropertyName = NameOfHelper.PropertyName <TrackingStatPerDateEntity>(x => x.Date); string statAggCountPropertyName = NameOfHelper.PropertyName <TrackingStatPerDateEntity>(x => x.Count); // Filter filterPipeline.Add(new BsonDocument { { "$match", new BsonDocument { { statSubscriptionIdPropertyName, subscriptionId } } } }); if (!string.IsNullOrEmpty(redirectUrl)) { filterPipeline.Add(new BsonDocument { { "$match", new BsonDocument { { statRedirectUrlPropertyName, new BsonDocument { { "$regex", string.Format("^{0}", Regex.Escape(redirectUrl)) } } } } } }); } if (dateFrom.HasValue) { DateTime dateFromUtc = DateTime.SpecifyKind(dateFrom.Value, DateTimeKind.Utc); filterPipeline.Add(new BsonDocument { { "$match", new BsonDocument { { statDatePropertyName, new BsonDocument { { "$gte", dateFromUtc } } } } } }); } if (dateTo.HasValue) { DateTime dateToUtc = DateTime.SpecifyKind(dateTo.Value, DateTimeKind.Utc); filterPipeline.Add(new BsonDocument { { "$match", new BsonDocument { { statDatePropertyName, new BsonDocument { { "$lt", dateToUtc } } } } } }); } // Group by date // Since we have no way to combine datetime parts back to ISODate object we should substract all time parts from ISODate object before grouping // http://www.kamsky.org/1/post/2013/03/stupid-date-tricks-with-aggregation-framework.html string refDateProperty = string.Format("${0}", statDatePropertyName); var preGroupOperator = new BsonDocument { { "$project", new BsonDocument { { statDatePropertyName, new BsonDocument { { "$subtract", new BsonArray { string.Format("${0}", statDatePropertyName), new BsonDocument { { "$add", new BsonArray { new BsonDocument { { "$millisecond", refDateProperty } }, new BsonDocument { { "$multiply", new BsonArray { new BsonDocument { { "$second", refDateProperty } }, 1000 } } }, new BsonDocument { { "$multiply", new BsonArray { new BsonDocument { { "$minute", refDateProperty } }, 60, 1000 } } }, new BsonDocument { { "$multiply", new BsonArray { new BsonDocument { { "$hour", refDateProperty } }, 60, 60, 1000 } } }, } } } } } } } } } }; var groupOperator = new BsonDocument { { "$group", new BsonDocument { { "_id", new BsonDocument { { statAggDatePropertyName, refDateProperty } } }, { statAggCountPropertyName, new BsonDocument { { "$sum", 1 } } } } } }; // Project for compatible object var projectOperator = new BsonDocument { { "$project", new BsonDocument { { "_id", 0 }, { statAggDatePropertyName, string.Format("$_id.{0}", statAggDatePropertyName) }, { statAggCountPropertyName, 1 } } } }; // Sort var sortOperator = new BsonDocument { { "$sort", new BsonDocument { { statAggDatePropertyName, 1 } } } }; // Build pipelines var resultPipeline = new List <BsonDocument>(filterPipeline) { preGroupOperator, groupOperator, projectOperator, sortOperator }; // Execute aggregations var aggArgs = new AggregateArgs { Pipeline = resultPipeline }; IEnumerable <BsonDocument> aggResult = Collection.Aggregate(aggArgs); // Project IEnumerable <TrackingStatPerDateEntity> result = aggResult.Select(BsonSerializer.Deserialize <TrackingStatPerDateEntity>); return result; })); }
private string GetTagList(IMongoCollection <Post> posts) { var unwind = new BsonDocument { { "$unwind", "$Tags" } }; var group = new BsonDocument { { "$group", new BsonDocument { { "_id", "$Tags" }, { "TagFrequency", new BsonDocument { { "$sum", 1 } } } } } }; var sort = new BsonDocument("$sort", new BsonDocument("TagFrequency", -1)); AggregateArgs aggregateArgs = new AggregateArgs() { Pipeline = new[] { unwind, group, sort } }; var result = Context.Posts.Aggregate(aggregateArgs).ToList(); var result2 = posts.Aggregate() .Unwind(t => t.Tags) .Group(new BsonDocument { { "_id", "$Tags" }, { "TagFrequency", new BsonDocument("$sum", 1) } }) .Sort(new BsonDocument("TagFrequency", -1)) .ToList(); var results = new Dictionary <string, int>(); result2.ForEach(doc => { results.Add(doc["_id"].AsString, doc["TagFrequency"].AsInt32); }); var list = new List <string>(); results.Keys.ToList().ForEach(key => list.Add( string.Format("{0}({1})", key, results[key]))); return(string.Join(", ", list)); }
public Task <DataResultEntity <TrackingStatPerUrlEntity> > AggregatePerUrlAsync(string subscriptionId, string redirectUrl, DateTime?dateFrom, DateTime?dateTo, string orderBy, bool?orderDesc, long?skip, long?take, bool?count) { return(Task.Run(() => { if (!string.IsNullOrEmpty(orderBy)) { PropertyInfo[] properties = typeof(TrackingStatPerUrlEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance); if (properties.All(p => p.Name != orderBy)) { throw new ArgumentException("Invalid property name for sort order", "orderBy"); } } if (skip.HasValue && skip.Value < 0) { throw new ArgumentException("Skip value must be non-negative", "skip"); } if (take.HasValue && take.Value <= 0) { throw new ArgumentException("Take value must be positive", "take"); } var filterPipeline = new List <BsonDocument>(); string statSubscriptionIdPropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.SubscriptionId); string statRedirectUrlPropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.RedirectUrl); string statProjectIdPropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.ProjectId); string statDatePropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.Date); string statAggRedirectUrlPropertyName = NameOfHelper.PropertyName <TrackingStatPerUrlEntity>(x => x.RedirectUrl); string statAggProjectIdPropertyName = NameOfHelper.PropertyName <TrackingStatPerUrlEntity>(x => x.ProjectId); string statAggDateFromPropertyName = NameOfHelper.PropertyName <TrackingStatPerUrlEntity>(x => x.DateFrom); string statAggDateToPropertyName = NameOfHelper.PropertyName <TrackingStatPerUrlEntity>(x => x.DateTo); string statAggCountPropertyName = NameOfHelper.PropertyName <TrackingStatPerUrlEntity>(x => x.Count); // Filter filterPipeline.Add(new BsonDocument { { "$match", new BsonDocument { { statSubscriptionIdPropertyName, subscriptionId } } } }); if (!string.IsNullOrEmpty(redirectUrl)) { filterPipeline.Add(new BsonDocument { { "$match", new BsonDocument { { statRedirectUrlPropertyName, new BsonDocument { { "$regex", string.Format("^{0}", Regex.Escape(redirectUrl)) } } } } } }); } if (dateFrom.HasValue) { DateTime dateFromUtc = DateTime.SpecifyKind(dateFrom.Value, DateTimeKind.Utc); filterPipeline.Add(new BsonDocument { { "$match", new BsonDocument { { statDatePropertyName, new BsonDocument { { "$gte", dateFromUtc } } } } } }); } if (dateTo.HasValue) { DateTime dateToUtc = DateTime.SpecifyKind(dateTo.Value, DateTimeKind.Utc); filterPipeline.Add(new BsonDocument { { "$match", new BsonDocument { { statDatePropertyName, new BsonDocument { { "$lte", dateToUtc } } } } } }); } // Group var countGroupOperator = new BsonDocument { { "$group", new BsonDocument { { "_id", new BsonDocument { { statAggRedirectUrlPropertyName, string.Format("${0}", statRedirectUrlPropertyName) }, { statAggProjectIdPropertyName, string.Format("${0}", statProjectIdPropertyName) }, } } } } }; var resultGroupOperator = new BsonDocument { { "$group", new BsonDocument { { "_id", new BsonDocument { { statAggRedirectUrlPropertyName, string.Format("${0}", statRedirectUrlPropertyName) }, { statAggProjectIdPropertyName, string.Format("${0}", statProjectIdPropertyName) } } }, { statAggCountPropertyName, new BsonDocument { { "$sum", 1 } } }, { statAggDateFromPropertyName, new BsonDocument { { "$min", string.Format("${0}", statDatePropertyName) } } }, { statAggDateToPropertyName, new BsonDocument { { "$max", string.Format("${0}", statDatePropertyName) } } } } } }; // Project for compatible object var projectOperator = new BsonDocument { { "$project", new BsonDocument { { "_id", 0 }, { statAggRedirectUrlPropertyName, string.Format("$_id.{0}", statAggRedirectUrlPropertyName) }, { statAggProjectIdPropertyName, string.Format("$_id.{0}", statAggProjectIdPropertyName) }, { statAggCountPropertyName, 1 }, { statAggDateFromPropertyName, 1 }, { statAggDateToPropertyName, 1 } } } }; // Sort BsonDocument sortOperator = null; if (!string.IsNullOrEmpty(orderBy)) { sortOperator = new BsonDocument { { "$sort", new BsonDocument { { orderBy, orderDesc.HasValue && orderDesc.Value ? -1 : 1 } } } }; } // Skip BsonDocument skipOperator = null; if (skip.HasValue) { skipOperator = new BsonDocument { { "$skip", skip.Value } }; } // Limit BsonDocument limitOperator = null; if (take.HasValue) { limitOperator = new BsonDocument { { "$limit", take.Value } }; } // Build pipelines var countPipeline = new List <BsonDocument>(filterPipeline) { countGroupOperator }; var resultPipeline = new List <BsonDocument>(filterPipeline) { resultGroupOperator, projectOperator }; if (sortOperator != null) { resultPipeline.Add(sortOperator); } if (skipOperator != null) { resultPipeline.Add(skipOperator); } if (limitOperator != null) { resultPipeline.Add(limitOperator); } // Execute aggregations // Count var aggArgs = new AggregateArgs { Pipeline = countPipeline }; long?resultCount = null; if (count.HasValue && count.Value) { resultCount = Collection.Aggregate(aggArgs).LongCount(); } // Result aggArgs.Pipeline = resultPipeline; IEnumerable <BsonDocument> aggResult = Collection.Aggregate(aggArgs); // Project IEnumerable <TrackingStatPerUrlEntity> result = aggResult.Select(BsonSerializer.Deserialize <TrackingStatPerUrlEntity>); return new DataResultEntity <TrackingStatPerUrlEntity>(result, resultCount); })); }
public IEnumerable <BsonDocument> Aggregate(AggregateArgs aggregateArgs) { return(Collection.Aggregate(aggregateArgs)); }
// // GET: /Counter/ /*用户使用接口次数统计 *查询接口日志记录表,已记录每个接口的访问信息 */ public ActionResult Index(FormCollection form) { string interfacecode = form["interface_code"] ?? ""; MongoCollection <ModUApiInterfaceList> uapinter = mongoh.GetCollection <ModUApiInterfaceList>(); #region 统计当月,今日,合计调用次数 DateTime MonthlyFirstDate = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-01")); List <object> objarray = new List <object>(); objarray.Add("$visited_time"); objarray.Add(new BsonDateTime(MonthlyFirstDate)); DateTime TodayFirstDate = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-dd")); List <object> objarrayToday = new List <object>(); objarrayToday.Add("$visited_time"); objarrayToday.Add(new BsonDateTime(TodayFirstDate)); BsonDocument group = new BsonDocument { { "$group", new BsonDocument { { "_id", "$interface_code" }, { "MonthlyNum", new BsonDocument { { "$sum", new BsonDocument { { "$cond", new BsonDocument { { "if", new BsonDocument("$gte", new BsonArray(objarray.ToArray())) } , { "then", 1 } , { "else", 0 } } } } } } }, { "TodayNum", new BsonDocument { { "$sum", new BsonDocument { { "$cond", new BsonDocument { { "if", new BsonDocument("$gte", new BsonArray(objarrayToday.ToArray())) } , { "then", 1 } , { "else", 0 } } } } } } }, { "TotalNum", new BsonDocument { { "$sum", 1 } } } } } }; #endregion AggregateArgs args = new AggregateArgs(); args.Pipeline = new List <BsonDocument> { group }; var bsonDocumentlist = mongoh.GetCollection <ModUApiUseLogFind>().Aggregate(args).ToList(); if (string.IsNullOrEmpty(interfacecode)) { foreach (var item in bsonDocumentlist) { interfacecode = item["_id"].ToString(); break; } } #region 统计本周流程图 DateTime dt = DateTime.Now; DateTime starweek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); string endtime = dt.ToString("yyyy-MM-dd 23:59:59"); string begintime = starweek.ToString("yyyy-MM-dd 00:00:00"); List <IMongoQuery> queries = new List <IMongoQuery>(); if (!string.IsNullOrEmpty(interfacecode)) { queries.Add(Query.EQ("interface_code", new BsonString(interfacecode))); } queries.Add(Query.GTE("visited_time", CUtil.CDate(begintime))); queries.Add(Query.LTE("visited_time", CUtil.CDate(endtime))); var serachResult = mongoh.GetCollection <ModUApiUseLogFind>().FindByAnd(queries).ToList(); var curveChart = (from b in serachResult orderby b.visited_time group b by b.visited_time.Date into b select new CurveChart { amount = b.Count().ToString(), date = b.Key }).ToList(); string amount = ""; string date = ""; if (curveChart.Count > 0) { foreach (var item in curveChart) { amount += item.amount + ","; date += item.date.ToString("yyyyMMdd") + ","; } } amount = amount.TrimEnd(','); #endregion ViewBag.BsonDocumentlist = bsonDocumentlist; ViewBag.interfacecode = interfacecode; ViewBag.ModUApiInterfaceList = uapinter.FindAll().ToList(); ViewBag.Amount = amount; ViewBag.Date = date; return(View()); }