2012-12-30 46 views
2

我需要在文档的字段数组中插入一些元素。那么...我知道Mongo有原子Update.Push ...事实是,我需要在许多文档中进行插入操作。案是下述(我需要插入一个角色阵列,每名):如何使用mongoDB执行“全部或全部”操作?

public override void AddUsersToRoles(string[] usernames, string[] roleNames) 
     { 
      foreach (string role in roleNames) 
      { 
       if (!this.RoleExists(role)) 
       { 
        throw new ProviderException(String.Format("The role '{0}' was not found.", role)); 
       } 
      } 

      //How to guarantee that all users will be updated with roles? 
      foreach (string user in usernames) 
      { 
       var docs = this.users.Update(Query.And(Query.EQ("Username", user), 
        Query.EQ("Applications.Name", this.ApplicationName)), Update.AddToSetEach("Applications.$.Roles", 
        new BsonArray(roleNames))); 
      } 
     } 

假设连接下来角色,第三的用户名的“推”的时刻。我需要回滚以前的操作。任何想法?

回答