public UnifiedInsertManyOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            List <BsonDocument>  documents = null;
            InsertManyOptions    options   = null;
            IClientSessionHandle session   = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "documents":
                    documents = argument.Value.AsBsonArray.Cast <BsonDocument>().ToList();
                    break;

                case "ordered":
                    options           = options ?? new InsertManyOptions();
                    options.IsOrdered = argument.Value.AsBoolean;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid InsertManyOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedInsertManyOperation(session, collection, documents, options));
        }
예제 #2
0
 /// <summary>
 /// 插入多条
 /// </summary>
 /// <typeparam name="T">文档类型</typeparam>
 /// <param name="collection">集合</param>
 /// <param name="documents">文档对象</param>
 /// <param name="options">插入操作设置</param>
 /// <param name="cancellationToken">取消操作设置</param>
 public static void AddMany <T>(this IMongoCollection <T> collection,
                                IEnumerable <T> documents,
                                InsertManyOptions options           = null,
                                CancellationToken cancellationToken = default)
 {
     collection.InsertMany(documents, options, cancellationToken);
 }
예제 #3
0
        public MongoDbRepository(
            IMapper mapper,
            ILogger <MongoDbRepository> logger,
            IOptions <MongoOptions> options)
        {
            this.mapper = mapper;
            this.logger = logger;

            var internalIdentity = new MongoInternalIdentity(Constants.ADMIN, options.Value.Username);
            var passwordEvidence = new PasswordEvidence(options.Value.Password);
            var mongoCredential  = new MongoCredential(options.Value.AuthMechanism, internalIdentity, passwordEvidence);

            var settings = new MongoClientSettings
            {
                Credential         = mongoCredential,
                Server             = new MongoServerAddress(options.Value.MongoHost, int.Parse(options.Value.Port)),
                GuidRepresentation = GuidRepresentation.Standard
            };

            var client = new MongoClient(settings);

            this.database = client.GetDatabase(options.Value.DefaultDb);

            this.insertManyOptions = new InsertManyOptions
            {
                BypassDocumentValidation = false,
                IsOrdered = false
            };

            this.insertOneOptions = new InsertOneOptions
            {
                BypassDocumentValidation = false
            };
        }
예제 #4
0
 /// <summary>
 /// 同步添加多条记录
 /// </summary>
 /// <param name="collectionName"></param>
 /// <param name="documents"></param>
 /// <param name="options"></param>
 public void BulkAdd(string collectionName, IEnumerable <T> documents, InsertManyOptions options = null)
 {
     infrastructure.Exec(database =>
     {
         database.GetCollection <T>(collectionName).InsertMany(documents, options);
     });
 }
예제 #5
0
 public void InsertMany <T>(IEnumerable <T> items, InsertManyOptions options = null) where T : IMongoEntity
 {
     foreach (var item in items)
     {
         Data[typeof(T)].Add(item);
     }
 }
예제 #6
0
 public async Task BulkInsertAsync(IEnumerable <TModel> models, InsertManyOptions options = null, CancellationToken token = default)
 {
     try
     {
         await _collection.InsertManyAsync(models, options, token);
     }
     catch (MongoAuthenticationException mongoAuthEx)
     {
         throw new NautilusMongoDbException("Mongo security error", mongoAuthEx);
     }
     catch (MongoConnectionException mongoConnectEx)
     {
         throw new NautilusMongoDbException(mongoConnectEx.Message, mongoConnectEx);
     }
     catch (MongoWriteException mongoWriteEx)
     {
         throw new NautilusMongoDbException("Mongo write error", mongoWriteEx);
     }
     catch (MongoCommandException mongoCmdEx)
     {
         throw new NautilusMongoDbException("Mongo command error", mongoCmdEx);
     }
     catch (TimeoutException timeoutEx)
     {
         throw new NautilusMongoDbException("Mongo has timed out", timeoutEx);
     }
     catch (Exception ex)
     {
         throw new NautilusMongoDbException("Mongo throws a general exception", ex);
     }
 }
