public void Save(CrashSummary entity) { try { IMongoQuery query = Query.And ( Query <CrashSummary> .EQ <DateTime>(mem => mem.Date, entity.Date), Query <CrashSummary> .EQ <Guid>(mem => mem.ApplicationId, entity.ApplicationId), Query <CrashSummary> .EQ <string>(mem => mem.Version, entity.Version), Query <CrashSummary> .EQ <PlatformType>(mem => mem.PlatformId, entity.PlatformId) ); IMongoUpdate update = Update <CrashSummary> .SetOnInsert(x => x.Version, entity.Version) .SetOnInsert(x => x.Date, entity.Date) .SetOnInsert(x => x.ApplicationId, entity.ApplicationId) .SetOnInsert(x => x.PlatformId, entity.PlatformId) .Inc(mem => mem.Count, entity.Count); this.GetCollection <CrashSummary>().FindAndModify(query, SortBy.Descending("Date"), update, false, true); this.GetCollection <CrashSummary>().EnsureIndex(IndexKeys.Descending("Date")); } catch (Exception ex) { throw new DataAccessLayerException(ex); } }
protected override async Task SetupCollectionAsync(IMongoCollection <UserNotification> collection, CancellationToken ct = default) { await Collection.Indexes.CreateOneAsync( new CreateIndexModel <UserNotification>( IndexKeys .Ascending(x => x.AppId) .Ascending(x => x.UserId) .Ascending(x => x.Updated) .Ascending(x => x.IsDeleted) .Descending(x => x.Created)), null, ct); await Collection.Indexes.CreateOneAsync( new CreateIndexModel <UserNotification>( IndexKeys .Ascending(x => x.AppId) .Ascending(x => x.UserId) .Ascending(x => x.Created)), null, ct); await Collection.Indexes.CreateOneAsync( new CreateIndexModel <UserNotification>( IndexKeys .Descending(x => x.Created), new CreateIndexOptions { ExpireAfter = options.RetentionTime }), null, ct); }
public void TestDescending1() { var keys = IndexKeys.Descending("a"); string expected = "{ \"a\" : -1 }"; Assert.Equal(expected, keys.ToJson()); }
public Database(string connectionString) { cli = new MongoClient(connectionString); db = cli.GetServer().GetDatabase("autoru"); CachedContent.EnsureIndex(IndexKeys <CachedContent> .Descending(x => x.Timestamp), IndexOptions.SetTimeToLive(TimeSpan.FromDays(7))); Post.EnsureIndex(IndexKeys <Post> .Descending(x => x.Timestamp), IndexOptions.SetTimeToLive(TimeSpan.FromDays(7))); Post.EnsureIndex(IndexKeys <Post> .Ascending(x => x.ForumId) .Descending(x => x.TopicId) .Ascending(x => x.Index) ); if (!Forum.AsQueryable().Any()) { Forum.Save(new Forum { ForumId = "moto", DoCrawl = true }); Forum.Save(new Forum { ForumId = "scooter", DoCrawl = true }); } User.EnsureIndex(IndexKeys <User> .Ascending(x => x.UserName)); ReadId.EnsureIndex(IndexKeys <ReadId> .Ascending(x => x.UserId).Ascending(x => x.PostId)); ReadId.EnsureIndex(IndexKeys <ReadId> .Ascending(x => x.UserId).Descending(x => x.Timestamp)); }
public void CreateIndex <T>(string name, bool isUnique, params string[] keys) { var options = IndexOptions.SetName(name).SetUnique(isUnique); var indexKeys = IndexKeys.Descending(keys); db.GetCollection <T>().EnsureIndex(indexKeys, options); }
public void TestDescendingAscending() { var keys = IndexKeys.Descending("a").Ascending("b"); string expected = "{ \"a\" : -1, \"b\" : 1 }"; Assert.AreEqual(expected, keys.ToJson()); }
public void Handle(PaymentIsRecorded message) { BsonDocument doc = BsonDocument.Parse(message.PaymentJson); BsonDocument index = new BsonDocument(); // index["Keywords"] = BsonValue.Create(new string[13]{ // doc["_id"].ToString(), // doc["Customer"].ToString(), // doc["CustomerId"].ToString(), // doc["InvoiceDate"].ToString(), // doc["DueDate"].ToString(), // doc["LateFee"].ToString(), // doc["Note"].ToString(), // doc["PONo"].ToString(), // doc["Status"].ToString(), // doc["SubTotal"].ToString(), // doc["TermCondition"].ToString(), // doc["Total"].ToString(), // doc["OwnerId"].ToString()}); index["InvoiceNo"] = doc["InvoiceNo"]; index["Customer"] = doc["Customer"]; index["OwnerId"] = doc["OwnerId"]; index["_id"] = doc["_id"]; index["PONo"] = doc["PONo"]; Collections.Save(index); Collections.EnsureIndex(IndexKeys.Descending("Keywords"), IndexOptions.SetName("Keywords")); }
public void TestDescendingDescending() { var keys = IndexKeys <Test> .Descending(x => x.A).Descending(x => x.B); string expected = "{ \"a\" : -1, \"b\" : -1 }"; Assert.AreEqual(expected, keys.ToJson()); }
protected override void InitializeCollection(MongoDatabase database) { base.InitializeCollection(database); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.ProjectId, FieldNames.SignatureHash), IndexOptions.SetUnique(true).SetBackground(true)); _collection.CreateIndex(IndexKeys.Descending(FieldNames.ProjectId, FieldNames.LastOccurrence), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Descending(FieldNames.ProjectId, FieldNames.IsHidden, FieldNames.DateFixed, FieldNames.SignatureInfo_Path), IndexOptions.SetBackground(true)); }
protected override void InitializeCollection(MongoDatabase database) { base.InitializeCollection(database); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.ProjectId), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.StackId), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.OrganizationId, FieldNames.Date_UTC), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Descending(FieldNames.ProjectId, FieldNames.Date_UTC, FieldNames.IsFixed, FieldNames.IsHidden, FieldNames.Type), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Descending(FieldNames.StackId, FieldNames.Date_UTC), IndexOptions.SetBackground(true)); }
protected override void InitializeCollection(MongoCollection <Error> collection) { base.InitializeCollection(collection); collection.EnsureIndex(IndexKeys.Ascending(FieldNames.ProjectId)); collection.EnsureIndex(IndexKeys.Ascending(FieldNames.ErrorStackId)); collection.EnsureIndex(IndexKeys.Descending(FieldNames.ProjectId, FieldNames.OccurrenceDate_UTC)); collection.EnsureIndex(IndexKeys.Descending(FieldNames.RequestInfo_ClientIpAddress, FieldNames.OccurrenceDate_UTC)); collection.EnsureIndex(IndexKeys.Descending(FieldNames.ErrorStackId, FieldNames.OccurrenceDate_UTC)); }
public void Save(FeedbackSummary entity) { try { IMongoQuery queryBase = Query.And ( Query <FeedbackSummary> .EQ <DateTime>(mem => mem.Date, entity.Date), Query <FeedbackSummary> .EQ <Guid>(mem => mem.ApplicationId, entity.ApplicationId), Query <FeedbackSummary> .EQ <string>(mem => mem.Version, entity.Version), Query <FeedbackSummary> .EQ <PlatformType>(mem => mem.PlatformId, entity.PlatformId) ); IMongoUpdate update = Update <FeedbackSummary> .SetOnInsert(x => x.Version, entity.Version) .SetOnInsert(x => x.Date, entity.Date) .SetOnInsert(x => x.ApplicationId, entity.ApplicationId) .SetOnInsert(x => x.PlatformId, entity.PlatformId) .SetOnInsert(x => x.Ratings, new List <RatingAggregate>()) .Inc(mem => mem.Count, entity.Count) .Inc(mem => mem.SumOfRatings, entity.SumOfRatings); this.GetCollection <FeedbackSummary>() .FindAndModify(queryBase, SortBy.Descending("Date"), update, false, true); this.GetCollection <FeedbackSummary>().EnsureIndex(IndexKeys.Descending("Date")); IMongoQuery queryFeedbackInsert = Query.And ( queryBase, Query.NE("Ratings.Key", BsonValue.Create(entity.Ratings.First().Key)) ); IMongoUpdate updateFeedback = Update .Push("Ratings", BsonValue.Create(entity.Ratings.First().CopyOnlyKey().ToBsonDocument())); this.GetCollection <FeedbackSummary>().Update(queryFeedbackInsert, updateFeedback); IMongoQuery queryFeedbackUpdate = Query.And ( queryBase, Query.EQ("Ratings.Key", BsonValue.Create(entity.Ratings.First().Key)) ); IMongoUpdate updateInc = Update .Inc("Ratings.$.Rating", entity.Ratings.First().Rating) .Inc("Ratings.$.Count", 1); this.GetCollection <FeedbackSummary>().Update(queryFeedbackUpdate, updateInc); } catch (Exception ex) { throw new DataAccessLayerException(ex); } }
public void Save(EventSummary entity) { try { IMongoQuery queryBase = Query.And ( Query <EventSummary> .EQ <DateTime>(mem => mem.Date, entity.Date), Query <EventSummary> .EQ <Guid>(mem => mem.ApplicationId, entity.ApplicationId), Query <EventSummary> .EQ <string>(mem => mem.Version, entity.Version), Query <EventSummary> .EQ <PlatformType>(mem => mem.PlatformId, entity.PlatformId) ); IMongoUpdate update = Update <EventSummary> .SetOnInsert(x => x.Version, entity.Version) .SetOnInsert(x => x.Date, entity.Date) .SetOnInsert(x => x.ApplicationId, entity.ApplicationId) .SetOnInsert(x => x.PlatformId, entity.PlatformId) .Inc(mem => mem.Count, entity.Count); this.GetCollection <EventSummary>().FindAndModify(queryBase, SortBy.Descending("Date"), update, false, true); this.GetCollection <EventSummary>().EnsureIndex(IndexKeys.Descending("Date")); IMongoQuery queryEventInsert = Query.And ( queryBase, Query.NE("ScreenEvents.ScreenAndEvent", BsonValue.Create(entity.ScreenEvents.First().ScreenAndEvent)) ); IMongoUpdate insertEvent = Update .Push("ScreenEvents", BsonValue.Create(entity.ScreenEvents.First() .CopyOnlyKeys().ToBsonDocument())); this.GetCollection <EventSummary>().Update(queryEventInsert, insertEvent); IMongoQuery queryGetExistingScreenEvents = Query.And ( queryBase, Query.EQ("ScreenEvents.ScreenAndEvent", BsonValue.Create(String.Concat(entity.ScreenEvents.First().ScreenAndEvent))) ); IMongoUpdate updateScreenEvents = Update .Inc("ScreenEvents.$.Count", 1); this.GetCollection <EventSummary>().Update(queryGetExistingScreenEvents, updateScreenEvents); } catch (Exception ex) { throw new DataAccessLayerException(ex); } }
public MongoDbPostRepository(IMongoConnectionFactory connectionFactory) { db = connectionFactory.Create(); db.GetCollection("posts").EnsureIndex( IndexKeys.Descending("Slug", "Version"), IndexOptions.SetUnique(true)); db.GetCollection("sequences").EnsureIndex( IndexKeys.Descending("Name"), IndexOptions.SetUnique(true)); }
public void Save(ContentLoadSummary entity) { try { IMongoQuery queryBase = Query.And ( Query <ContentLoadSummary> .EQ <DateTime>(mem => mem.Date, entity.Date), Query <ContentLoadSummary> .EQ <Guid>(mem => mem.ApplicationId, entity.ApplicationId), Query <ContentLoadSummary> .EQ <string>(mem => mem.Version, entity.Version), Query <ContentLoadSummary> .EQ <PlatformType>(mem => mem.PlatformId, entity.PlatformId) ); IMongoUpdate update = Update <ContentLoadSummary> .SetOnInsert(x => x.Version, entity.Version) .SetOnInsert(x => x.Date, entity.Date) .SetOnInsert(x => x.ApplicationId, entity.ApplicationId) .SetOnInsert(x => x.PlatformId, entity.PlatformId) .SetOnInsert(x => x.Loads, new List <ContentDurationAggregate>()) .Inc(mem => mem.Count, entity.Count); this.GetCollection <ContentLoadSummary>().FindAndModify(queryBase, SortBy.Descending("Date"), update, false, true); this.GetCollection <ContentLoadSummary>().EnsureIndex(IndexKeys.Descending("Date")); IMongoQuery queryLoadInsert = Query.And ( queryBase, Query.NE("Loads.ScreenContent", BsonValue.Create(entity.Loads.First().ScreenContent)) ); IMongoUpdate insertLoad = Update .Push("Loads", BsonValue.Create(entity.Loads.First().CopyOnlyKey().ToBsonDocument())); this.GetCollection <ContentLoadSummary>().Update(queryLoadInsert, insertLoad); IMongoQuery queryLoadUpdate = Query.And ( queryBase, Query.EQ("Loads.ScreenContent", BsonValue.Create(entity.Loads.First().ScreenContent)) ); IMongoUpdate updateContentLoad = Update .Inc("Loads.$.Seconds", entity.Loads.First().Seconds) .Inc("Loads.$.Count", 1); this.GetCollection <ContentLoadSummary>().Update(queryLoadUpdate, updateContentLoad); } catch (Exception ex) { throw new DataAccessLayerException(ex); } }
public virtual void CreateIndex(string[] keyNames, bool descending, bool unique) { var options = IndexOptions.SetUnique(unique); if (descending) { MongoDBColletion.CreateIndex(IndexKeys.Descending(keyNames), options); } else { MongoDBColletion.CreateIndex(IndexKeys.Ascending(keyNames), options); } }
public MongoIndexKeysWarpper Descending(params string[] names) { if (MongoIndexKeys == null) { MongoIndexKeys = IndexKeys.Descending(names); } else { MongoIndexKeys = MongoIndexKeys.Descending(names); } return(this); }
/// <summary> /// 创建索引 /// </summary> /// <param name="collname">表名</param> /// <param name="IndexName">索引名</param> /// <param name="isdec">是否倒序</param> public void CreateIndex(string collname, string IndexName, string isdes) { MongoCollection <BsonDocument> mc = this._db.GetCollection <BsonDocument>(collname); if (isdes == "1") { mc.CreateIndex(IndexKeys.Descending(IndexName)); } else { mc.CreateIndex(IndexKeys.Ascending(IndexName)); } }
public void Save(ScreenRouteSummary entity) { try { IMongoQuery queryBase = Query.And ( Query <ScreenRouteSummary> .EQ <DateTime>(mem => mem.Date, entity.Date), Query <ScreenRouteSummary> .EQ <Guid>(mem => mem.ApplicationId, entity.ApplicationId), Query <ScreenRouteSummary> .EQ <string>(mem => mem.Version, entity.Version), Query <ScreenRouteSummary> .EQ <PlatformType>(mem => mem.PlatformId, entity.PlatformId) ); IMongoUpdate update = Update <ScreenRouteSummary> .SetOnInsert(x => x.Version, entity.Version) .SetOnInsert(x => x.Date, entity.Date) .SetOnInsert(x => x.ApplicationId, entity.ApplicationId) .SetOnInsert(x => x.PlatformId, entity.PlatformId) .SetOnInsert(x => x.ScreenRoutes, new List <ScreenRoute>()) .Inc(mem => mem.Count, entity.Count); this.GetCollection <ScreenRouteSummary>().FindAndModify(queryBase, SortBy.Descending("Date"), update, false, true); this.GetCollection <ScreenRouteSummary>().EnsureIndex(IndexKeys.Descending("Date")); IMongoQuery queryFeedbackInsert = Query.And ( queryBase, Query.NE("ScreenRoutes.AB", BsonValue.Create(entity.ScreenRoutes.First().LastAndCurrentScreen)) ); IMongoUpdate updateFeedback = Update .Push("ScreenRoutes", BsonValue.Create(entity.ScreenRoutes.First().CopyOnlyKeys().ToBsonDocument())); this.GetCollection <ScreenRouteSummary>().Update(queryFeedbackInsert, updateFeedback); IMongoQuery queryScreenFlows = Query.And ( queryBase, Query.EQ("ScreenRoutes.AB", BsonValue.Create(entity.ScreenRoutes.First().LastAndCurrentScreen)) ); IMongoUpdate updateScreenFlows = Update .Inc("ScreenRoutes.$.Count", 1); this.GetCollection <ScreenRouteSummary>().Update(queryScreenFlows, updateScreenFlows); } catch (Exception ex) { throw new DataAccessLayerException(ex); } }
public PostRepository(MongoDatabase mongoDatabase) { db = mongoDatabase; db.GetCollection("posts").EnsureIndex( IndexKeys.Descending("Slug", "Version"), IndexOptions.SetUnique(true) ); db.GetCollection("sequences").EnsureIndex( IndexKeys.Descending("Name"), IndexOptions.SetUnique(true) ); }
/// <summary> /// 创建索引 /// </summary> public void CreateIndex() { LogService.Write("创建索引..."); try { //创建索引配置库索引 MongoDatabase dbWcf = MongoDBConn.GetDatabase("WcfLogServer");//数据库名字 MongoCollection <BsonDocument> tableInfoSetting = dbWcf["InfoSetting"]; var indexesWcf = tableInfoSetting.GetIndexes().Select(a => a["name"].ToString()).ToList(); if (!indexesWcf.Any(index => index.Contains("CollectionName"))) { tableInfoSetting.CreateIndex(IndexKeys.Descending("CollectionName"), IndexOptions.SetBackground(true).SetUnique(false)); LogService.Write("创建库:WcfLogServer 表:InfoSetting 列:CollectionName的索引"); } //创建数据库索引 string dbName = "WcfLog" + GetCurrentDate(); string[] orderby = { "CreateTime" }; List <InfoSetting> list = MongoDBUtil.GetAll <InfoSetting>("WcfLogServer", "InfoSetting", new BsonDocument(), orderby, new BsonDocument()); if (list != null && list.Count > 0) { MongoDatabase db = MongoDBConn.GetDatabase(dbName);//数据库名字 foreach (InfoSetting info in list) { if (!string.IsNullOrEmpty(info.IndexName)) { MongoCollection <BsonDocument> table = db[info.CollectionName]; var indexes = table.GetIndexes().Select(a => a["name"].ToString()).ToList(); List <string> listIndexes = info.IndexName.Split(',').ToList(); if (listIndexes != null && listIndexes.Count > 0) { foreach (string item in listIndexes) { if (!indexes.Any(index => index.Contains(item))) { table.CreateIndex(IndexKeys.Ascending(item), IndexOptions.SetBackground(true).SetUnique(false)); LogService.Write("创建库:" + dbName + " 表:" + info.CollectionName + " 列:" + item + "的索引"); } } } } } } } catch (Exception ex) { LogService.Write(ex); } }
private void EnsureIndexes() { _commits.EnsureIndex( IndexKeys.Ascending("CommitId"), IndexOptions.SetName("CommitIdIndex")); _commits.EnsureIndex( IndexKeys.Ascending("Processed"), IndexOptions.SetName("ProcessedIndex")); _commits.EnsureIndex( IndexKeys.Descending("EventSourceId", "FromVersion"), IndexOptions.SetName("OptimisticEventSourceConcurrencyIndex").SetUnique(true)); _events.EnsureIndex( IndexKeys.Ascending("EventIdentifier"), IndexOptions.SetName("EventIdentifierIndex")); }
public void Handle(CurrenciesCreated message) { BsonDocument ccy = BsonDocument.Parse(message.CurrenciesCreatedJson); BsonDocument index = new BsonDocument(); index["_id"] = ccy["_id"]; index["Name"] = ccy["Name"]; index["Code"] = ccy["Code"]; index["OwnerId"] = ccy["OwnerId"]; index["Keywords"] = BsonValue.Create(new string[] { ccy["_id"].ToString(), ccy["Name"].ToString(), ccy["Code"].ToString(), ccy["OwnerId"].ToString() }); Collections.Save(index); Collections.EnsureIndex(IndexKeys.Descending("Keywords"), IndexOptions.SetName("Keywords")); }
/// <summary> /// 创建索引 /// </summary> /// <typeparam name="T">需要创建索引的实体类型</typeparam> public bool CreateIndex <T>() { if (this._db == null) { return(false); } try { string collectionName = typeof(T).Name; MongoCollection <BsonDocument> mc = this._db.GetCollection <BsonDocument>(collectionName); PropertyInfo[] propertys = typeof(T).GetProperties(BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty); //得到该实体类型的属性 foreach (PropertyInfo property in propertys) { //在各个属性中得到其特性 foreach (object obj in property.GetCustomAttributes(true)) { MongoFieldAttribute mongoField = obj as MongoFieldAttribute; if (mongoField != null) {// 此特性为mongodb的字段属性 IndexKeysBuilder indexKey; if (mongoField.Ascending) { //升序 索引 indexKey = IndexKeys.Ascending(property.Name); } else { //降序索引 indexKey = IndexKeys.Descending(property.Name); } //创建该属性 mc.CreateIndex(indexKey, IndexOptions.SetUnique(mongoField.Unique)); } } } return(true); } catch (Exception ex) { return(false); } }
public void Handle(TaxCreated message) { BsonDocument tax = BsonDocument.Parse(message.TaxJson); BsonDocument index = new BsonDocument(); index["_id"] = tax["_id"]; index["Name"] = tax["Name"]; index["Value"] = tax["Value"]; index["OwnerId"] = tax["OwnerId"]; index["Keywords"] = BsonValue.Create(new string[4] { tax["_id"].ToString(), tax["Name"].ToString(), tax["Value"].ToString(), tax["OwnerId"].ToString() }); Collections.Save(index); Collections.EnsureIndex(IndexKeys.Descending("Keywords"), IndexOptions.SetName("Keywords")); }
protected override async Task SetupCollectionAsync(IMongoCollection <MongoEventCommit> collection) { var indexNames = await Task.WhenAll( collection.Indexes.CreateOneAsync(IndexKeys.Ascending(x => x.EventsOffset), new CreateIndexOptions { Unique = true }), collection.Indexes.CreateOneAsync(IndexKeys.Ascending(x => x.EventStreamOffset).Ascending(x => x.EventStream), new CreateIndexOptions { Unique = true }), collection.Indexes.CreateOneAsync(IndexKeys.Descending(x => x.EventsOffset), new CreateIndexOptions { Unique = true }), collection.Indexes.CreateOneAsync(IndexKeys.Descending(x => x.EventStreamOffset).Ascending(x => x.EventStream), new CreateIndexOptions { Unique = true })); eventsOffsetIndex = indexNames[0]; }
protected override async Task SetupCollectionAsync(IMongoCollection <MongoDbEvent> collection, CancellationToken ct) { await Collection.Indexes.CreateOneAsync( new CreateIndexModel <MongoDbEvent>( IndexKeys .Ascending(x => x.Doc.AppId) .Ascending(x => x.Doc.Topic) .Ascending(x => x.SearchText) .Descending(x => x.Doc.Created)), null, ct); await Collection.Indexes.CreateOneAsync( new CreateIndexModel <MongoDbEvent>( IndexKeys .Descending(x => x.Doc.Created), new CreateIndexOptions { ExpireAfter = TimeSpan.FromDays(10) }), null, ct); }
public void Handle(ItemCreated message) { BsonDocument doc = BsonDocument.Parse(message.Data); BsonDocument index = new BsonDocument(); index["Keywords"] = BsonValue.Create(new string[5] { doc["_id"].ToString(), doc["OwnerId"].ToString(), doc["Code"].ToString(), doc["Barcode"].ToString(), doc["Name"].ToString() }); index["_id"] = doc["_id"]; index["OwnerId"] = doc["OwnerId"]; index["Code"] = doc["Code"]; index["Barcode"] = doc["Barcode"]; index["Name"] = doc["Name"]; Collections.Save(index); Collections.EnsureIndex(IndexKeys.Descending("Keywords"), IndexOptions.SetName("Keywords")); }
public Task CreateCollectionIfNotExist(string route) { var collectionName = _mongoAgent.GetEnvelopsCollectionName(route); var db = _mongoAgent.GetDb(); if (!db.CollectionExists(collectionName)) { var collectionOptionsBuilder = new CollectionOptionsBuilder() .SetCapped(true) .SetMaxDocuments(1000000) .SetMaxSize(500000000); db.CreateCollection(collectionName, collectionOptionsBuilder); var collection = db.GetCollection(collectionName); collection.CreateIndex(IndexKeys <Envelope> .Ascending(x => x.IsRead)); collection.CreateIndex(IndexKeys <Envelope> .Descending(x => x.ReadAt).Ascending(x => x.ProcessedAt)); collection.CreateIndex(IndexKeys <Envelope> .Descending(x => x.ProcessingStartedAt).Ascending(x => x.ProcessedAt)); collection.CreateIndex(IndexKeys <Envelope> .Descending(x => x.Id).Ascending(x => x.ProcessedAt)); } return(Task.FromResult(true)); }
public void Save(ScreenSummary entity) { try { IMongoQuery queryBase = Query.And ( Query <ScreenSummary> .EQ <DateTime>(mem => mem.Date, entity.Date), Query <ScreenSummary> .EQ <Guid>(mem => mem.ApplicationId, entity.ApplicationId), Query <ScreenSummary> .EQ <string>(mem => mem.Version, entity.Version), Query <ScreenSummary> .EQ <PlatformType>(mem => mem.PlatformId, entity.PlatformId) ); IMongoUpdate update = Update <ScreenSummary> .SetOnInsert(x => x.Version, entity.Version) .SetOnInsert(x => x.Date, entity.Date) .SetOnInsert(x => x.ApplicationId, entity.ApplicationId) .SetOnInsert(x => x.PlatformId, entity.PlatformId) .AddToSet(x => x.Durations, entity.Durations.First()) .Inc(mem => mem.Count, entity.Count); this.GetCollection <ScreenSummary>().FindAndModify(queryBase, SortBy.Descending("Date"), update, false, true); //TODO: need a more elegant solution going forward this.GetCollection <ScreenSummary>().EnsureIndex(IndexKeys.Descending("Date")); IMongoQuery queryDuration = Query.And ( queryBase, Query.EQ("Durations.Key", BsonValue.Create(entity.Durations.First().Key)) ); IMongoUpdate updateDuration = Update .Inc("Durations.$.Count", 1); this.GetCollection <ScreenSummary>().Update(queryDuration, updateDuration); } catch (Exception ex) { throw new DataAccessLayerException(ex); } }