///////////////////////////////////////// // remove client from business ///////////////////////////////////////// public async Task <Boolean> removeFromBusiness(string businessName, string client, bool block) { var builderF = Builders <Business> .Filter; FilterDefinition <Business> filter = builderF.Eq("BName", businessName); var builderU = Builders <Business> .Update; UpdateDefinition <Business> update = builderU.Pull("BClient", client); // refers to blocked list if (block) { var tempResult = await _businesses.Find(filter).FirstOrDefaultAsync(); if (tempResult.BBlockedList.Length <= 0 || tempResult.BBlockedList[0] == null) { string[] blocked = new string[] { client }; update = update.Set("BBlockedList", blocked); } else { update = update.Push("BBlockedList", client); } } var result = await _businesses.UpdateOneAsync(filter, update); return(result != null ? true : false); }
/// <summary> /// Builds the push for a MongoDb array field. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="updates">The updates.</param> /// <param name="field">The MongoDb field.</param> /// <param name="value">The value.</param> /// <returns>The update.</returns> public static UpdateDefinition <T> BuildPush <T>(UpdateDefinition <T> updates, string field, object value) { if (updates == null) { return(Builders <T> .Update.Push(field, value)); } return(updates.Push(field, value)); }
public void AddActivityAndManageConnectionToUser(string userId, Activity activity, Connection connection) { try { if (activity != null) { UpdateDefinition <User> updateQuery = Builders <User> .Update.Push("Activities", activity); bool isOnline = activity.ActivityType == ChatServer.API.Model.Enum.ActivityType.getOnline; updateQuery = updateQuery.Set("IsOnline", isOnline); updateQuery = (isOnline && connection != null) ? updateQuery.Push("Connections", connection) : updateQuery.Pull("Connections", connection); _users.FindOneAndUpdate(Builders <User> .Filter.Eq("Id", userId), updateQuery); } } catch (Exception ex) { _logger.LogError(ex, "AddActivityAndManageConnectionToUser UserRepository Exception"); } }
protected virtual UpdateDefinition <T> ToUpdateDefinition(Updates <T> updates) { UpdateDefinition <T> updateDefinition = Builders <T> .Update.Combine(); if (updates.Sets != null) { foreach (Update <T> update in updates.Sets) { updateDefinition = updateDefinition.Set(update.PropertyExpression, update.Value); } } if (updates.Increments != null) { foreach (Update <T> update in updates.Increments) { updateDefinition = updateDefinition.Inc(update.PropertyExpression, update.Value); } } if (updates.SetOnInserts != null) { foreach (Update <T> update in updates.SetOnInserts) { updateDefinition = updateDefinition.SetOnInsert(update.PropertyExpression, update.Value); } } if (updates.Pushes != null) { foreach (UpdateCollection <T> update in updates.Pushes) { var property = new ExpressionFieldDefinition <T>(update.PropertyExpression); updateDefinition = updateDefinition.Push(property, update.Value); } } if (updates.Pulls != null) { foreach (UpdateCollection <T> update in updates.Pulls) { var property = new ExpressionFieldDefinition <T>(update.PropertyExpression); updateDefinition = updateDefinition.Pull(property, update.Value); } } return(updateDefinition); }
public List <UpdateDefinition <BsonDocument> > BuildUpdatesForSave( UpdateDefinition <BsonDocument> update, TrackableListTracker <T> tracker, params object[] keyValues) { var keyNamespace = DocumentHelper.ToDotPath(keyValues); // Multiple push-back batching optimization if (tracker.ChangeList.Count > 1 && tracker.ChangeList.All(c => c.Operation == TrackableListOperation.PushBack)) { var newValues = tracker.ChangeList.Select(c => c.NewValue); return(new List <UpdateDefinition <BsonDocument> > { update == null ? Builders <BsonDocument> .Update.PushEach(keyNamespace, newValues) : update.PushEach(keyNamespace, newValues) }); } // Multiple push-front batching optimization if (tracker.ChangeList.Count > 1 && tracker.ChangeList.All(c => c.Operation == TrackableListOperation.PushFront)) { var newValues = tracker.ChangeList.Select(c => c.NewValue).Reverse(); return(new List <UpdateDefinition <BsonDocument> > { update == null ? Builders <BsonDocument> .Update.PushEach(keyNamespace, newValues, position : 0) : update.PushEach(keyNamespace, newValues, position: 0) }); } // List update can process only one change each time var updates = new List <UpdateDefinition <BsonDocument> >(); foreach (var change in tracker.ChangeList) { switch (change.Operation) { case TrackableListOperation.Insert: updates.Add(update == null ? Builders <BsonDocument> .Update.PushEach(keyNamespace, new[] { change.NewValue }, position: change.Index) : update.PushEach(keyNamespace, new[] { change.NewValue }, position: change.Index)); update = null; break; case TrackableListOperation.Remove: throw new Exception("Remove operation is not supported!"); case TrackableListOperation.Modify: updates.Add(update == null ? Builders <BsonDocument> .Update.Set(keyNamespace + "." + change.Index, change.NewValue) : update.Set(keyNamespace + "." + change.Index, change.NewValue)); break; case TrackableListOperation.PushFront: updates.Add(update == null ? Builders <BsonDocument> .Update.PushEach(keyNamespace, new[] { change.NewValue }, position: 0) : update.PushEach(keyNamespace, new[] { change.NewValue }, position: 0)); break; case TrackableListOperation.PushBack: updates.Add(update == null ? Builders <BsonDocument> .Update.Push(keyNamespace, change.NewValue) : update.Push(keyNamespace, change.NewValue)); break; case TrackableListOperation.PopFront: updates.Add(update == null ? Builders <BsonDocument> .Update.PopFirst(keyNamespace) : update.PopFirst(keyNamespace)); break; case TrackableListOperation.PopBack: updates.Add(update == null ? Builders <BsonDocument> .Update.PopLast(keyNamespace) : update.PopLast(keyNamespace)); break; } } if (update != null) { updates.Add(update); } return(updates); }
public Task UpdateAsync(Expression <Func <T, bool> > filter, UpdateDefinition <T> updates) { updates.Push("UpdateDate", DateTimeOffset.Now); return(_collection.UpdateManyAsync(filter, updates)); }
public Task UpdateAsync(string id, UpdateDefinition <T> updates) { updates.Push("UpdateDate", DateTimeOffset.Now); return(_collection.UpdateOneAsync(x => x.Id == id, updates)); }
public bool Update(Expression <Func <T, bool> > filter, UpdateDefinition <T> updates) { updates.Push("UpdateDate", DateTimeOffset.Now); return(_collection.UpdateMany(filter, updates).IsAcknowledged); }
public bool Update(string id, UpdateDefinition <T> updates) { updates.Push("UpdateDate", DateTimeOffset.Now); return(_collection.UpdateOne(x => x.Id == id, updates).IsAcknowledged); }