예제 #7
0
 /// <summary>
 /// 异步添加多条数据
 /// </summary>
 /// <param name="collectionName"></param>
 /// <param name="documents"></param>
 /// <param name="options"></param>
 /// <param name="cancellationToken"></param>
 /// <returns></returns>
 public Task BulkAddAsync(string collectionName, IEnumerable <T> documents, InsertManyOptions options = null, CancellationToken cancellationToken = default)
 {
     return(infrastructure.Exec(database =>
     {
         return database.GetCollection <T>(collectionName).InsertManyAsync(documents, options, cancellationToken);
     }));
 }
예제 #8
0
        /// <summary>
        /// 批量插入。
        /// </summary>
        /// <param name="entities">插入实体集合。</param>
        /// <param name="options">插入数据配置项。</param>
        public void InsertMany(IEnumerable <TEntity> entities, InsertManyOptions options = null)
        {
            string collectionName = typeof(TEntity).Name;
            var    colleciton     = GetMongoCollection(collectionName);

            colleciton.InsertMany(entities, options);
        }
예제 #9
0
 public override void InsertMany(IEnumerable <TVal> documents, InsertManyOptions options = null, CancellationToken cancellationToken = new CancellationToken())
 {
     foreach (TVal doc in documents)
     {
         InsertOne(doc, null, cancellationToken);
     }
 }
 public override void InsertMany(IEnumerable <TDocument> documents, InsertManyOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     foreach (var document in documents)
     {
         var bsonDocument = SerializeDocument(document);
         _documents.Add(bsonDocument);
     }
 }
예제 #11
0
        public Task Insert(IEnumerable <TValue> items)
        {
            var options = new InsertManyOptions {
                BypassDocumentValidation = true
            };

            return(_collection.InsertManyAsync(items, options));
        }
예제 #12
0
        /// <summary>
        /// 异步批量插入。
        /// </summary>
        /// <param name="entities">插入实体集合。</param>
        /// <param name="options">插入数据配置项。</param>
        public async Task InsertManyAsync(IEnumerable <TEntity> entities, InsertManyOptions options = null)
        {
            string collectionName = typeof(TEntity).Name;
            var    colleciton     = GetMongoCollection(collectionName);
            await colleciton.InsertManyAsync(entities, options);

            await this.SyncContext;
        }
예제 #13
0
        async Task <IReadOnlyList <TEntity> > ICollectionAdapter <TEntity> .InsertManyAsync(
            IReadOnlyList <TEntity> entities,
            InsertManyOptions options,
            CancellationToken token)
        {
            await this.collection.InsertManyAsync(entities, options, token);

            return(entities);
        }
 /// <summary>
 /// 插入多条
 /// </summary>
 /// <typeparam name="T">文档类型</typeparam>
 /// <param name="collection">集合</param>
 /// <param name="documents">文档对象</param>
 /// <param name="options">插入操作设置</param>
 /// <param name="cancellationToken">取消操作设置</param>
 public static Task AddManyAsync <T>(
     this IMongoCollection <T> collection,
     IEnumerable <T> documents,
     InsertManyOptions options           = null,
     CancellationToken cancellationToken = default)
     where T : BaseMongoEntity
 {
     return(Task.Run(() => AddMany(collection, documents, options, cancellationToken)));
 }
예제 #15
0
 public ICallistoInsertMany <T> CreateInsertMany <T>(IEnumerable <T> values, InsertManyOptions insertOneOptions, string operationName)
     where T : class, IDocumentRoot
 {
     // if (values is null)
     //     throw new CallistoException($"No insert can be made with a null {nameof(T)} instance");
     // if (!values.Any())
     //     throw new CallistoException("There is no need to insert an empty list of values into the database.");
     return(new DefaultCallistoInsertMany <T>(operationName, values, insertOneOptions));
 }
 public UnifiedInsertManyOperation(
     IClientSessionHandle session,
     IMongoCollection <BsonDocument> collection,
     List <BsonDocument> documents,
     InsertManyOptions options)
 {
     _session    = session;
     _collection = collection;
     _documents  = documents;
     _options    = options;
 }
