根据多个条件删除/拉出一个文档

时间:2016-12-12 13:34:00

标签: c# mongodb mongodb-.net-driver

我试图根据用户的两个输入找到一个MongoDB文档。这是我的榜样:

public async Task RemoveAdminRole(string userId, string groupId) { 
    var idFilter = Builders<Group>.Filter.Eq(group => group._id, groupId);
    var roleFilter = Builders<Group>.Update.PullFilter(group => group.Roles, role => role.UserId == userId);
    var roleFilter2 = Builders<Group>.Update.PullFilter(group => group.Roles, role => role.Role == "ADMIN");

    var update = roleFilter & roleFilter2;

    await this.DataContext.MongoCollection.UpdateOneAsync(idFilter, update);
}

基本上我想要做的就是移除/拉动元素&#34;角色&#34;位于Grouprole.Role == "ADMIN"的{​​{1}}内。我收到了错误:

  

运营商&#39;&amp;&#39;不能应用于类型&#39; UpdateDefinition&lt; Group&gt;&#39;的操作数。和&#39; UpdateDefinition&lt; Group&gt;&#39;

我如何创建多个role.UserId == userId(或类似的),以便根据我的两个输入删除特定元素?

1 个答案:

答案 0 :(得分:2)

您可以添加第二个,如下所示:

var roleFilter = Builders<Group>.Update
    .PullFilter(group => group.Roles, role => role.UserId == userId)
    .PullFilter(group => group.Roles, role => role.Role == "ADMIN");