public string CreateOne(string collectionName, CreateIndexModel <T> model, CreateOneIndexOptions options = null) { return(infrastructure.Exec(database => { return database.GetCollection <T>(collectionName).Indexes.CreateOne(model, options); })); }
public void MaxTime_set_should_have_expected_result( [Values(null, -10000, 0, 1, 9999, 10000, 10001)] int?maxTimeTicks) { var subject = new CreateOneIndexOptions(); var maxTime = maxTimeTicks == null ? (TimeSpan?)null : TimeSpan.FromTicks(maxTimeTicks.Value); subject.MaxTime = maxTime; subject.MaxTime.Should().Be(maxTime); }
public void MaxTime_get_should_return_expected_result() { var subject = new CreateOneIndexOptions { MaxTime = TimeSpan.FromSeconds(123) }; var result = subject.MaxTime; result.Should().Be(TimeSpan.FromSeconds(123)); }
public void MaxTime_set_should_throw_when_value_is_invalid( [Values(-10001, -9999, -1)] long maxTimeTicks) { var subject = new CreateOneIndexOptions(); var value = TimeSpan.FromTicks(maxTimeTicks); var exception = Record.Exception(() => subject.MaxTime = value); var e = exception.Should().BeOfType <ArgumentOutOfRangeException>().Subject; e.ParamName.Should().Be("value"); }
public void CommitQuorum_set_should_have_expected_result( [Values(null, 1, 2)] int?w) { var commitQuorum = w.HasValue ? CreateIndexCommitQuorum.Create(w.Value) : null; var subject = new CreateOneIndexOptions() { CommitQuorum = commitQuorum }; subject.CommitQuorum = commitQuorum; subject.CommitQuorum.Should().Be(commitQuorum); }
public void CommitQuorum_get_should_return_expected_result( [Values(null, 1, 2)] int?w) { var commitQuorum = w.HasValue ? CreateIndexCommitQuorum.Create(w.Value) : null; var subject = new CreateOneIndexOptions() { CommitQuorum = commitQuorum }; var result = subject.CommitQuorum; result.Should().BeSameAs(commitQuorum); }
//https://stackoverflow.com/questions/35019313/checking-if-an-index-exists-in-mongodb static async Task CreateIndexAsync <TEntity> (IMongoCollection <TEntity> collection, Expression <Func <TEntity, object> > field, CreateOneIndexOptions createOneIndexOptions = null, IndexKeyType indexKeyType = IndexKeyType.Ascending, ILogger <TContext> logger = default, CancellationToken cancellationToken = default) where TEntity : Entity { if (field == null) { throw new ArgumentNullException($"Field expression cannot be empty"); } CreateIndexModel <TEntity> indexModel; switch (indexKeyType) { case IndexKeyType.Descending: indexModel = new CreateIndexModel <TEntity> (Builders <TEntity> .IndexKeys.Descending(field)); break; case IndexKeyType.Text: indexModel = new CreateIndexModel <TEntity> (Builders <TEntity> .IndexKeys.Text(field)); break; case IndexKeyType.Hashed: indexModel = new CreateIndexModel <TEntity> (Builders <TEntity> .IndexKeys.Hashed(field)); break; default: indexModel = new CreateIndexModel <TEntity> (Builders <TEntity> .IndexKeys.Ascending(field)); break; } try { await collection.Indexes .CreateOneAsync(indexModel, createOneIndexOptions, cancellationToken) .ConfigureAwait(false); } catch (MongoConnectionException mongoConnectionException) { logger?.LogError(mongoConnectionException, "Passbridge MongoDb database connection error"); } catch (TimeoutException timeoutException) { logger?.LogError(timeoutException, "Timeout Exception in CreateIndexAsync method"); } catch (Exception ex) { logger?.LogError(ex, "Something is wrong is MongoDB"); } }
public Task <string> CreateOneAsync(IClientSessionHandle session, CreateIndexModel <Book> model, CreateOneIndexOptions options = null, CancellationToken cancellationToken = default) => null;
public string CreateOne(IClientSessionHandle session, CreateIndexModel <Book> model, CreateOneIndexOptions options = null, CancellationToken cancellationToken = default) => "";
public Task <string> CreateOneAsync(CreateIndexModel <Book> model, CreateOneIndexOptions options = null, CancellationToken cancellationToken = default) => null;
public Task <string> CreateOneAsync(IClientSessionHandle session, CreateIndexModel <TDocument> model, CreateOneIndexOptions options = null, CancellationToken cancellationToken = default) { return(Task.FromResult(CreateOne(session, model, options, cancellationToken))); }
public string CreateOne(CreateIndexModel <Book> model, CreateOneIndexOptions options = null, CancellationToken cancellationToken = default) => "index name here";
public string CreateOne(CreateIndexModel <TDocument> model, CreateOneIndexOptions options = null, CancellationToken cancellationToken = default) { return(CreateOne(null, model, options, cancellationToken)); }
public Task <string> CreateOneAsync(string collectionName, CreateIndexModel <T> model, CreateOneIndexOptions options = null, CancellationToken cancellationToken = default) { return(infrastructure.Exec(database => { return database.GetCollection <T>(collectionName).Indexes.CreateOneAsync(model, options, cancellationToken); })); }
public Task <string> CreateOneAsync(CreateIndexModel <T> model, CreateOneIndexOptions options = null, CancellationToken cancellationToken = default) => CreateOneAsync(collectionTypeName, model, options);
private void CreateIndex <TDoc>(IMongoCollection <TDoc> mongoCollection, string[] indexFields, CreateIndexModel <TDoc> model, CreateOneIndexOptions options = null) { if (indexFields == null) { return; } var indexKeys = Builders <TDoc> .IndexKeys; IndexKeysDefinition <TDoc> keys = null; if (indexFields.Length > 0) { keys = indexKeys.Descending(indexFields[0]); } for (var i = 1; i < indexFields.Length; i++) { var strIndex = indexFields[i]; keys = keys.Descending(strIndex); } if (keys != null) { mongoCollection.Indexes.CreateOne(model, options); } }
public string CreateOne(CreateIndexModel <T> model, CreateOneIndexOptions options = null) => CreateOne(collectionTypeName, model, options);
static async Task CreateCombinedIndex <TEntity> (IMongoCollection <TEntity> collection, CreateOneIndexOptions createOneIndexOptions = null, IndexKeyType indexKeyType = IndexKeyType.Ascending, ILogger <TContext> logger = default, CancellationToken cancellationToken = default, params IndexKeysDefinition <TEntity>[] keys) where TEntity : Entity { if (keys == null) { throw new ArgumentNullException($"Field expression cannot be empty"); } var indexModel = new CreateIndexModel <TEntity> (Builders <TEntity> .IndexKeys.Combine(keys)); await collection.Indexes.CreateOneAsync(indexModel, createOneIndexOptions, cancellationToken).ConfigureAwait(false); }
public string CreateOne(IClientSessionHandle session, CreateIndexModel <TDocument> model, CreateOneIndexOptions options = null, CancellationToken cancellationToken = default) { return(Insert(model)); }