예제 #17
0
        //methods
        public virtual Task Insert(List <TCategory> settings)
        {
            var options = new InsertManyOptions()
            {
                IsOrdered = false
            };

            return(_collectionFactory
                   .GetCollection <TCategory>()
                   .InsertManyAsync(settings, options));
        }
예제 #18
0
        public void Supported_multi_statement_writes_should_have_transaction_id(
            [Values("insertMany", "bulkWrite")] string operation,
            [Values(false, true)] bool ordered,
            [Values(false, true)] bool async)
        {
            RequireServer.Check().VersionGreaterThanOrEqualTo("3.6.0").ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded);

            DropCollection();
            var eventCapturer = CreateEventCapturer();

            using (var client = CreateDisposableClient(eventCapturer))
            {
                var database   = client.GetDatabase(_databaseName);
                var collection = database.GetCollection <BsonDocument>(_collectionName);

                switch (operation)
                {
                case "bulkWrite":
                    var requests         = new[] { new InsertOneModel <BsonDocument>(new BsonDocument("_id", 1)) };
                    var bulkWriteOptions = new BulkWriteOptions {
                        IsOrdered = ordered
                    };
                    if (async)
                    {
                        collection.BulkWriteAsync(requests, bulkWriteOptions).GetAwaiter().GetResult();
                    }
                    else
                    {
                        collection.BulkWrite(requests, bulkWriteOptions);
                    }
                    break;

                case "insertMany":
                    var documents         = new[] { new BsonDocument("_id", 1) };
                    var insertManyOptions = new InsertManyOptions {
                        IsOrdered = ordered
                    };
                    if (async)
                    {
                        collection.InsertManyAsync(documents, insertManyOptions).GetAwaiter().GetResult();
                    }
                    else
                    {
                        collection.InsertMany(documents, insertManyOptions);
                    }
                    break;

                default:
                    throw new Exception($"Unexpected operation: {operation}.");
                }

                AssertCommandHasTransactionId(eventCapturer);
            }
        }
예제 #19
0
        //methods
        public Task Insert(List <DispatchTemplate <ObjectId> > items)
        {
            var options = new InsertManyOptions()
            {
                IsOrdered = true
            };

            return(_collectionFactory
                   .GetCollection <DispatchTemplate <ObjectId> >()
                   .InsertManyAsync(items, options));
        }
예제 #20
0
 /// <summary>
 /// Insert all entities to database
 /// </summary>
 /// <param name="entities">Entity list model</param>
 /// <param name="insertManyOptions">To set insert options</param>
 /// <param name="cancellationToken">Token</param>
 /// <typeparam name="TEntity">Entity type</typeparam>
 /// <returns></returns>
 public async Task SaveAllAsync <TEntity> (IEnumerable <TEntity> entities, InsertManyOptions insertManyOptions = null, CancellationToken cancellationToken = default) where TEntity : Entity
 {
     try {
         await GetCollection <TEntity> ().InsertManyAsync(entities, insertManyOptions, cancellationToken);
     } catch (TimeoutException ex) {
         _logger?.LogError($"Timeout Exception in SaveAsync method. Source: {ex.Source}");
     } catch (MongoAuthenticationException ex) {
         _logger?.LogError($"Mongo Authentication Exception in SaveAsync method. Source: {ex.Source}");
     } catch (Exception ex) {
         _logger?.LogError(ex, $"These records cannot be saved");
     }
 }
예제 #21
0
        //methods
        public virtual async Task Insert(List <SubscriberScheduleSettings <ObjectId> > periods)
        {
            var options = new InsertManyOptions()
            {
                IsOrdered = false
            };

            await _collectionFactory
            .GetCollection <SubscriberScheduleSettings <ObjectId> >()
            .InsertManyAsync(periods, options)
            .ConfigureAwait(false);
        }
