public bool UpdateBulk(int BatchSize) { Stopwatch timer = new Stopwatch(); timer.Start(); bool Updated = false; try { var builder = Builders <LatLong> .Filter; var filterDef = builder.And(builder.Ne(l => l.Latitude, "0"), builder.Ne(l => l.Longitude, "0")); var updateDef = Builders <LatLong> .Update.Set(l => l.Latitude, "0").Set(l => l.Longitude, "0"); var records = _coords.Find(filterDef).Limit(BatchSize).ToList(); var bulkOps = new List <WriteModel <LatLong> >(); foreach (var record in records) { var upsertOne = new UpdateOneModel <LatLong>(filterDef, updateDef) { IsUpsert = true }; bulkOps.Add(upsertOne); } Console.WriteLine("Time taken: " + timer.Elapsed.TotalSeconds + " seconds"); _coords.BulkWrite(bulkOps); Console.WriteLine("Time taken: " + timer.Elapsed.TotalSeconds + " seconds"); } catch (Exception ex) { Console.WriteLine(ex.Message); } timer.Stop(); return(Updated); }
public BulkWriteResult <T> BulkWrite( IEnumerable <WriteModel <T> > requests, BulkWriteOptions?options = null, CancellationToken cancellationToken = default ) { return(collection.BulkWrite(requests, options, cancellationToken)); }
public void Insert(IEnumerable <TEntity> item) { if (item != null && item.Any()) { var list = new List <WriteModel <TEntity> >(); foreach (var iitem in item) { list.Add(new InsertOneModel <TEntity>(iitem)); } _table.BulkWrite(list); } }
private void PartialDeleteXmlAnyElements(List <XElement> elements, PropertyInfo propertyInfo, object propertyValue, string parentPath) { if (elements.Count < 1 || propertyInfo == null) { return; } var element = elements.First(); if (!element.HasElements) { UnsetProperty(propertyInfo, parentPath); return; } Logger.DebugFormat($"Updating XmlAnyElements: {parentPath} {propertyInfo?.Name}"); var anyPropertyInfo = propertyInfo.PropertyType.GetProperties() .First(x => x.IsDefined(typeof(XmlAnyElementAttribute), false)); var propertyPath = GetPropertyPath(GetPropertyPath(parentPath, propertyInfo.Name), anyPropertyInfo.Name); var items = (IList <XmlElement>)anyPropertyInfo.GetValue(propertyValue); var names = items.Select(x => x.LocalName).ToList(); var updateBuilder = Builders <T> .Update; var updateList = element .Elements() .Select(x => { var position = names.IndexOf(x.Name.LocalName); var positionPath = propertyPath + "." + position; if (position == -1) { return(null); } // Set position to null var update = updateBuilder.Unset(positionPath); return(new UpdateOneModel <T>(_entityFilter, update)); }) .Where(x => x != null) .ToList(); if (updateList.Count > 0) { // Remove all null items var update = updateBuilder.PullAll(propertyPath, new object[] { null }); updateList.Add(new UpdateOneModel <T>(_entityFilter, update)); _collection.BulkWrite(updateList); } }
public BulkWriteResult <T> BulkWrite( IEnumerable <WriteModel <T> > requests, BulkWriteOptions?options = null, CancellationToken cancellationToken = default) { if (TryGetSession(out IClientSessionHandle? session)) { return(BulkWrite(session, requests, options, cancellationToken)); } return(_collection.BulkWrite(requests, options, cancellationToken)); }
public void Remove(TAggregateRoot[] aggregates) { List <WriteModel <TAggregateRoot> > writeModels = new List <WriteModel <TAggregateRoot> >(); foreach (var aggregate in aggregates) { writeModels.Add( new DeleteOneModel <TAggregateRoot>(Builders <TAggregateRoot> .Filter.Eq(a => a.Id, aggregate.Id)) ); } _collection.BulkWrite(writeModels, _options); }
public long SaveAll(IEnumerable <WebhookPublisher> models, bool?references = true) { var saves = 0L; using (var scope = TransactionScopeOption.Required.AsTransactionScope()) { var keys = new List <Guid>(); var candidates = new List <WebhookPublisher>(); foreach (var model in models) { var candidate = model; if (!references.HasValue || !references.Value) { candidate = model.CreateCopy(); candidate.Definitions.Clear(); } candidates.Add(candidate); keys.Add(candidate.Id); } var matches = FindAllByKeys(keys, references: references); var similar = candidates.Intersect(matches); var different = matches.Any() ? candidates.Except(matches) : candidates; var requests = new List <WriteModel <WebhookPublisher> >(); if (similar.Any()) { foreach (var model in similar) { requests.Add(new ReplaceOneModel <WebhookPublisher>(Builders <WebhookPublisher> .Filter.Where(x => x.Id == model.Id), model)); } var result = collection.BulkWrite(requests); saves += result.IsAcknowledged ? result.MatchedCount : 0L; } if (different.Any()) { requests.Clear(); foreach (var model in different) { requests.Add(new InsertOneModel <WebhookPublisher>(model)); } var results = collection.BulkWrite(requests); saves += results.IsAcknowledged ? results.InsertedCount : 0L; } scope.Complete(); } return(saves); }
/// <summary> /// The InsertRange. /// </summary> /// <param name="entities">The entities<see cref="IEnumerable{TEntity}"/>.</param> /// <returns>The <see cref="bool"/>.</returns> public void InsertRange(IEnumerable <TEntity> entities) { var options = new BulkWriteOptions { IsOrdered = false, BypassDocumentValidation = false }; var result = (_collection.BulkWrite((IEnumerable <WriteModel <TEntity> >)entities, options)).IsAcknowledged; }
public void CreateOrUpdateUserGames(StoredGameDataWithUserId[] gameDataWithUserId) { IMongoCollection <StoredGameDataWithUserId> users = Database.GetCollection <StoredGameDataWithUserId>("StoredGameDataWithUserId"); var groupedByUserId = gameDataWithUserId.GroupBy(_ => _.UserId).Select(_ => _.First()).ToArray(); var usersFilter = Builders <StoredGameDataWithUserId> .Filter.In("UserId", groupedByUserId.Select(_ => _.UserId)); var usersIdsForUpdateList = users.Find(usersFilter).Project(_ => _.UserId).ToList(); var gameDataForUpdate = groupedByUserId.Where(_ => usersIdsForUpdateList.Contains(_.UserId)); var gameDataToCreate = groupedByUserId.Where(_ => !usersIdsForUpdateList.Contains(_.UserId)); if (gameDataToCreate.Any()) { users.InsertMany(gameDataToCreate); } var models = gameDataForUpdate.Select(update => new ReplaceOneModel <StoredGameDataWithUserId>(Builders <StoredGameDataWithUserId> .Filter.Eq(s => s.UserId, update.UserId), update) { IsUpsert = true }).ToList(); if (models.Count > 0) { users.BulkWrite(models); } }
private void InsertMongoDocs() { var sw = Stopwatch.StartNew(); _packages.BulkWrite(_packagesData.Select(u => new InsertOneModel <Package>(u))); Console.WriteLine("Docs inserted into mongodb at " + sw.ElapsedMilliseconds + " ms."); }
public BulkWriteResult <TModel> BulkWrite( IEnumerable <WriteModel <TModel> > requests, BulkWriteOptions options = null, CancellationToken token = default) { try { return(_collection.BulkWrite(requests, 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); } }
/// <summary> /// Writes one or more <see cref="BsonDocument"/>(s) to MongoDb /// </summary> /// <param name="toWrite"></param> /// <param name="collectionNamePostfix">Optional argument to write to a different collection with the specified tag</param> /// <returns></returns> public WriteResult WriteMany(IList <BsonDocument> toWrite, string collectionNamePostfix = null) { if (!toWrite.Any()) { return(WriteResult.Success); } //TODO Test whether pre-fetching references to all the image_* collections results in any speedup IMongoCollection <BsonDocument> collectionForWrite = collectionNamePostfix == null ? _defaultCollection : _database.GetCollection <BsonDocument>($"{_defaultCollectionName}_{collectionNamePostfix}"); _logger.Info($"Attempting bulk write of {toWrite.Count} documents to {collectionForWrite.CollectionNamespace}"); try { //TODO Try and determine if any write errors are to do with a document in the batch or not BulkWriteResult <BsonDocument> res = collectionForWrite.BulkWrite(toWrite.Select(d => new InsertOneModel <BsonDocument>(d))); _logger.Debug(" Write to {0} acknowledged: {1}", collectionForWrite.CollectionNamespace, res.IsAcknowledged); return(res.IsAcknowledged ? WriteResult.Success : WriteResult.Failure); } catch (MongoBulkWriteException e) { //TODO Determine possible causes of MongoBulkWriteException _logger.Error("Exception when writing to MongoDb: " + e); return(WriteResult.Unknown); } }
public void UpdateClients(List <BannedClientRecord> newBannedClientRecords) { var filter = Builders <BannedClientRecord> .Filter; var update = Builders <BannedClientRecord> .Update; var updatesLength = newBannedClientRecords.Count; var updates = new WriteModel <BannedClientRecord> [updatesLength]; for (int i = 0; i < updatesLength; i++) { var banRecord = newBannedClientRecords[i]; var source = banRecord.Source; var currentAccFilter = filter.Eq(a => a.Source, source); if (banRecord.BanCounts == 0) { updates[i] = new DeleteOneModel <BannedClientRecord>(currentAccFilter); } else { updates[i] = new ReplaceOneModel <BannedClientRecord>(currentAccFilter, banRecord) { IsUpsert = true } }; } bannedClientsCollection.BulkWrite(updates); } }
private void InsertDocs() { var rand = new Random(); var bulkBar = new List <WriteModel <TestConcurrentModel> >(); var bulkBaz = new List <WriteModel <TestConcurrentModel> >(); foreach (var i in Enumerable.Range(0, Count)) { bulkBar.Add(new InsertOneModel <TestConcurrentModel>(new TestConcurrentModel { Spread = i % 12, Additional = new TestStruct { X = rand.Next(255), Y = rand.Next(255) } })); bulkBaz.Add(new InsertOneModel <TestConcurrentModel>(new TestConcurrentModel { Spread = i % 12, Additional = new TestStruct { X = rand.Next(255), Y = rand.Next(255) } })); } _collectionBar.BulkWrite(bulkBar); _collectionBaz.BulkWrite(bulkBaz); }
static void Main(string[] args) { Update(); Console.WriteLine("Hello World!"); List <WriteModel <BsonDocument> > InputDBList = new List <WriteModel <BsonDocument> >(); IMongoCollection <BsonDocument> hk_test = Database.GetCollection <BsonDocument>("test"); var projection = Builders <BsonDocument> .Projection.Include("ITCode2").Exclude("_id"); //var projection = Builders<BsonDocument>.Projection.Include("ID").Include("ITCode2").Include("TMSTAMP").Exclude("_id"); var filter1 = Builders <BsonDocument> .Filter.Ne("ID", 11); var result = Database.GetCollection <BsonDocument>("hk_test").Find("{}").Project(projection).ToList().ToJson(); var Fin_List = BsonSerializer.Deserialize <List <teststru> >(result).AsQueryable().ToList(); var Fin_List1 = BsonSerializer.Deserialize <List <string> >(result).AsQueryable().Distinct().ToList(); var rep = new ReplaceOneModel <BsonDocument>(filter1, new BsonDocument("ID", 14).Add("ITCode2", "ok").Add("TMSTAMP", "OK")); rep.IsUpsert = true; InputDBList.Add(rep); hk_test.BulkWrite(InputDBList); var filter = Builders <BsonDocument> .Filter.Eq("ID", 11); //var update = Builders<BsonDocument>.Update.Set("ITCode2", "OK2").Set("TMSTAMP", "OK2"); var update = Builders <BsonDocument> .Update.Set("ITCode2", new BsonDateTime(DateTime.Now)); Database.GetCollection <BsonDocument>("test").UpdateMany("{}", update); //Database.GetCollection<BsonDocument>("hk_test").UpdateMany(filter, update, new UpdateOptions { IsUpsert = true}); //var updateOptions = new UpdateOptions { IsUpsert = true }; //InputDBList.Add(new UpdateManyModel<BsonDocument>(filter, update)); var a = new UpdateManyModel <BsonDocument>(filter, update); a.IsUpsert = true; InputDBList.Add(a); hk_test.BulkWrite(InputDBList); //var projection = Builders<BsonDocument>.Projection.Include("ID").Include("ITCode2").Include("TMSTAMP").Exclude("_id"); //var result = Database.GetCollection<BsonDocument>("hk_test").Find(new BsonDocument()).Project(projection).ToList().ToJson(); ////var document2 = Database.GetCollection<BsonDocument>("TCR0002_V2").Find(new BsonDocument()).Project(projection).ToList().ToJson(); //var DataList = BsonSerializer.Deserialize<List<BsonDocument>>(result).AsQueryable().ToList(); }
public long SaveAll(IEnumerable <WebhookDefinition> models, bool?references = true) { var count = 0L; using (var scope = TransactionScopeOption.Required.AsTransactionScope()) { var keys = new List <Guid>(); foreach (var model in models) { keys.Add(model.Id); } var matches = FindAllByKeys(keys, references ?? true); var similar = Enumerable.Empty <WebhookDefinition>(); var different = Enumerable.Empty <WebhookDefinition>(); if (matches.Any()) { similar = models.Intersect(matches); different = models.Except(matches); } var requests = new List <WriteModel <WebhookDefinition> >(); if (similar.Any()) //update { foreach (var model in similar) { requests.Add(new ReplaceOneModel <WebhookDefinition>(Builders <WebhookDefinition> .Filter.Where(x => x.Id == model.Id), model)); } var result = collection.BulkWrite(requests); count += result.IsAcknowledged ? result.MatchedCount : 0L; } if (different.Any()) //insert { requests.Clear(); foreach (var model in different) { requests.Add(new InsertOneModel <WebhookDefinition>(model)); } var results = collection.BulkWrite(requests); count += results.IsAcknowledged ? results.InsertedCount : 0L; } scope.Complete(); } return(count); }
private void InsertMongoDocs() { var sw = Stopwatch.StartNew(); _users.BulkWrite(_usersMnRowData.Select(u => new InsertOneModel <UserMn>(u))); _articles.BulkWrite(_articlesMnRowData.Select(a => new InsertOneModel <ArticleMn>(a))); Console.WriteLine("Docs inserted into mongodb at " + sw.ElapsedMilliseconds + " ms."); }
private void TestMongo3(int iterations) { List <WriteModel <BsonDocument> > list = new List <WriteModel <BsonDocument> >(7); Stopwatch sw = new Stopwatch(); for (int i = 0; i < iterations; i++) { string iStr = i.ToString(); Guid elementGuid = new Guid("00000000-0000-0000-0000-" + new string('0', 12 - iStr.Length) + iStr); list.Clear(); for (int j = 0; j < classifierTypes.Length; j++) { Classifier.Type type = classifierTypes[j]; Classifier c; if (type == Classifier.Type.CProps || type == Classifier.Type.CElements || type == Classifier.Type.CLinks) { c = new Classifier(type, i); } else { c = new Classifier(type, i, i, elementGuid); } BsonDocument mdbd = c.ToBsonDocument(); list.Add(new ReplaceOneModel <BsonDocument>(new BsonDocument("_id", c.Key), mdbd) { IsUpsert = true }); } sw.Start(); _classifier.BulkWrite(list); sw.Stop(); if (((i + 1) % 100) == 0) { Console.WriteLine(i + 1); } } sw.Stop(); Console.WriteLine($"TestMongo3. {iterations} итераций. {sw.Elapsed.ToString()}, {sw.ElapsedMilliseconds * 1000 / iterations} мc/1000"); }
public void UpdateAllCards(IEnumerable <IMtgCard> cards) { var existingCards = FindCardsByNames(cards.Select(card => card.Name)); var plan = CreateCardUpdatePlan(existingCards, cards); if (plan.Any()) { collection.BulkWrite(plan); } }
protected override BulkWriteResult <BsonDocument> ExecuteAndGetResult(IMongoCollection <BsonDocument> collection, bool async) { if (async) { return(collection.BulkWriteAsync(_requests, _options).GetAwaiter().GetResult()); } else { return(collection.BulkWrite(_requests, _options)); } }
public virtual void updateMany(IEnumerable <WriteModel <Modelo> > movs, TContext db, IClientSessionHandle session = null) { try { IMongoCollection <Modelo> Collection = dbMongo.GetCollection <Modelo>(typeof(Modelo).Name); if (session == null) { Collection.BulkWrite(movs); } else { Collection.BulkWrite(session, movs); } } catch (Exception) { throw; } }
public static BulkWriteResult <T> BulkUpdateEach <T>( this IMongoCollection <T> collection, IEnumerable <T> items, Func <UpdateDefinitionBuilder <T>, T, UpdateDefinition <T> > updater ) where T : DocumentBase { return(collection.BulkWrite( items.Select( item => new UpdateOneModel <T>(Builders <T> .Filter.Where(document => document._id == item._id), updater(Builders <T> .Update, item)) ))); }
public OperationResult Execute(CancellationToken cancellationToken) { try { BulkWriteResult <BsonDocument> result; if (_session == null) { result = _collection.BulkWrite(_requests, _options); } else { result = _collection.BulkWrite(_session, _requests, _options); } return(new UnifiedBulkWriteOperationResultConverter().Convert(result)); } catch (Exception exception) { return(OperationResult.FromException(exception)); } }
public void Save(IEnumerable <TObject> objs) { List <ReplaceOneModel <TObject> > bulks = objs.Select(obj => new ReplaceOneModel <TObject>(Builders <TObject> .Filter.Where(s => s.Id == obj.Id), obj) { IsUpsert = true }).ToList(); Collection.BulkWrite(bulks, new BulkWriteOptions { IsOrdered = true }); }
/// <summary> /// Update语法 /// </summary> public static void Update() { List <WriteModel <BsonDocument> > InputDBList = new List <WriteModel <BsonDocument> >(); IMongoCollection <BsonDocument> CompanyList_ITCode2_Input = Database.GetCollection <BsonDocument>("test"); var filter = Builders <BsonDocument> .Filter.Eq("ITCode2", "1"); var update = Builders <BsonDocument> .Update.Set("ENTRYDT", new BsonDateTime(DateTime.Now.AddHours(8))); var upsert = new UpdateManyModel <BsonDocument>(filter, update); upsert.IsUpsert = true; InputDBList.Add(upsert); CompanyList_ITCode2_Input.BulkWrite(InputDBList); }
private void WriteBuffer() { try { var result = _collection.BulkWrite(_buffer, new BulkWriteOptions { IsOrdered = false }); Statistics.Written += (ulong)result.InsertedCount; } catch (MongoBulkWriteException <BsonDocument> e) { Statistics.Written = (ulong)e.Result.InsertedCount; } }
public static void updata() { List <WriteModel <BsonDocument> > InputDBList = new List <WriteModel <BsonDocument> >(); IMongoDatabase Database = new MongoClient("mongodb://*****:*****@10.15.97.183:27017/DerivedData").GetDatabase("DerivedData"); IMongoCollection <BsonDocument> CompanyList_ITCode2_Input = Database.GetCollection <BsonDocument>("test"); var filter = Builders <BsonDocument> .Filter.Eq("ITCode2", "1"); var update = Builders <BsonDocument> .Update.Set("ENTRYDT", new BsonDateTime(DateTime.Now.AddHours(8))); var upsert = new UpdateManyModel <BsonDocument>(filter, update); upsert.IsUpsert = true; InputDBList.Add(upsert); CompanyList_ITCode2_Input.BulkWrite(InputDBList); }
public void SaveMany(List <T> models) { var writeModels = new List <WriteModel <T> >(); foreach (T model in models) { FilterDefinition <T> filter = Builders <T> .Filter.Eq(m => m.ID, model.ID); writeModels.Add(new ReplaceOneModel <T>(filter, model) { IsUpsert = true }); } Collection.BulkWrite(writeModels); }
public static long BulkReplaceEach <T>( this IMongoCollection <T> collection, IEnumerable <T> items ) where T : DocumentBase { if (!items.Any()) { return(0L); } return(collection.BulkWrite( items.Select( item => new ReplaceOneModel <T>(Builders <T> .Filter.Where(document => document._id == item._id), item) )).ModifiedCount); }
protected override void Execute(IMongoCollection <BsonDocument> collection, bool async) { if (collection.Settings.WriteConcern == null) { collection = collection.WithWriteConcern(WriteConcern.Acknowledged); } if (async) { collection.BulkWriteAsync(_requests, _options).GetAwaiter().GetResult(); } else { collection.BulkWrite(_requests, _options); } }