public Task EnsureCollectionExistsAsync(IWriteBinding binding) { var requests = new[] { new InsertRequest(new BsonDocument("_id", 1)) }; var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings); return(operation.ExecuteAsync(binding, CancellationToken.None)); }
// helper methods private void EnsureDatabaseExists(IWriteBinding binding) { var collectionNamespace = new CollectionNamespace(_databaseNamespace, "test"); var requests = new[] { new InsertRequest(new BsonDocument()) }; var insertOperation = new BulkInsertOperation(collectionNamespace, requests, _messageEncoderSettings); insertOperation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult(); }
// methods public async Task <WriteConcernResult> ExecuteAsync(IChannelHandle channel, CancellationToken cancellationToken) { Ensure.IsNotNull(channel, "channel"); var requests = _documentSource.GetRemainingItems().Select(d => { if (d == null) { throw new ArgumentException("Batch contains one or more null documents."); } return(new InsertRequest(new BsonDocumentWrapper(d, _serializer))); }); var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings) { IsOrdered = !_continueOnError, MaxBatchCount = _maxBatchCount, MaxBatchLength = _maxMessageSize, // ReaderSettings = ? WriteConcern = _writeConcern, // WriteSettings = ? }; BulkWriteOperationResult bulkWriteResult; MongoBulkWriteOperationException bulkWriteException = null; try { bulkWriteResult = await operation.ExecuteAsync(channel, cancellationToken).ConfigureAwait(false); } catch (MongoBulkWriteOperationException ex) { bulkWriteResult = ex.Result; bulkWriteException = ex; } var converter = new BulkWriteOperationResultConverter(); if (bulkWriteException != null) { throw converter.ToWriteConcernException(channel.ConnectionDescription.ConnectionId, bulkWriteException); } else { if (_writeConcern.IsAcknowledged) { return(converter.ToWriteConcernResult(bulkWriteResult)); } else { return(null); } } }
// methods public async Task <WriteConcernResult> ExecuteAsync(IConnectionHandle connection, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(connection, "connection"); var requests = _documentSource.GetRemainingItems().Select(d => { if (d == null) { throw new ArgumentException("Batch contains one or more null documents."); } return(new InsertRequest(new BsonDocumentWrapper(d, _serializer))); }); var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings) { IsOrdered = !_continueOnError, MaxBatchCount = _maxBatchCount ?? 0, // ReaderSettings = ? WriteConcern = _writeConcern, // WriteSettings = ? }; BulkWriteOperationResult bulkWriteResult; BulkWriteOperationException bulkWriteException = null; try { bulkWriteResult = await operation.ExecuteAsync(connection, timeout, cancellationToken); } catch (BulkWriteOperationException ex) { bulkWriteResult = ex.Result; bulkWriteException = ex; } var converter = new BulkWriteOperationResultConverter(); if (bulkWriteException != null) { throw converter.ToWriteConcernException(bulkWriteException); } else { if (_writeConcern.IsAcknowledged) { return(converter.ToWriteConcernResult(bulkWriteResult)); } else { return(null); } } }
private Task <BulkWriteOperationResult> ExecuteInsertsAsync(IChannelHandle channel, IEnumerable <InsertRequest> requests, CancellationToken cancellationToken) { var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings) { MaxBatchCount = _maxBatchCount, MaxBatchLength = _maxBatchLength, IsOrdered = _isOrdered, MessageEncoderSettings = _messageEncoderSettings, WriteConcern = _writeConcern }; return(operation.ExecuteAsync(channel, cancellationToken)); }
private Task <BulkWriteResult> ExecuteInsertsAsync(IConnectionHandle connection, IEnumerable <InsertRequest> requests, TimeSpan timeout, CancellationToken cancellationToken) { var operation = new BulkInsertOperation(_databaseName, _collectionName, requests) { AssignId = _assignId, CheckElementNames = _checkElementNames, MaxBatchCount = _maxBatchCount, MaxBatchLength = _maxBatchLength, IsOrdered = _isOrdered, ReaderSettings = _readerSettings, WriteConcern = _writeConcern, WriterSettings = _writerSettings }; return(operation.ExecuteAsync(connection, timeout, cancellationToken)); }
public Task EnsureCollectionExistsAsync(IWriteBinding binding) { var requests = new[] { new InsertRequest(new BsonDocument("_id", 1)) }; var operation = new BulkInsertOperation(_collectionNamespace, requests, _messageEncoderSettings); return operation.ExecuteAsync(binding, CancellationToken.None); }
// helper methods private void EnsureDatabaseExists(IWriteBinding binding) { var collectionNamespace = new CollectionNamespace(_databaseNamespace, "test"); var requests = new[] { new InsertRequest(new BsonDocument()) }; var insertOperation = new BulkInsertOperation(collectionNamespace, requests, _messageEncoderSettings); insertOperation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult(); }