예제 #22
0
        //methods
        public virtual async Task Insert(List <TTopic> settings)
        {
            var options = new InsertManyOptions()
            {
                IsOrdered = false
            };

            await _collectionFactory
            .GetCollection <TTopic>()
            .InsertManyAsync(settings, options)
            .ConfigureAwait(false);
        }
        //methods
        public virtual async Task Insert(List <StoredNotification <ObjectId> > items)
        {
            var options = new InsertManyOptions()
            {
                IsOrdered = true
            };

            await _collectionFactory
            .GetCollection <StoredNotification <ObjectId> >()
            .InsertManyAsync(items, options)
            .ConfigureAwait(false);
        }
예제 #24
0
        //Insert
        public virtual async Task Insert(List <SignalBounce <ObjectId> > messages)
        {
            var options = new InsertManyOptions()
            {
                IsOrdered = false
            };

            await _collectionFactory
            .GetCollection <SignalBounce <ObjectId> >()
            .InsertManyAsync(messages, options)
            .ConfigureAwait(false);
        }
예제 #25
0
        public async Task <Response <string> > SaveCache(MongoAccess mongo)
        {
            Response <string> response = new Response <string>();
            var col = mongo.DBContext.GetCollection <GameModel>("games");
            InsertManyOptions options = new InsertManyOptions();

            options.BypassDocumentValidation = true;
            await col.InsertManyAsync(Cache, options);


            return(response);
        }
 public MongoOutputAdapter(IConfigurationService configurationService, IVinterMongoDBClient dbClient)
 {
     this._configurationService = configurationService;
     this.dbClient        = dbClient;
     this.database        = null;
     this.frameCollection = null;
     this.bodyCollection  = null;
     this._buffer         = new List <MocapFrame>();
     this._bufferSize     = this._configurationService.GetConfiguration().Mongo.MongoBufferSize;
     this.Enabled         = this._configurationService.GetConfiguration().Mongo.Enabled;
     this.Write           = this._configurationService.GetConfiguration().Mongo.Write;
     this._insertOptions  = new InsertManyOptions();
 }
예제 #27
0
        /// <summary>
        /// 创建多条数据
        /// </summary>
        /// <param name="data">基于数据类的数据列表</param>
        /// <param name="option">创建参数</param>
        /// <returns>是否成功</returns>
        protected async Task <bool> CreateManyAsync(IEnumerable <T> data, InsertManyOptions option = null)
        {
            try
            {
                await DBContext.InsertManyAsync(data, option);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
예제 #28
0
        /// <summary>
        /// Dùng để insert Async một array documents
        /// </summary>
        /// <param name="arrT"></param>
        /// <param name="clientSession"></param>
        /// <returns></returns>
        public async Task InsertManyAsync(IList <T> arrT, IClientSessionHandle clientSession = null)
        {
            var options = new InsertManyOptions {
                IsOrdered = true
            };

            if (clientSession == null)
            {
                await _collection.InsertManyAsync(arrT, options);
            }
            else
            {
                await _collection.InsertManyAsync(clientSession, arrT, options);
            }
        }
예제 #29
0
        /// <summary>批量插入</summary>
        public async Task <AppDataSaveResult[]> InsertManyAsync(IndexDataSaveRequest <TDataSave> request)
        {
            var pairs = new List <PKSKeyValuePair <TDocument, AppDataSaveResult> >();

            foreach (var value in request.Values)
            {
                var pair = await Validate(value, false);

                pairs.Add(pair);
            }
            var options = new InsertManyOptions();
            await Accessor.InsertManyAsync(pairs.Select(e => e.Key));

            return(pairs.Select(e => e.Value).ToArray());
        }
예제 #30
0
        public async Task InsertManyLesson(List <Lesson> lessons, CancellationToken cancellationToken = default)
        {
            var options = new InsertManyOptions {
                IsOrdered = false, BypassDocumentValidation = false
            };

            try
            {
                await GetCollection().InsertManyAsync(lessons);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }