public override FilterDefinition <T> Visit(CompareFilter <ClrValue> nodeIn, None args) { var propertyName = nodeIn.Path.ToString(); var value = nodeIn.Value.Value; switch (nodeIn.Operator) { case CompareOperator.Empty: return(Filter.Or( Filter.Exists(propertyName, false), Filter.Eq <object?>(propertyName, null), Filter.Eq <object?>(propertyName, string.Empty), Filter.Size(propertyName, 0))); case CompareOperator.Exists: return(Filter.And( Filter.Exists(propertyName, true), Filter.Ne <object?>(propertyName, null))); case CompareOperator.Matchs: return(Filter.Regex(propertyName, BuildMatchRegex(nodeIn))); case CompareOperator.StartsWith: return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => "^" + s))); case CompareOperator.Contains: return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => s))); case CompareOperator.EndsWith: return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => s + "$"))); case CompareOperator.Equals: return(Filter.Eq(propertyName, value)); case CompareOperator.GreaterThan: return(Filter.Gt(propertyName, value)); case CompareOperator.GreaterThanOrEqual: return(Filter.Gte(propertyName, value)); case CompareOperator.LessThan: return(Filter.Lt(propertyName, value)); case CompareOperator.LessThanOrEqual: return(Filter.Lte(propertyName, value)); case CompareOperator.NotEquals: return(Filter.Ne(propertyName, value)); case CompareOperator.In: return(Filter.In(propertyName, ((IList)value !).OfType <object>())); } throw new NotSupportedException(); }
private async void GetUser() { DBMongo.MongoDBHelper<BsonDocument> mongoHelper = new DBMongo.MongoDBHelper<BsonDocument>("User"); IMongoCollection<BsonDocument> collection = mongoHelper.GetCollection(); // var list = collection.Find<XK.Model.User_Model>(u=>u.Name== "k1199").ToListAsync(); FilterDefinitionBuilder<BsonDocument> builder = new FilterDefinitionBuilder<BsonDocument>(); var define= builder.Lt("Age", 122); var list = collection.Find<BsonDocument>(define).ToListAsync(); string usernames = ""; list.Result.ForEach(d => usernames += d["Name"]+"</br>"); Response.Write(usernames); }
/// <inheritdoc/> public async Task <IEnumerable <Store.Streams.StreamEvent> > FetchRange( StreamPositionRange range, CancellationToken cancellationToken) { try { var maxNumEvents = range.Length; var events = await _stream.Find( _filter.Gte(_sequenceNumberExpression, range.From.Value) & _filter.Lt(_sequenceNumberExpression, range.From.Value + range.Length)) .Project(_eventToStreamEvent) .ToListAsync(cancellationToken) .ConfigureAwait(false); return(events.ToArray()); } catch (MongoWaitQueueFullException ex) { throw new EventStoreUnavailable("Mongo wait queue is full", ex); } }
/// <summary> /// 这里的逻辑是,如果有这一炮的版本号,直接返回 /// 如果这一炮不存在版本号,找到比这一炮好小的最大炮号的版本号返回 /// 比如搜索1056333,但只有小于等于1056300以及大于1056333的炮号存在ShotNoVersionNo数据库中,此时返回1056300的版本号 /// </summary> /// <param name="shotNo"></param> /// <returns></returns> public int GetVersionNoByShotNo(int shotNo) { FilterDefinitionBuilder <ShotNoVersionNo> builder = Builders <ShotNoVersionNo> .Filter; FilterDefinition <ShotNoVersionNo> filter = builder.Eq("ShotNo", shotNo); var result = MyCollection.Find <ShotNoVersionNo>(filter).ToList(); //找到了这一炮的版本号(可能存在多个),返回最大的那个 if (result.Count >= 1) { int max = 1; foreach (var v in result) { if (v.VersionNo > max) { max = v.VersionNo; } } return(max); } //如果没有找到这一炮的版本号,找到比它小的所有炮 builder = Builders <ShotNoVersionNo> .Filter; filter = builder.Lt("ShotNo", shotNo); result = MyCollection.Find <ShotNoVersionNo>(filter).ToList(); var maxOne = new ShotNoVersionNo() { //因为必定存在-1炮,所以后面foreach肯定会有结果 ShotNo = -2, VersionNo = 1 }; foreach (var r in result) { if (r.ShotNo > maxOne.ShotNo) { maxOne = r; } else if (r.ShotNo == maxOne.ShotNo) { if (r.VersionNo > maxOne.VersionNo) { maxOne = r; } } } return(maxOne.VersionNo); }
public IPageList <T> Query <T>(FilterCondition filter) where T : IBaseObject { this.CheckType <T>(); IMongoCollection <T> collection = this.GetCollection <T>(); FilterDefinitionBuilder <T> filter2 = Builders <T> .Filter; SortDefinitionBuilder <T> sort = Builders <T> .Sort; FilterDefinition <T> filterDefinition = filter2.Empty; foreach (FilterValue current in filter.Fields) { switch (current.FilterType) { case FilterType.LTE: filterDefinition &= filter2.Lte <object>(current.FieldName, current.Value); break; case FilterType.GTE: filterDefinition &= filter2.Gte <object>(current.FieldName, current.Value); break; case FilterType.LT: filterDefinition &= filter2.Lt <object>(current.FieldName, current.Value); break; case FilterType.GT: filterDefinition &= filter2.Gt <object>(current.FieldName, current.Value); break; case FilterType.EQ: goto IL_12E; case FilterType.Like: filterDefinition &= filter2.Regex(current.FieldName, new Regex(current.Value.ToString(), RegexOptions.IgnoreCase)); break; default: goto IL_12E; } continue; IL_12E: filterDefinition &= filter2.Eq <object>(current.FieldName, current.Value); } Task <long> task = collection.CountAsync(filterDefinition, null, default(CancellationToken)); SortDefinition <T> sort2 = (filter.Direction == OrderDirection.DESC) ? sort.Descending(filter.OrderBy) : sort.Ascending(filter.OrderBy); Task <List <T> > task2 = collection.Find(filterDefinition, null).Sort(sort2).Limit(new int?(filter.PageSize)).Skip(new int?((filter.PageIndex - 1) * filter.PageSize)).ToListAsync(default(CancellationToken)); return(new PageList <T>(task2.Result, filter.PageIndex, filter.PageSize, (int)task.Result)); }
public override FilterDefinition <T> Visit(FilterComparison nodeIn) { var propertyName = string.Join(".", nodeIn.Lhs); switch (nodeIn.Operator) { case FilterOperator.Empty: return(Filter.Or(Filter.Exists(propertyName, false), Filter.Eq(propertyName, default(T)), Filter.Eq(propertyName, string.Empty), Filter.Eq(propertyName, new T[0]))); case FilterOperator.StartsWith: return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => "^" + s))); case FilterOperator.Contains: return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => s))); case FilterOperator.EndsWith: return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => s + "$"))); case FilterOperator.Equals: return(Filter.Eq(propertyName, nodeIn.Rhs.Value)); case FilterOperator.GreaterThan: return(Filter.Gt(propertyName, nodeIn.Rhs.Value)); case FilterOperator.GreaterThanOrEqual: return(Filter.Gte(propertyName, nodeIn.Rhs.Value)); case FilterOperator.LessThan: return(Filter.Lt(propertyName, nodeIn.Rhs.Value)); case FilterOperator.LessThanOrEqual: return(Filter.Lte(propertyName, nodeIn.Rhs.Value)); case FilterOperator.NotEquals: return(Filter.Ne(propertyName, nodeIn.Rhs.Value)); case FilterOperator.In: return(Filter.In(propertyName, ((IList)nodeIn.Rhs.Value).OfType <object>())); } throw new NotSupportedException(); }
public FilterDefinition <T> GetFromCompareType <T>(FilterCondition s) { FilterDefinitionBuilder <T> filter = Builders <T> .Filter; if (s.Value == null) { return(filter.Empty); } switch (s.CompareType) { case TableCompareType.EQ: return(filter.Eq(s.Key, s.Value)); case TableCompareType.GT: return(filter.Gt(s.Key, s.Value)); case TableCompareType.GTE: return(filter.Gte(s.Key, s.Value)); case TableCompareType.LT: return(filter.Lt(s.Key, s.Value)); case TableCompareType.LTE: return(filter.Lte(s.Key, s.Value)); case TableCompareType.NE: return(filter.Ne(s.Key, s.Value)); case TableCompareType.REGEX: { return(filter.Regex(s.Key, s.Value.ToString())); } case TableCompareType.TEXT: return(filter.Text(s.Value.ToString())); default: return(filter.Eq(s.Key, s.Value)); } }
static void ExecuteAggregationQuery(DateRange dr) { FilterDefinitionBuilder <BsonDocument> matchFilterDefinition = Builders <BsonDocument> .Filter; var filter = matchFilterDefinition.Eq("event_type", "Address_Contact_Change_Request_Event") & matchFilterDefinition.Gt("request_status_isodate", dr.Begin.ToString("o", CultureInfo.InvariantCulture)) & matchFilterDefinition.Lt("request_status_isodate", dr.End.ToString("o", CultureInfo.InvariantCulture)); var matchFilter = BsonDocument.Parse("{\"event_type\":\"Address_Contact_Change_Request_Event\",\"request_status_isodate\":{$ne:null}}"); var sortFilter = BsonDocument.Parse("{\"request_status_isodate\":-1}"); var groupByFilter = BsonDocument.Parse("{\"_id\":{\"agreement_id\":\"$agreement_id\"},\"events\":{ \"$push\":{ \"event\":\"$$CURRENT\"} }}"); var projectOp = BsonDocument.Parse("{\"events\":{\"$slice\":[\"$events\",1]}}"); var finalProjectOp = BsonDocument.Parse("{\"_id\":0,\"data\":\"$events.event\"}"); var docs = aggCollection .Aggregate() .Match(filter) .Sort(sortFilter) .Group(groupByFilter) .Project(projectOp) .Unwind("events") .Project(finalProjectOp) .ToList(); }
public override FilterDefinition <MongoContentEntity> Visit(BinaryOperatorNode nodeIn) { if (nodeIn.OperatorKind == BinaryOperatorKind.And) { return(Filter.And(nodeIn.Left.Accept(this), nodeIn.Right.Accept(this))); } if (nodeIn.OperatorKind == BinaryOperatorKind.Or) { return(Filter.Or(nodeIn.Left.Accept(this), nodeIn.Right.Accept(this))); } if (nodeIn.OperatorKind == BinaryOperatorKind.NotEqual) { return(Filter.Ne(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } if (nodeIn.OperatorKind == BinaryOperatorKind.Equal) { return(Filter.Eq(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } if (nodeIn.OperatorKind == BinaryOperatorKind.LessThan) { return(Filter.Lt(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } if (nodeIn.OperatorKind == BinaryOperatorKind.LessThanOrEqual) { return(Filter.Lte(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } if (nodeIn.OperatorKind == BinaryOperatorKind.GreaterThan) { return(Filter.Gt(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } if (nodeIn.OperatorKind == BinaryOperatorKind.GreaterThanOrEqual) { return(Filter.Gte(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } throw new NotSupportedException(); }
public object Lt <TProperty>(string property, TProperty value) { return(InternalBuilder.Lt(property, value)); }
public override FilterDefinition <MongoContentEntity> Visit(BinaryOperatorNode nodeIn) { if (nodeIn.OperatorKind == BinaryOperatorKind.And) { return(Filter.And(nodeIn.Left.Accept(this), nodeIn.Right.Accept(this))); } if (nodeIn.OperatorKind == BinaryOperatorKind.Or) { return(Filter.Or(nodeIn.Left.Accept(this), nodeIn.Right.Accept(this))); } if (nodeIn.Left is SingleValueFunctionCallNode functionNode) { var regexFilter = Visit(functionNode); var value = BuildValue(nodeIn.Right); if (value is bool booleanRight) { if ((nodeIn.OperatorKind == BinaryOperatorKind.Equal && !booleanRight) || (nodeIn.OperatorKind == BinaryOperatorKind.NotEqual && booleanRight)) { regexFilter = Filter.Not(regexFilter); } return(regexFilter); } } else { if (nodeIn.OperatorKind == BinaryOperatorKind.NotEqual) { var field = BuildFieldDefinition(nodeIn.Left); return(Filter.Or( Filter.Not(Filter.Exists(field)), Filter.Ne(field, BuildValue(nodeIn.Right)))); } if (nodeIn.OperatorKind == BinaryOperatorKind.Equal) { return(Filter.Eq(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } if (nodeIn.OperatorKind == BinaryOperatorKind.LessThan) { return(Filter.Lt(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } if (nodeIn.OperatorKind == BinaryOperatorKind.LessThanOrEqual) { return(Filter.Lte(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } if (nodeIn.OperatorKind == BinaryOperatorKind.GreaterThan) { return(Filter.Gt(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } if (nodeIn.OperatorKind == BinaryOperatorKind.GreaterThanOrEqual) { return(Filter.Gte(BuildFieldDefinition(nodeIn.Left), BuildValue(nodeIn.Right))); } } throw new NotSupportedException(); }
public void Lt_with_field_name_should_render_correctly(int?value, string expectedFilter) { var filter = __subject.Lt("I", value); filter.Render(__serializer, __registry).Should().Be(expectedFilter); }
/* 输入:设备Id、起止时间 * 输出:总告警次数 */ public int GetDeviceAffiliateAlarmInfoNumber(String deviceId, DateTime startTime, DateTime endTime) { FilterDefinitionBuilder <AlarmInfoModel> builderFilter = Builders <AlarmInfoModel> .Filter; FilterDefinition <AlarmInfoModel> filter = builderFilter.And(builderFilter.Eq("DeviceId", deviceId), builderFilter.Gt("Timestamp", startTime), builderFilter.Lt("Timestamp", endTime)); return((int)_alarmInfo.Find(filter).CountDocuments()); /* * return _alarmInfo.AsQueryable() * .Where(ai => ai.DeviceId == deviceId & ai.Timestamp>=startTime &ai.Timestamp<=endTime) * .ToList().Count; */ }
// // 摘要: // Tests that the value of the named element is less than some value (see $lt). // // 参数: // name: // The name of the element to test. // // value: // The value to compare to. // // 返回结果: // The builder (so method calls can be chained). public static FilterDefinition <BsonDocument> LT(string name, BsonValue value) { return(filter.Lt(name, value)); }
public async Task WhenGet_Grade_Score_Less_Than_Returns_RestaurantList() { var collection = _database.GetCollection<BsonDocument>("restaurants"); var builder = new FilterDefinitionBuilder<BsonDocument>(); var filter = builder.Lt("grades.score", 15); var result = await collection.Find(filter).ToListAsync(); Assert.AreEqual(23828, result.Count()); }
static void DoTask(BsonDocument pointitem, TaskScheduler scheduler) { Action <object> act = obj => { try { var isactive = Convert.ToDouble(pointitem["isactive"]); //非激活点不处理 if (isactive == 0) { return; } var tenantid = pointitem["tenantid"].ToString(); var pointid = pointitem["pointid"].ToString(); var interval = Convert.ToDouble(pointitem["interval"]); var pointtype = Convert.ToInt32(pointitem["pointtype"]); var iscompress = Convert.ToInt32(pointitem["compress"]["iscompress"]); var compressmethod = pointitem["compress"]["compressmethod"].ToString(); var compresscycle = Convert.ToDouble(pointitem["compress"]["compresscycle"]); var accuracy = Convert.ToDouble(pointitem["compress"]["accuracy"]); var rtcollection = pointitem["storage"]["rtcollection"].ToString(); var historycollection = pointitem["storage"]["historycollection"].ToString(); SortDefinitionBuilder <BsonDocument> builderSort = Builders <BsonDocument> .Sort; //排序约束 Ascending 正序 Descending 倒序 //SortDefinition<BsonDocument> sort = builderSort.Ascending("timestampclient"); //创建约束生成器 FilterDefinitionBuilder <BsonDocument> builderFilter = Builders <BsonDocument> .Filter; FilterDefinition <BsonDocument> pointDataFilter = builderFilter.Eq("pointid", pointid) & builderFilter.Eq("tenantid", tenantid); var nowTimestamp = GetTimestamp(DateTime.Now); if (0 == pointtype)//心跳包 { var firstPointDataBsonOneResult = _context.RTPointDatasBson(rtcollection).Find <BsonDocument>(pointDataFilter).Sort(builderSort.Ascending("timestampserver")).FirstOrDefault(); if (firstPointDataBsonOneResult == null) { return; } var firstTimestamp = (long)firstPointDataBsonOneResult["timestampserver"]; if (nowTimestamp - firstTimestamp < compresscycle) { return; } var lastPointDataBsonOneResult = _context.RTPointDatasBson(rtcollection).Find <BsonDocument>(pointDataFilter).Sort(builderSort.Descending("timestampserver")).FirstOrDefault(); var lastTimestamp = (long)lastPointDataBsonOneResult["timestampserver"]; pointDataFilter = builderFilter.Eq("pointid", pointid) & builderFilter.Eq("tenantid", tenantid) & builderFilter.Lt("timestampserver", lastTimestamp) & builderFilter.Gte("timestampserver", firstTimestamp); var pointDataBsonResult = _context.RTPointDatasBson(rtcollection).Find <BsonDocument>(pointDataFilter).Sort(builderSort.Ascending("timestampserver")).Skip(0).Limit(5000).ToList(); if (pointDataBsonResult.Count == 0) { return; } pointDataBsonResult.ForEach((x) => { if (x.Contains("_id")) { x.Remove("_id"); } }); pointDataFilter = builderFilter.Eq("pointid", pointid) & builderFilter.Eq("tenantid", tenantid) & builderFilter.Lte("timestampserver", Convert.ToInt64(pointDataBsonResult[pointDataBsonResult.Count - 1]["timestampserver"])) & builderFilter.Gte("timestampserver", Convert.ToInt64(pointDataBsonResult[0]["timestampserver"])); _context.HistoryPointDatasBson(historycollection).InsertMany(pointDataBsonResult); var deleteResult = _context.RTPointDatasBson(rtcollection).DeleteMany(pointDataFilter); return; } else { var firstPointDataBsonOneResult = _context.RTPointDatasBson(rtcollection).Find <BsonDocument>(pointDataFilter).Sort(builderSort.Ascending("timestampclient")).FirstOrDefault(); if (firstPointDataBsonOneResult == null) { return; } var firstTimestamp = (long)firstPointDataBsonOneResult["timestampclient"]; if (nowTimestamp - firstTimestamp < compresscycle) { return; } var lastPointDataBsonOneResult = _context.RTPointDatasBson(rtcollection).Find <BsonDocument>(pointDataFilter).Sort(builderSort.Descending("timestampclient")).FirstOrDefault(); var lastTimestamp = (long)lastPointDataBsonOneResult["timestampclient"]; //取值时全部取出,作为压缩算法处理队列数据 pointDataFilter = builderFilter.Eq("pointid", pointid) & builderFilter.Eq("tenantid", tenantid) & builderFilter.Lte("timestampclient", lastTimestamp) & builderFilter.Gte("timestampclient", firstTimestamp); var pointDataBsonResult = _context.RTPointDatasBson(rtcollection).Find <BsonDocument>(pointDataFilter).Sort(builderSort.Ascending("timestampclient")).Skip(0).Limit(5000).ToList(); if (pointDataBsonResult.Count == 0) { return; } pointDataBsonResult.ForEach((x) => { if (x.Contains("_id")) { x.Remove("_id"); } }); if (iscompress == 0)//不压缩处理 { _context.HistoryPointDatasBson(historycollection).InsertMany(pointDataBsonResult); //删除时不留最新数据,不用考虑断电状况,因为没有压缩 pointDataFilter = builderFilter.Eq("pointid", pointid) & builderFilter.Eq("tenantid", tenantid) & builderFilter.Lte("timestampserver", Convert.ToInt64(pointDataBsonResult[pointDataBsonResult.Count - 1]["timestampserver"])) & builderFilter.Gte("timestampserver", Convert.ToInt64(pointDataBsonResult[0]["timestampserver"])); var deleteResult = _context.RTPointDatasBson(rtcollection).DeleteMany(pointDataFilter); } else//压缩处理 { if (pointDataBsonResult.Count <= 1) { return; } var originData = new List <Point>(); pointDataBsonResult.ForEach(x => { originData.Add(new Point() { pointid = x["pointid"].ToString(), tenantid = x["tenantid"].ToString(), interval = Convert.ToInt32(x["interval"]), quality = Convert.ToDouble(x["quality"]), timestampClient = (long)x["timestampclient"], timestampServer = (long)x["timestampserver"], value = Convert.ToDouble(x["value"]) }); }); var dataCompressed = new List <Point>(); switch (pointtype) { case 2: //模拟量 dataCompressed = CompressUtility.CompressMonitorSDT(originData, accuracy); #region bsondocument demo //var document = new BsonDocument //{ // { "name", "MongoDB" }, // { "type", "Database" }, // { "count", 1 }, // { "info", new BsonDocument // { // { "x", 203 }, // { "y", 102 } // } // } //}; #endregion break; case 1: //开关量 dataCompressed = CompressUtility.CompressSwitch(originData); break; case 3: //字符串 dataCompressed = CompressUtility.CompressString(originData); break; default: break; } var insertBsonDocuments = dataCompressed.Select(i => new BsonDocument { { "pointid", pointid }, { "timestampclient", i.timestampClient }, { "interval", interval }, { "value", i.value }, { "quality", i.quality }, { "tenantid", tenantid }, { "timestampserver", i.timestampServer } }); _context.HistoryPointDatasBson(historycollection).InsertMany(insertBsonDocuments); //删除时留一个最新数据,作为下一压缩算法处理队列的第一个数据(用于处理两个处理队列之间发生断电状况)(这里冗余一个数据) pointDataFilter = builderFilter.Eq("pointid", pointid) & builderFilter.Eq("tenantid", tenantid) & builderFilter.Lt("timestampserver", Convert.ToInt64(pointDataBsonResult[pointDataBsonResult.Count - 1]["timestampserver"])) & builderFilter.Gte("timestampserver", Convert.ToInt64(pointDataBsonResult[0]["timestampserver"])); var deleteResult = _context.RTPointDatasBson(rtcollection).DeleteMany(pointDataFilter); } } } catch (Exception ex) { Console.WriteLine("Process compress method:" + ex.Message); } }; var task = Task.Factory.StartNew(act, pointitem, CancellationToken.None, TaskCreationOptions.None, scheduler).ContinueWith((t, obj) => { if (t.Status != TaskStatus.RanToCompletion) { DoTask(pointitem, scheduler); } //Console.WriteLine(obj); }, pointitem); _runningTasks.Add(task); }