protected ICollection <TModel> Find <TModel>(MultiOptions options) where TModel : class, new() { if (options == null) { throw new ArgumentNullException("options"); } ICollection <TModel> result = null; if (options.UseCache) { result = Cache.Get <ICollection <TModel> >(GetScopedCacheKey(options.CacheKey)); } if (result != null) { return(result); } var cursor = _collection.FindAs <TModel>(options.GetMongoQuery(_getIdValue)); var mongoOptions = options as MongoOptions; if (mongoOptions != null && mongoOptions.ReadPreference != null) { cursor.SetReadPreference(mongoOptions.ReadPreference); } if (options.UsePaging) { cursor.SetSkip(options.GetSkip()); } if (options.UseLimit) { cursor.SetLimit(options.GetLimit() + 1); } if (options.Fields.Count > 0) { cursor.SetFields(Fields.Include(options.Fields.ToArray())); } if (mongoOptions != null && mongoOptions.SortBy != null) { cursor.SetSortOrder(mongoOptions.SortBy); } result = cursor.ToList(); if (options.UseLimit) { if (result.Count > options.GetLimit()) { options.HasMore = true; } result = result.Take(options.GetLimit()).ToList(); } if (options.UseCache) { Cache.Set(GetScopedCacheKey(options.CacheKey), result, options.GetCacheExpirationDate()); } return(result); }
public static IMongoQuery GetMongoQuery(this MultiOptions options, Func <string, BsonValue> getIdValue = null) { var query = GetMongoQuery((QueryOptions)options, getIdValue); var mongoOptions = options as MongoOptions; if (mongoOptions == null) { return(query); } if (getIdValue == null) { getIdValue = id => new BsonObjectId(new ObjectId(id)); } if (options.UseDateRange) { query = query.And(Query.GTE(options.DateField, options.GetStartDate()).And(Query.LTE(options.DateField, options.GetEndDate()))); } if (!String.IsNullOrEmpty(mongoOptions.BeforeValue) && mongoOptions.BeforeQuery == null) { try { mongoOptions.BeforeQuery = Query.LT(CommonFieldNames.Id, getIdValue(mongoOptions.BeforeValue)); } catch (Exception ex) { ex.ToExceptionless().AddObject(mongoOptions.BeforeQuery, "BeforeQuery").Submit(); } } if (!String.IsNullOrEmpty(mongoOptions.AfterValue) && mongoOptions.AfterQuery == null) { try { mongoOptions.AfterQuery = Query.GT(CommonFieldNames.Id, getIdValue(mongoOptions.AfterValue)); } catch (Exception ex) { ex.ToExceptionless().AddObject(mongoOptions.AfterQuery, "AfterQuery").Submit(); } } return(query.And(mongoOptions.BeforeQuery, mongoOptions.AfterQuery)); }
public static IMongoQuery GetMongoQuery(this MultiOptions options, Func <string, BsonValue> getIdValue = null) { var query = GetMongoQuery((QueryOptions)options, getIdValue); var mongoOptions = options as MongoOptions; if (mongoOptions == null) { return(query); } if (getIdValue == null) { getIdValue = id => new BsonObjectId(new ObjectId(id)); } if (options.UseDateRange) { query = query.And(Query.GTE(options.DateField, options.GetStartDate()).And(Query.LTE(options.DateField, options.GetEndDate()))); } return(query); }
public ICollection <Stack> GetNew(string projectId, DateTime utcStart, DateTime utcEnd, PagingOptions paging, bool includeHidden = false, bool includeFixed = false, bool includeNotFound = true) { var options = new MultiOptions().WithProjectId(projectId).WithSort(SortBy.Descending(FieldNames.FirstOccurrence)).WithPaging(paging); options.Query = options.Query.And(Query.GTE(FieldNames.FirstOccurrence, utcStart), Query.LTE(FieldNames.FirstOccurrence, utcEnd)); if (!includeFixed) { options.Query = options.Query.And(Query.NotExists(FieldNames.DateFixed)); } if (!includeHidden) { options.Query = options.Query.And(Query.NE(FieldNames.IsHidden, true)); } if (!includeNotFound) { options.Query = options.Query.And(Query.NotExists(FieldNames.SignatureInfo_Path)); } return(Find <Stack>(options)); }
public ICollection <Organization> GetByCriteria(string criteria, PagingOptions paging, OrganizationSortBy sortBy, bool?paid = null, bool?suspended = null) { var options = new MultiOptions().WithPaging(paging); if (!String.IsNullOrWhiteSpace(criteria)) { options.Query = options.Query.And(Query.Matches(FieldNames.Name, new BsonRegularExpression(String.Format("/{0}/i", criteria)))); } if (paid.HasValue) { if (paid.Value) { options.Query = options.Query.And(Query.NE(FieldNames.PlanId, new BsonString(BillingManager.FreePlan.Id))); } else { options.Query = options.Query.And(Query.EQ(FieldNames.PlanId, new BsonString(BillingManager.FreePlan.Id))); } } if (suspended.HasValue) { if (suspended.Value) { options.Query = options.Query.And( Query.Or( Query.And( Query.NE(FieldNames.BillingStatus, new BsonInt32((int)BillingStatus.Active)), Query.NE(FieldNames.BillingStatus, new BsonInt32((int)BillingStatus.Trialing)), Query.NE(FieldNames.BillingStatus, new BsonInt32((int)BillingStatus.Canceled)) ), Query.EQ(FieldNames.IsSuspended, new BsonBoolean(true)))); } else { options.Query = options.Query.And( Query.Or( Query.EQ(FieldNames.BillingStatus, new BsonInt32((int)BillingStatus.Active)), Query.EQ(FieldNames.BillingStatus, new BsonInt32((int)BillingStatus.Trialing)), Query.EQ(FieldNames.BillingStatus, new BsonInt32((int)BillingStatus.Canceled)) ), Query.EQ(FieldNames.IsSuspended, new BsonBoolean(false))); } } switch (sortBy) { case OrganizationSortBy.Newest: options.SortBy = SortBy.Descending(FieldNames.Id); break; case OrganizationSortBy.Subscribed: options.SortBy = SortBy.Descending(FieldNames.SubscribeDate); break; case OrganizationSortBy.MostActive: options.SortBy = SortBy.Descending(FieldNames.TotalEventCount); break; default: options.SortBy = SortBy.Ascending(FieldNames.Name); break; } return(Find <Organization>(options)); }