Beispiel #1
0
        protected void BulkWriteAsync(IEnumerable <T> elements, int bulkSize)
        {
            if (elements == null)
            {
                throw new ArgumentNullException("elements");
            }

            if (bulkSize < 1)
            {
                throw new InvalidOperationException("bulkSize is small than 1.");
            }

            var cache   = new WriteModel <T> [bulkSize];
            var options = new BulkWriteOptions
            {
                BypassDocumentValidation = true,
                IsOrdered = false
            };

            var index = 0;

            foreach (var item in elements)
            {
                cache[index % BulkSize] = new InsertOneModel <T>(item);

                if (((index + 1) % bulkSize) == 0)
                {
                    MongoCollection.BulkWriteAsync(cache, options);
                }

                index++;
            }
        }
        public async Task BulkInsert(ICollection <T> entities)
        {
            var stores = new List <WriteModel <T> >();

            stores.AddRange(entities.Select(x => new InsertOneModel <T>(x)));

            await MongoCollection.BulkWriteAsync(stores);
        }