如果数据库中不存在,则插入;如果数据库中不存在,则不执行任何操作;如果列表中不存在,则删除

时间:2019-01-26 23:41:06

标签: c# mongodb .net-core-2.2

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中删除。如何编写此查询?

0 个答案:

没有答案