public class Ingredient
{
[BsonId]
public ObjectId IngredientId { get; set; }
public bool IsActive { get; set; }
public List<IngredientSideEffectItem> SideEffects { get; set; }
}
public class IngredientSideEffectItem
{
public ObjectId SideEffectId { get; set; }
public long Counter{ get; set; }
}
public async Task UpdateIngredient(Ingredient s)
{
FilterDefinition<Ingredient> filter =
Builders<Ingredient>.Filter.Eq(l => l.IngredientId, , s.IngredientId);
UpdateDefinition<Ingredient> update =
Builders<Ingredient>.Update
.Set(x => x.SideEffects, s.SideEffects)
.Set(x => x.IsActive,s.IsActive)
UpdateResult result = await db.Ingredients.UpdateOneAsync(filter, update);
}
我想将s.SideEffects
项插入x.SideEffects
中。但是,如果项目存在于数据库中-忽略;如果db中不存在-插入;并且如果存在于db中而不在列表中-从db中删除。如何编写此查询?