public async Task <ISchema> AddAsync(ISchema schema, string appSchemasCollectionName) { CreateCollectionOptions options = schema.Options.ToObject <CreateCollectionOptions>(); await _database.CreateCollectionAsync(schema.Name, options); return(await GenericRepository.AddAsync(schema, appSchemasCollectionName)); }
/// <summary> /// Cria a coleção se não existir /// </summary> /// <param name="limitada">Indica se a coleção será do tipo limitada ("capped")</param> /// <param name="tamanhoMaximo">Tamanho máximo da coleção</param> /// <param name="limiteDocumentos">Número máxio de documentos</param> protected async Task <IMongoCollection <TDocument> > CriarColecaoAsync(bool limitada, long?tamanhoMaximo, long?limiteDocumentos) { if (!(await ExisteColexaoAsync())) { if (limitada && tamanhoMaximo > 0) { CreateCollectionOptions options = new CreateCollectionOptions { Capped = limitada, MaxSize = tamanhoMaximo }; if (limiteDocumentos > 0) { options.MaxDocuments = limiteDocumentos; } await _db.CreateCollectionAsync(_nomeColecao, options); } else { await _db.CreateCollectionAsync(_nomeColecao); } } return(ColecaoMongo); }
/// <summary> /// Creates a collection. /// </summary> /// <param name="collectionName">The name of the collection to create.</param> /// <param name="ReuseExisting">If false, throws an exception if the collection exists.</param> /// <returns>Collection referente.</returns> public Collection CreateCollection(string collectionName, bool ReuseExisting = false) { ValidateOpenSession(); Collection coll = new Collection(this, collectionName); try { if (Session.Version.isAtLeast(8, 0, 19)) { CreateCollectionOptions options = new CreateCollectionOptions() { ReuseExisting = ReuseExisting }; Session.XSession.CreateCollection(Name, collectionName, options); } else { Session.XSession.CreateCollection(Name, collectionName); } } catch (MySqlException ex) when(ex.Code == 1050) { if (ReuseExisting) { return(coll); } throw ex; } return(new Collection(this, collectionName)); }
private void Initialize() { lock (Sync) { var mongoUrl = MongoUrl.Create(_connectionString); var mongoClient = new MongoClient(mongoUrl); //var server = mongoClient.GetServer(); var database = mongoClient.GetDatabase(mongoUrl.DatabaseName); //var filter = new Filter(new BsonDocument("name", _collectionName)); var findThisOne = new ListCollectionsOptions(); findThisOne.Filter = Builders <BsonDocument> .Filter.Eq("name", _collectionName); var cursor = database.ListCollectionsAsync(findThisOne).Result; var list = cursor.ToListAsync().GetAwaiter().GetResult(); var allCollections = list.Select(c => c["name"].AsString).OrderBy(n => n).ToList(); if (!allCollections.Contains(_collectionName)) { var options = new CreateCollectionOptions(); options.Capped = true; options.AutoIndexId = true; options.MaxSize = _maxSize; database.CreateCollectionAsync(_collectionName, options).GetAwaiter().GetResult(); } _collection = database.GetCollection <ErrorModel>(_collectionName); //_mongoInsertOptions = new MongoInsertOptions { CheckElementNames = false }; } }
/// <summary> /// Creates a collection including a schema validation. /// </summary> /// <param name="collectionName">The name of the collection to create.</param> /// <param name="options">This object hold the parameters required to create the collection.</param> /// <see cref="CreateCollectionOptions"/> /// <returns>Collection referente.</returns> public Collection CreateCollection(string collectionName, CreateCollectionOptions options) { ValidateOpenSession(); Collection coll = new Collection(this, collectionName); try { Session.XSession.CreateCollection(Name, collectionName, options); return(new Collection(this, collectionName)); } catch (MySqlException ex_1) when(ex_1.Code == 5015) { var msg = string.Format("{0}{1}{2}", ex_1.Message, ", ", ResourcesX.SchemaCreateCollectionMsg); throw new MySqlException(msg); } catch (MySqlException ex) when(ex.Code == 1050) { if (options.ReuseExisting) { return(coll); } throw ex; } catch (MySqlException ex) { throw ex; } }
// Purpose: Create collection 'collectionName' // Input: collectionName // result - [out] result // Output: true / false private bool CreateCollection(string collectionName, out string result) { try { if (CollectionExist(collectionName, out result)) { result = "Collection with name:[" + collectionName + "] already exists. "; return(true); } else { /* false to disable the automatic creation of an index on the _id field */ var createCollectionOptions = new CreateCollectionOptions(); _database.CreateCollection(collectionName, createCollectionOptions); result = "Creating a new collection with the name:[" + collectionName + "]"; return(true); } } catch (Exception e) { result = "Creating a new collection with the name:[" + collectionName + "] failed. " + e.Message; return(false); } }
public void CreateCollection(string Coleccion) { var options = new CreateCollectionOptions { Capped = true, MaxSize = 1024 * 1024 }; _cnx.CreateCollection(Coleccion, options); }
/// <summary> /// Create collection in mongo database /// </summary> /// <param name="name"></param> /// <param name="options"></param> /// <returns></returns> public async Task CreateCollectionAsync(string name, CreateCollectionOptions options = null) { await mongoDatabaseContext.MongoDatabase.CreateCollectionAsync(name, new CreateCollectionOptions { AutoIndexId = true, Capped = true }); }
private void CreateCollection(IMongoDatabase db, string collectionName) { var cob = new CreateCollectionOptions(); SetCappedCollectionOptions(cob); db.CreateCollectionAsync(collectionName, cob).GetAwaiter().GetResult(); }
/// <summary> /// Creates a collection for an Entity type explicitly using the given options /// </summary> /// <typeparam name="T">The type of entity that will be stored in the created collection</typeparam> /// <param name="options">The options to use for collection creation</param> /// <param name="cancellation">An optional cancellation token</param> /// <param name="session">An optional session if using within a transaction</param> public static Task CreateCollectionAsync <T>(Action <CreateCollectionOptions <T> > options, CancellationToken cancellation = default, IClientSessionHandle session = null) where T : IEntity { var opts = new CreateCollectionOptions <T>(); options(opts); return(session == null ? Cache <T> .Collection.Database.CreateCollectionAsync(Cache <T> .CollectionName, opts, cancellation) : Cache <T> .Collection.Database.CreateCollectionAsync(session, Cache <T> .CollectionName, opts, cancellation)); }
public void CreateCollection(string dbName, string collectionName) { db = client.GetDatabase(dbName); var options = new CreateCollectionOptions { Capped = false }; db.CreateCollection(collectionName, options); }
/// <summary> /// Construct a sink posting to the specified database. /// </summary> /// <param name="databaseUrlOrConnStrName">The URL of a MongoDB database, or connection string name containing the URL.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <param name="collectionName">Name of the MongoDb collection to use for the log. Default is "log".</param> /// <param name="collectionCreationOptions">Collection Creation Options for the log collection creation.</param> public MongoDBSink( string databaseUrlOrConnStrName, int batchPostingLimit = DefaultBatchPostingLimit, TimeSpan?period = null, IFormatProvider formatProvider = null, string collectionName = DefaultCollectionName, CreateCollectionOptions collectionCreationOptions = null) : this(DatabaseFromMongoUrl(databaseUrlOrConnStrName), batchPostingLimit, period, formatProvider, collectionName, collectionCreationOptions) { }
public UnifiedCreateCollectionOperation( IClientSessionHandle session, IMongoDatabase database, string collectionName, CreateCollectionOptions options) { _session = session; _database = database; _collectionName = collectionName; _options = options; }
/// <summary> /// Construct a sink posting to a specified database. /// </summary> /// <param name="database">The MongoDB database.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <param name="collectionName">Name of the MongoDb collection to use for the log. Default is "log".</param> /// <param name="collectionCreationOptions">Collection Creation Options for the log collection creation.</param> public MongoDBSink(IMongoDatabase database, int batchPostingLimit, TimeSpan period, IFormatProvider formatProvider, string collectionName, CreateCollectionOptions collectionCreationOptions) : base(batchPostingLimit, period) { if (database == null) { throw new ArgumentNullException("database"); } _mongoDatabase = database; _collectionName = collectionName; _collectionCreationOptions = collectionCreationOptions; _formatProvider = formatProvider; }
public void Tailable_cursor_should_be_able_to_be_cancelled_from_a_different_thread_with_expected_result() { RequireServer.Check().Supports(Feature.TailableCursor); string testCollectionName = "test"; string testDatabaseName = "test"; var client = DriverTestConfiguration.Client; var database = client.GetDatabase(testDatabaseName); var collection = database.GetCollection <BsonDocument>(testCollectionName); DropCollection(client, testDatabaseName, testCollectionName); var createCollectionOptions = new CreateCollectionOptions() { Capped = true, MaxSize = 1000 }; database.CreateCollection(testCollectionName, createCollectionOptions); collection.InsertOne(new BsonDocument()); // tailable cursors don't work on an empty collection using (var cancellationTokenSource = new CancellationTokenSource()) { var findOptions = new FindOptions <BsonDocument>() { BatchSize = 1, CursorType = CursorType.TailableAwait }; var cursor = collection.FindSync(FilterDefinition <BsonDocument> .Empty, findOptions); var enumerator = cursor.ToEnumerable(cancellationTokenSource.Token).GetEnumerator(); var semaphore = new SemaphoreSlim(0); var thread = new Thread(() => { semaphore.Wait(); cancellationTokenSource.Cancel(); }); thread.Start(); var exception = Record.Exception((Action)(() => { while (true) { _ = enumerator.MoveNext(); semaphore.Release(1); } })); exception.Should().BeAssignableTo <OperationCanceledException>(); } }
private void InitDB() { client = new MongoClient(); database = client.GetDatabase(databaseName); if (!CollectionExists(database, collectionName)) { CreateCollectionOptions options = new CreateCollectionOptions { AutoIndexId = true }; database.CreateCollection(collectionName, options); } }
/// <summary> /// Construct a sink posting to a specified database. /// </summary> /// <param name="database">The MongoDB database.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <param name="collectionName">Name of the MongoDb collection to use for the log. Default is "log".</param> /// <param name="collectionCreationOptions">Collection Creation Options for the log collection creation.</param> /// <param name="mongoDBJsonFormatter">Formatter to produce json for MongoDB.</param> public MongoDBSink(IMongoDatabase database, string collectionName = DEFAULT_COLLECTION_NAME, int batchPostingLimit = DEFAULT_BATCH_POSTING_LIMIT, TimeSpan?period = null, IFormatProvider formatProvider = null, CreateCollectionOptions collectionCreationOptions = null, ITextFormatter mongoDBJsonFormatter = null) : base(batchPostingLimit, period ?? DefaultPeriod) { _mongoDatabase = database ?? throw new ArgumentNullException(nameof(database)); _collectionName = collectionName; _mongoDbJsonFormatter = mongoDBJsonFormatter ?? new CustomJsonFormatter(); _mongoDatabase.VerifyCollectionExists(_collectionName, collectionCreationOptions); }
public async Task CreateTable(string name, string collation) { var database = _database ?? TryReadMongoDatabase(); if (!string.IsNullOrEmpty(collation)) { var options = new CreateCollectionOptions(); options.Collation = new Collation(collation); await database.CreateCollectionAsync(name, options); } else { await database.CreateCollectionAsync(name); } }
/// <summary> /// Construct a sink posting to the specified database. /// </summary> /// <param name="databaseUrlOrConnStrName">The URL of a MongoDB database, or connection string name containing the URL.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <param name="collectionName">Name of the MongoDb collection to use for the log. Default is "log".</param> /// <param name="collectionCreationOptions">Collection Creation Options for the log collection creation.</param> /// <param name="mongoDBJsonFormatter">Formatter to produce json for MongoDB.</param> public MongoDBSink(string databaseUrlOrConnStrName, int batchPostingLimit = DEFAULT_BATCH_POSTING_LIMIT, TimeSpan?period = null, IFormatProvider formatProvider = null, string collectionName = DEFAULT_COLLECTION_NAME, CreateCollectionOptions collectionCreationOptions = null, ITextFormatter mongoDBJsonFormatter = null) : this(DatabaseFromMongoUrl(databaseUrlOrConnStrName), collectionName, batchPostingLimit, period, formatProvider, collectionCreationOptions, mongoDBJsonFormatter) { }
/// <summary> /// Construct a sink posting to a specified database. /// </summary> /// <param name="database">The MongoDB database.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <param name="collectionName">Name of the MongoDb collection to use for the log. Default is "log".</param> /// <param name="collectionCreationOptions">Collection Creation Options for the log collection creation.</param> public MongoDBSink(IMongoDatabase database, int batchPostingLimit, TimeSpan period, IFormatProvider formatProvider, string collectionName, CreateCollectionOptions collectionCreationOptions) : base(batchPostingLimit, period) { if (database == null) { throw new ArgumentNullException("database"); } BsonSerializer.RegisterSerializer(typeof(DateTime), new DateTimeSerializer(DateTimeKind.Local)); _mongoDatabase = database; _collectionName = collectionName; _collectionCreationOptions = collectionCreationOptions; _formatProvider = formatProvider; }
public bool Execute(IMongoDatabase database, MongoStorageOptions storageOptions, IMongoMigrationContext migrationContext) { var name = $@"{storageOptions.Prefix}.signal"; database.DropCollection(name); var createOptions = new CreateCollectionOptions { Capped = true, MaxSize = 1000000, MaxDocuments = 1000 }; database.CreateCollection(name, createOptions); return true; }
/// <summary> /// Creates a collection. /// </summary> /// <param retval="options">The options.</param> /// <returns>The create collection.</returns> public bool CreateCollection(CreateCollectionOptions options) { try { return(GetCollection <GenericCommandResponse>("$cmd").FindOne(new CreateCollectionRequest(options)).WasSuccessful); } catch (MongoException exception) { if (this._connection.StrictMode || exception.Message != "collection already exists") { throw; } return(false); } }
public async Task <bool> CreateAccountCollection() { string collectionName = GetCollectionName <AccountModel>(); var options = new CreateCollectionOptions { UsePowerOf2Sizes = true }; try { var collection = _db.GetCollection <AccountModel>(collectionName); if (collection != null) { return(true); } await _db.CreateCollectionAsync(collectionName, options); var uniqueOption = new CreateIndexOptions { Unique = true }; var emailField = new StringFieldDefinition <AccountModel>("EmailAddress"); var ssoTokenField = new StringFieldDefinition <AccountModel>("SsoToken"); var ssoProviderField = new StringFieldDefinition <AccountModel>("SsoProvider"); var emailIx = new IndexKeysDefinitionBuilder <AccountModel>().Ascending(emailField); var ssoTokenIx = new IndexKeysDefinitionBuilder <AccountModel>() .Ascending(ssoTokenField) .Ascending(ssoProviderField); IEnumerable <CreateIndexModel <AccountModel> > indicies = new List <CreateIndexModel <AccountModel> > { new CreateIndexModel <AccountModel>(emailIx), new CreateIndexModel <AccountModel>(ssoTokenIx) }; var result = await collection.Indexes.CreateManyAsync(indicies); return(true); } catch (MongoException ex) { Console.Error.WriteLine(ex.Message); return(false); } }
public UnifiedCreateCollectionOperation Build(string targetDatabaseId, BsonDocument arguments) { var database = _entityMap.GetDatabase(targetDatabaseId); string collectionName = null; CreateCollectionOptions createCollectionOptions = null; IClientSessionHandle session = null; foreach (var argument in arguments) { switch (argument.Name) { case "collection": collectionName = argument.Value.AsString; break; case "expireAfterSeconds": createCollectionOptions ??= new CreateCollectionOptions(); createCollectionOptions.ExpireAfter = TimeSpan.FromSeconds(argument.Value.ToInt64()); break; case "session": var sessionId = argument.Value.AsString; session = _entityMap.GetSession(sessionId); break; case "timeseries": var timeseries = argument.Value.AsBsonDocument; var timeField = timeseries["timeField"].AsString; var metaField = timeseries.TryGetValue("metaField", out var metaFieldValue) ? metaFieldValue.AsString : null; TimeSeriesGranularity?granularity = null; if (timeseries.TryGetValue("granularity", out var granularityValue)) { granularity = (TimeSeriesGranularity)Enum.Parse(typeof(TimeSeriesGranularity), granularityValue.AsString, true); } createCollectionOptions ??= new CreateCollectionOptions(); createCollectionOptions.TimeSeriesOptions = new TimeSeriesOptions(timeField, metaField, granularity); break; default: throw new FormatException($"Invalid CreateCollectionOperation argument name: '{argument.Name}'."); } } return(new UnifiedCreateCollectionOperation(session, database, collectionName, createCollectionOptions)); }
public async Task <bool> CreatePlayerCollection() { string collectionName = GetCollectionName <PlayerModel>(); var options = new CreateCollectionOptions { UsePowerOf2Sizes = true }; try { var collection = _db.GetCollection <PlayerModel>(collectionName); if (collection != null) { return(true); } await _db.CreateCollectionAsync(collectionName, options); var uniqueOption = new CreateIndexOptions { Unique = true }; var searchName = new StringFieldDefinition <PlayerModel>("PlayerSearchName"); var playerId = new StringFieldDefinition <PlayerModel>("PlayerIdentifier"); var playerRetro = new StringFieldDefinition <PlayerModel>("RetrosheetId"); var searchNameIx = new IndexKeysDefinitionBuilder <PlayerModel>().Text(searchName); var playerIdIx = new IndexKeysDefinitionBuilder <PlayerModel>().Ascending(playerId); var playerRetroIx = new IndexKeysDefinitionBuilder <PlayerModel>().Ascending(playerRetro); var indicies = new List <CreateIndexModel <PlayerModel> > { new CreateIndexModel <PlayerModel>(searchNameIx), new CreateIndexModel <PlayerModel>(playerIdIx), new CreateIndexModel <PlayerModel>(playerRetroIx) }; var result = await collection.Indexes.CreateManyAsync(indicies); return(result.Any()); } catch (MongoException mex) { Console.Error.WriteLine(mex.Message); return(false); } }
private void SetCappedCollectionOptions(CreateCollectionOptions options) { var unitResolver = new UnitResolver(); var newCollectionMaxSize = unitResolver.Resolve(NewCollectionMaxSize); var newCollectionMaxDocs = unitResolver.Resolve(NewCollectionMaxDocs); if (newCollectionMaxSize > 0) { options.Capped = true; options.MaxSize = newCollectionMaxSize; if (newCollectionMaxDocs > 0) { options.MaxDocuments = newCollectionMaxDocs; } } }
public void EnsureCollectionExists(string collectionName) { var database = GetDatabase(); if (IsCollectionExists(collectionName)) { return; } var options = new CreateCollectionOptions { Capped = IsCappedCollection, MaxSize = CappedCollectionSize, MaxDocuments = CappedCollectionMaxItems }; database.CreateCollection(collectionName, options); }
/// <summary> /// Gets a collection. If not exists, creates the collection with the specified options. /// </summary> /// <typeparam name="TDocument">the document type.</typeparam> /// <param name="database">MongoDB database.</param> /// <param name="options">MongoDB logger options.</param> /// <returns>An implementation of a collection.</returns> public static IMongoCollection <TDocument> GetCollection <TDocument>(this IMongoDatabase database, MongoDBLoggerOptions options) { var collection = database.GetCollection <TDocument>(options.CollectionName); if (!collection.Exists()) { var collectionOptions = new CreateCollectionOptions(); collectionOptions.Capped = options.Capped; if (options.Capped) { collectionOptions.MaxSize = options.MaxSize; collectionOptions.MaxDocuments = options.MaxDocuments; } database.CreateCollection(options.CollectionName, collectionOptions); } return(collection); }
private void InitDB() { client = new MongoClient(); Logger.Info(String.Format("Connecting to Mongo DB:{0} Collection:{1}", databaseName, collectionName)); database = client.GetDatabase(databaseName); if (CollectionExists(database, collectionName)) { return; } CreateCollectionOptions options = new CreateCollectionOptions { AutoIndexId = true }; database.CreateCollection(collectionName, options); }
/// <summary> /// Adds a sink that writes log events as documents to a capped collection in a MongoDb database. /// </summary> /// <param name="loggerConfiguration">The logger configuration.</param> /// <param name="database">The MongoDb database where the log collection will live.</param> /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> /// <param name="cappedMaxSizeMb">Max total size in megabytes of the created capped collection. (Default: 50mb)</param> /// <param name="cappedMaxDocuments">Max number of documents of the created capped collection.</param> /// <param name="collectionName">Name of the collection. Default is "log".</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <returns>Logger configuration, allowing configuration to continue.</returns> /// <exception cref="ArgumentNullException">A required parameter is null.</exception> public static LoggerConfiguration MongoDBCapped( this LoggerSinkConfiguration loggerConfiguration, IMongoDatabase database, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, long cappedMaxSizeMb = 50, long?cappedMaxDocuments = null, string collectionName = null, int batchPostingLimit = MongoDBSink.DefaultBatchPostingLimit, TimeSpan?period = null, IFormatProvider formatProvider = null) { if (loggerConfiguration == null) { throw new ArgumentNullException("loggerConfiguration"); } if (database == null) { throw new ArgumentNullException("database"); } var options = new CreateCollectionOptions() { Capped = true, MaxSize = cappedMaxSizeMb * 1024 * 1024 }; if (cappedMaxDocuments.HasValue) { options.MaxDocuments = cappedMaxDocuments.Value; } var defaultedPeriod = period ?? MongoDBSink.DefaultPeriod; return(loggerConfiguration.Sink( new MongoDBSink( database, batchPostingLimit, defaultedPeriod, formatProvider, collectionName ?? MongoDBSink.DefaultCollectionName, options), restrictedToMinimumLevel)); }
/// <summary> /// Initializes a new instance of the <see cref="CreateCollectionRequest"/> class. /// </summary> /// <param name="options">The options.</param> public CreateCollectionRequest(CreateCollectionOptions options) { this._options = options; }