public virtual void CreateSubscriberTopicSettingsIndex() { IndexKeysDefinition <SubscriberTopicSettings <ObjectId> > topicIndex = Builders <SubscriberTopicSettings <ObjectId> > .IndexKeys .Ascending(p => p.CategoryId) .Ascending(p => p.TopicId); CreateIndexOptions topicOptions = new CreateIndexOptions() { Unique = false }; var topicModel = new CreateIndexModel <SubscriberTopicSettings <ObjectId> >(topicIndex, topicOptions); IndexKeysDefinition <SubscriberTopicSettings <ObjectId> > subscriberIndex = Builders <SubscriberTopicSettings <ObjectId> > .IndexKeys .Ascending(p => p.SubscriberId); CreateIndexOptions subscriberOptions = new CreateIndexOptions() { Unique = false }; var subscriberModel = new CreateIndexModel <SubscriberTopicSettings <ObjectId> >(subscriberIndex, subscriberOptions); IMongoCollection <SubscriberTopicSettings <ObjectId> > collection = _context.SubscriberTopicSettings; string topicName = collection.Indexes.CreateOne(topicModel); string subscriberName = collection.Indexes.CreateOne(subscriberModel); }
public void Up(IMongoDatabase database) { var vehicleIndexDefinition = Builders <Vehicle> .IndexKeys.Combine( Builders <Vehicle> .IndexKeys.Ascending(v => v.ManufacturerName), Builders <Vehicle> .IndexKeys.Ascending(v => v.Model), Builders <Vehicle> .IndexKeys.Ascending(v => v.Generation), Builders <Vehicle> .IndexKeys.Ascending(v => v.StartProductionYear)); var engineVehicleTechSpecIndexDefinition = Builders <Vehicle> .IndexKeys.Combine( Builders <Vehicle> .IndexKeys.Ascending("VehicleTechSpecification.Engine.Name"), Builders <Vehicle> .IndexKeys.Ascending("VehicleTechSpecification.Engine.EngineCapacity"), Builders <Vehicle> .IndexKeys.Ascending("VehicleTechSpecification.Engine.HorsePowers"), Builders <Vehicle> .IndexKeys.Ascending("VehicleTechSpecification.Engine.Petrol")); var gearBoxVehicleTechSpecIndexDefinition = Builders <Vehicle> .IndexKeys.Combine( Builders <Vehicle> .IndexKeys.Ascending("VehicleTechSpecification.GearBox.Name"), Builders <Vehicle> .IndexKeys.Ascending("VehicleTechSpecification.GearBox.GearBoxType"), Builders <Vehicle> .IndexKeys.Ascending("VehicleTechSpecification.GearBox.GearsCount")); var indexDefinition = Builders <Vehicle> .IndexKeys.Combine( vehicleIndexDefinition, engineVehicleTechSpecIndexDefinition, gearBoxVehicleTechSpecIndexDefinition); var indexOptions = new CreateIndexOptions { Unique = true }; var indexModel = new CreateIndexModel <Vehicle>(indexDefinition, indexOptions); var vehiclesCollection = database.GetCollection <Vehicle>("Vehicles"); vehiclesCollection.Indexes.CreateOne(indexModel); }
public bool Execute(IMongoDatabase database, MongoStorageOptions storageOptions, IMongoMigrationContext migrationContext) { var cosmosStorageOptions = storageOptions as CosmosStorageOptions; if (cosmosStorageOptions != null) { database.CreateCollection(storageOptions.Prefix + ".notifications"); var collection = database.GetCollection <BsonDocument>(storageOptions.Prefix + ".notifications"); var options = new CreateIndexOptions { ExpireAfter = TimeSpan.FromHours(cosmosStorageOptions.CosmosHourlyTtl) }; var field = new StringFieldDefinition <BsonDocument>("_ts"); var indexDefinition = new IndexKeysDefinitionBuilder <BsonDocument>().Ascending(field); collection.Indexes.CreateOne(indexDefinition, options); } database.CreateCollection(storageOptions.Prefix + ".notifications", new CreateCollectionOptions { Capped = true, MaxSize = 1048576 * 16, // 16 MB, MaxDocuments = 100000, }); return(true); }
public static async Task <string> CreateDocumentIndex <T>( this IMongoCollection <T> collection, Func <IndexKeysDefinitionBuilder <T>, IndexKeysDefinition <T> > index, Action <CreateIndexOptions> configure, CancellationToken cancellationToken ) where T : Document { var options = new CreateIndexOptions(); configure?.Invoke(options); try { return(await CreateIndex()); } catch (MongoCommandException ex) when(ex.Message.Contains("already exists")) { Log.Error(ex, "Index already exists {@result}", ex.Result); } return(Empty); Task <string> CreateIndex() => collection.Indexes.CreateOneAsync( new CreateIndexModel <T>( index(Builders <T> .IndexKeys), options ), cancellationToken: cancellationToken ); }
public void setIndex(string mongodbConnStr, string mongodbDatabase, string coll, string indexDefinition, string indexName, bool isUnique = false) { var client = new MongoClient(mongodbConnStr); var database = client.GetDatabase(mongodbDatabase); var collection = database.GetCollection <BsonDocument>(coll); //检查是否已有设置index bool isSet = false; using (var cursor = collection.Indexes.List()) { JArray JAindexs = JArray.Parse(cursor.ToList().ToJson()); var query = JAindexs.Children().Where(index => (string)index["name"] == indexName); if (query.Count() > 0) { isSet = true; } // do something with the list... } if (!isSet) { try { var options = new CreateIndexOptions { Name = indexName, Unique = isUnique }; collection.Indexes.CreateOne(indexDefinition, options); } catch { } } client = null; }
private void TryCreateIndex(IndexKeysDefinition <MessageJournalEntryDocument> indexKeys, string name = null) { var options = new CreateIndexOptions { Name = name }; if (_collationSupported) { options.Collation = _collation; } try { _messageJournalEntries.Indexes.CreateOne(indexKeys, options); _diagnosticService.Emit(new MongoDBEventBuilder(this, MongoDBEventType.IndexCreated) { DatabaseName = _messageJournalEntries.Database.DatabaseNamespace.DatabaseName, CollectionName = _messageJournalEntries.CollectionNamespace.CollectionName, IndexName = name ?? indexKeys.ToString() }.Build()); } catch (Exception e) { _diagnosticService.Emit(new MongoDBEventBuilder(this, MongoDBEventType.IndexCreationFailed) { DatabaseName = _messageJournalEntries.Database.DatabaseNamespace.DatabaseName, CollectionName = _messageJournalEntries.CollectionNamespace.CollectionName, IndexName = name ?? indexKeys.ToString(), Exception = e }.Build()); } }
private IMongoCollection <BsonDocument> Collection(Mutator mutator = null) { var referenceCode = mutator?.SetCode ?? ""; lock (_collectionCache) { if (_collectionCache.ContainsKey(referenceCode)) { return(_collectionCache[referenceCode]); } _collectionCache[referenceCode] = Database.GetCollection <BsonDocument>(GetCollectionName(referenceCode)); try { var indexOptions = new CreateIndexOptions { Unique = false, Name = "fullTextSearch", Background = true }; var model = new CreateIndexModel <BsonDocument>(Builders <BsonDocument> .IndexKeys.Text("$**"), indexOptions); _collectionCache[referenceCode].Indexes.CreateOne(model); } catch (Exception e) { Current.Log.Info <T>(e.Message); } return(_collectionCache[referenceCode]); } }
private void CreateIndexes <T>(IMongoCollection <T> coll, string tableName, T item) { Dictionary <DBModelsMongoDbIndexAttribute, string> indexColumns = GetIndexColumns(item); List <string> columnNames = null; BsonDocument bsonIndex = null; int currentIndex = -1; bool isUnique = false; foreach (var index in indexColumns.OrderBy(o => o.Key.IndexNumber).ThenBy(o => o.Key.FieldOrder)) { if (currentIndex != index.Key.IndexNumber) { if (bsonIndex != null) { CreateIndexOptions options = new CreateIndexOptions(); options.Unique = isUnique; options.Name = tableName + "_" + string.Join("_", columnNames.ToArray()); coll.Indexes.CreateOne(bsonIndex, options); } currentIndex = index.Key.IndexNumber; columnNames = new List <string>(); bsonIndex = new BsonDocument(); } isUnique = index.Key.IsUnique; columnNames.Add(index.Value); bsonIndex.Add(index.Value, index.Key.SortOrder); } if (bsonIndex != null) { CreateIndexOptions options = new CreateIndexOptions(); options.Unique = isUnique; options.Name = tableName + "_" + string.Join("_", columnNames.ToArray()); coll.Indexes.CreateOne(bsonIndex, options); } }
/// <summary> /// Create indexes with <paramref name="indexNames"/> in the <paramref name="collection"/>. /// If an index already exists, and it is the same, it will be overwritten. /// If the index is different to an existing index, an exception will be thrown. /// </summary> /// <param name="collection"> /// The collection to create indexes in. /// </param> /// <param name="indexType"> /// The type of the indexes created. /// </param> /// <param name="indexNames"> /// The names of the indexes to create. /// </param> /// <returns> /// An <see cref="IEnumerable{String}"/> of the names of the indexes that were created. /// </returns> /// <exception cref="MongoCommandException"> /// Thrown if an existing index is attempted changed. /// </exception> /// <example> /// <code> /// collection.TryCreateIndexes(Builders{BsonDocument}.IndexKeys.Descending, "Name") /// </code> /// </example> internal static IEnumerable <string> TryCreateIndexes(this IMongoCollection <BsonDocument> collection, Func <FieldDefinition <BsonDocument>, IndexKeysDefinition <BsonDocument> > indexType, params string[] indexNames) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } if (indexType == null) { throw new ArgumentNullException(nameof(indexType)); } if (indexNames == null) { throw new ArgumentNullException(nameof(indexNames)); } if (indexNames.Length == 0) { throw new ArgumentException("Must have at least one index name", nameof(indexNames)); } var indexModels = indexNames.Select(indexName => { var index = indexType(indexName); var options = new CreateIndexOptions { Name = indexName, Sparse = true }; return(new CreateIndexModel <BsonDocument>(index, options)); }).ToList(); return(collection.Indexes.CreateMany(indexModels)); }
public virtual void CreateSignalDispatchHistoryIndex(TimeSpan?historyExpirationTime) { var receiverIndex = Builders <SignalDispatch <ObjectId> > .IndexKeys .Ascending(p => p.ReceiverSubscriberId) .Ascending(p => p.SendDateUtc); var receiverOptions = new CreateIndexOptions() { Unique = false }; var receiverModel = new CreateIndexModel <SignalDispatch <ObjectId> >(receiverIndex, receiverOptions); var ttlIndex = Builders <SignalDispatch <ObjectId> > .IndexKeys .Ascending(p => p.CreateDateUtc); CreateIndexOptions ttlOptions = new CreateIndexOptions() { Name = "CreateDateUtc TTL", Unique = false, ExpireAfter = historyExpirationTime }; var ttlModel = new CreateIndexModel <SignalDispatch <ObjectId> >(ttlIndex, ttlOptions); IMongoCollection <SignalDispatch <ObjectId> > collection = _context.SignalDispatchesHistory; string receiverName = collection.Indexes.CreateOne(receiverModel); if (historyExpirationTime != null) { string ttlName = collection.Indexes.CreateOne(ttlModel); } }
public bool VerifyOrCreateSchema() { var opts = new CreateIndexOptions() { Background = true }; opts.Name = "ProjectName-A"; if (!MongoUtil.IndexExists(Collection, opts.Name)) { Collection.Indexes.CreateOne(new CreateIndexModel <BsonDocument>(Builders <BsonDocument> .IndexKeys .Ascending(new StringFieldDefinition <BsonDocument, String>("ProjectName")) , opts)); } opts.Name = "SAST_LastScanDate-A"; if (!MongoUtil.IndexExists(Collection, opts.Name)) { Collection.Indexes.CreateOne(new CreateIndexModel <BsonDocument>(Builders <BsonDocument> .IndexKeys .Ascending(new StringFieldDefinition <BsonDocument, DateTime>("SAST_LastScanDate")) , opts)); } opts.Name = "SCA_LastScanDate-A"; if (!MongoUtil.IndexExists(Collection, opts.Name)) { Collection.Indexes.CreateOne(new CreateIndexModel <BsonDocument>(Builders <BsonDocument> .IndexKeys .Ascending(new StringFieldDefinition <BsonDocument, DateTime>("SCA_LastScanDate")) , opts)); } return(true); }
/// <summary> /// Creates all of the required MongoDB collections that this logset requires. /// </summary> private void CreateMongoDbCollections() { var collections = new Dictionary <string, HashSet <string> >(); ISet <IParser> parsers = parserFactory.GetAllParsers(); // Stuff collection names & indexes into the dictionary, deduping in the process. foreach (var parser in parsers) { var collectionName = parser.CollectionSchema.CollectionName.ToLowerInvariant(); IList <string> indexes = parser.CollectionSchema.Indexes; if (!collections.ContainsKey(collectionName)) { if (LogsetDependencyHelper.IsCollectionRequiredForRequest(collectionName, logsharkRequest)) { collections.Add(collectionName, new HashSet <string>()); } } // Add indexes. if (collections.ContainsKey(collectionName)) { foreach (var index in indexes) { if (collections.ContainsKey(collectionName)) { collections[collectionName].Add(index); } } } } // New up collections & indexes using the dictionary. foreach (var collection in collections) { var collectionName = collection.Key; ISet <string> indexes = collection.Value; var dbCollection = database.GetCollection <BsonDocument>(collectionName); logsharkRequest.RunContext.CollectionsGenerated.Add(collectionName); foreach (var index in indexes) { var indexKeysBuilder = new IndexKeysDefinitionBuilder <BsonDocument>(); CreateIndexOptions indexOptions = new CreateIndexOptions { Sparse = false }; dbCollection.Indexes.CreateOne(indexKeysBuilder.Ascending(index), indexOptions); } // If we are working against a sharded Mongo cluster, we need to explicitly shard each collection. MongoConnectionInfo mongoConnectionInfo = logsharkRequest.Configuration.MongoConnectionInfo; if (mongoConnectionInfo.ConnectionType == MongoConnectionType.ShardedCluster) { MongoAdminUtil.EnableShardingOnCollectionIfNotEnabled(mongoConnectionInfo.GetClient(), logsharkRequest.RunContext.MongoDatabaseName, collectionName); } } }
public override bool VerifyOrCreateSchema() { var opts = new CreateIndexOptions() { Background = true }; opts.Name = "ProjectName-A+ScanId-D"; if (!MongoUtil.IndexExists(Collection, opts.Name)) { Collection.Indexes.CreateOne(new CreateIndexModel <BsonDocument>(Builders <BsonDocument> .IndexKeys .Ascending(new StringFieldDefinition <BsonDocument, String>("ProjectName")) .Descending(new StringFieldDefinition <BsonDocument, String>("ScanId")) , opts)); } opts.Name = "High-A"; if (!MongoUtil.IndexExists(Collection, opts.Name)) { Collection.Indexes.CreateOne(new CreateIndexModel <BsonDocument>(Builders <BsonDocument> .IndexKeys .Ascending(new StringFieldDefinition <BsonDocument, Int64>("High")) , opts)); } opts.Name = "Medium-A"; if (!MongoUtil.IndexExists(Collection, opts.Name)) { Collection.Indexes.CreateOne(new CreateIndexModel <BsonDocument>(Builders <BsonDocument> .IndexKeys .Ascending(new StringFieldDefinition <BsonDocument, Int64>("Medium")) , opts)); } opts.Name = "Low-A"; if (!MongoUtil.IndexExists(Collection, opts.Name)) { Collection.Indexes.CreateOne(new CreateIndexModel <BsonDocument>(Builders <BsonDocument> .IndexKeys .Ascending(new StringFieldDefinition <BsonDocument, Int64>("Low")) , opts)); } opts.Name = "Information-A"; if (!MongoUtil.IndexExists(Collection, opts.Name)) { Collection.Indexes.CreateOne(new CreateIndexModel <BsonDocument>(Builders <BsonDocument> .IndexKeys .Ascending(new StringFieldDefinition <BsonDocument, Int64>("Information")) , opts)); } opts.Name = "ScanFinished-A"; if (!MongoUtil.IndexExists(Collection, opts.Name)) { Collection.Indexes.CreateOne(new CreateIndexModel <BsonDocument>(Builders <BsonDocument> .IndexKeys .Ascending(new StringFieldDefinition <BsonDocument, DateTime>("ScanFinished")) , opts)); } return(true); }
/// <inheritdoc/> public async Task <string> CreateOneIndexAsync(IndexKeysDefinition <T> keys, CreateIndexOptions options, CancellationToken cancellationToken = default) { var database = client.GetDatabase(databaseName); var myCollection = database.GetCollection <T>(collectionName); var index = new CreateIndexModel <T>(keys, options); return(await myCollection.Indexes.CreateOneAsync(index, cancellationToken : cancellationToken).ConfigureAwait(false)); }
static CreateIndexModel <Log> CreateIndexModel(string name, IndexKeysDefinition <Log> definition) { var options = new CreateIndexOptions { Name = name }; return(new CreateIndexModel <Log>(definition, options)); }
protected internal CreateIndexModelParameter(Type type, CreateIndexOptions options, BsonDocument rendered, MongoModelBuilder builder) { Type = type; Options = options; Rendered = rendered; Builder = builder; SetIndexesOnBuilder(Builder); }
/// <inheritdoc /> public virtual async Task Index( IndexKeysDefinition <TSchema> key, CreateIndexOptions <TSchema> options = null) { var model = new CreateIndexModel <TSchema>(key, options ?? new CreateIndexOptions()); await this.Collection.Indexes.CreateOneAsync(model); }
/// <summary> /// An index will be put on the place_id field by calling the default constructor. /// </summary> public CityRepository() { CreateIndexOptions options = new CreateIndexOptions { Unique = true }; _collection.Indexes.CreateOneAsync(Builders <City> .IndexKeys.Ascending(_ => _.place_id), options); }
/// <summary> /// 添加索引 /// </summary> public async Task AddIndex <T>(FieldDefinition <T> field) { var indexOptions = new CreateIndexOptions(); var indexKeys = Builders <T> .IndexKeys.Ascending(field); var indexModel = new CreateIndexModel <T>(indexKeys, indexOptions); await GetCollection <T>().Indexes.CreateOneAsync(indexModel); }
/// <summary> /// a constructor that makes sure we have a user email index over our users list. /// </summary> public UserRepository() { //unique index on email of a user CreateIndexOptions options = new CreateIndexOptions { Unique = true }; _collection.Indexes.CreateOneAsync(Builders <User> .IndexKeys.Ascending(_ => _.email), options); }
protected virtual async Task CreateIndex(string name, IndexKeysDefinition <TModel> keys) { var options = new CreateIndexOptions <TModel> { Name = name }; await GetCollection().Indexes.CreateOneAsync(keys, options); }
public static string CreateIndex <TDocument>(this IMongoCollection <TDocument> collection, Expression <Func <TDocument, object> > field) { var keys = Builders <TDocument> .IndexKeys.Ascending(field); var options = new CreateIndexOptions(); var model = new CreateIndexModel <TDocument>(keys, options); return(collection.Indexes.CreateOne(model)); }
public static void EnsureVendorIndex(IMongoCollection <Vendor> vendorProfileCollection) { var UniqueIndexOptions = new CreateIndexOptions(); UniqueIndexOptions.Unique = true; vendorProfileCollection.Indexes.CreateOne(Builders <Vendor> .IndexKeys.Ascending(x => x.UserId), UniqueIndexOptions); vendorProfileCollection.Indexes.CreateOne(Builders <Vendor> .IndexKeys.Descending(x => x.UserId), UniqueIndexOptions); }
public static void EnsureUniqueIndexOnPhoneNumber(IMongoCollection <User> userCollection) { CreateIndexOptions <User> options = new CreateIndexOptions <User>(); options.Unique = true; options.Sparse = true; userCollection.Indexes.CreateOne(Builders <User> .IndexKeys.Ascending(x => x.PhoneNumber), options); }
public static void EnsureHRIDIndex(IMongoCollection <HRIDEntity> hridCollection) { var hridIndexOptions = new CreateIndexOptions(); hridIndexOptions.Unique = true; hridCollection.Indexes.CreateOne(Builders <HRIDEntity> .IndexKeys.Ascending(x => x.HRID), hridIndexOptions); hridCollection.Indexes.CreateOne(Builders <HRIDEntity> .IndexKeys.Descending(x => x.HRID), hridIndexOptions); }
/// <summary> /// a constructor that makes sure we have a facebook id index over our FbUsers list. /// </summary> public FbUserRepository() { //unique index on fb pages id CreateIndexOptions options = new CreateIndexOptions { Unique = true }; _collection.Indexes.CreateOneAsync(Builders <FbUser> .IndexKeys.Ascending(_ => _.fbId), options); }
public static void EnsureIndexesOnUserType(IMongoCollection <User> userCollection) { CreateIndexOptions <User> options = new CreateIndexOptions <User>(); options.Background = true; userCollection.Indexes.CreateOne(Builders <User> .IndexKeys.Ascending(x => x.Type), options); userCollection.Indexes.CreateOne(Builders <User> .IndexKeys.Descending(x => x.Type), options); }
/// <summary> /// Find existing instance of ProcessManager /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mapper"></param> /// <param name="message"></param> /// <returns></returns> public IPersistanceData <T> FindData <T>(IProcessManagerPropertyMapper mapper, Message message) where T : class, IProcessManagerData { var mapping = mapper.Mappings.FirstOrDefault(m => m.MessageType == message.GetType()) ?? mapper.Mappings.First(m => m.MessageType == typeof(Message)); var collectionName = typeof(T).Name; IMongoCollection <MongoDbSslData <T> > collection = _mongoDatabase.GetCollection <MongoDbSslData <T> >(collectionName); var indexOptions = new CreateIndexOptions(); var indexKeys = Builders <MongoDbSslData <T> > .IndexKeys.Ascending("Data.CorrelationId"); var indexModel = new CreateIndexModel <MongoDbSslData <T> >(indexKeys, indexOptions); collection.Indexes.CreateOne(indexModel); object msgPropValue = null; try { msgPropValue = mapping.MessageProp.Invoke(message); } catch { return(null); } if (null == msgPropValue) { throw new ArgumentException("Message property expression evaluates to null"); } //Left ParameterExpression pe = Expression.Parameter(typeof(MongoDbSslData <T>), "t"); Expression left = Expression.Property(pe, typeof(MongoDbSslData <T>).GetTypeInfo().GetProperty("Data")); foreach (var prop in mapping.PropertiesHierarchy.Reverse()) { left = Expression.Property(left, left.Type, prop.Key); } //Right Expression right = Expression.Constant(msgPropValue, msgPropValue.GetType()); Expression expression; try { expression = Expression.Equal(left, right); } catch (InvalidOperationException ex) { throw new Exception("Mapped incompatible types of ProcessManager Data and Message properties.", ex); } Expression <Func <MongoDbSslData <T>, bool> > lambda = Expression.Lambda <Func <MongoDbSslData <T>, bool> >(expression, pe); return(collection.AsQueryable().FirstOrDefault(lambda)); }
/// <summary> /// Creates the index. /// </summary> private void CreateIndex() { var indexOptions = new CreateIndexOptions(); var indexKeys = Builders <Adviser> .IndexKeys.Ascending(adviser => adviser.UserDetails.Name); var indexModelName = new CreateIndexModel <Adviser>(indexKeys, indexOptions); DbSet.Indexes.CreateOne(indexModelName); }
public string createIndex(object keys, object options) { BsonDocumentIndexKeysDefinition <BsonDocument> keysDefinitions = new BsonDocumentIndexKeysDefinition <BsonDocument>(keys.ToBsonDocument()); CreateIndexOptions createIndexOptions = new CreateIndexOptions(); return(this._collection.Indexes.CreateOne(keysDefinitions, createIndexOptions)); }