我需要在文档的字段数组中插入一些元素。嗯……我知道 Mongo 有原子 Update.Push……事实上,我需要在许多文档中进行这种插入。情况如下(我需要为每个用户名插入一个角色数组):
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)));
}
}
假设在将角色“推送”到第三个用户名时连接断开。我需要回滚以前的操作。任何的想法?