如何使用mongoDB进行“all or Nothing”操作?

问题描述 投票:0回答:2

我需要在文档的字段数组中插入一些元素。嗯...我知道 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)));
            }
        }

假设在将角色“推送”到第三个用户名时连接断开。我需要回滚之前的操作。任何想法?

c# mongodb transactions push insertion
2个回答
2
投票

根据我对 MongoDB 的了解,它在多个集合方面不符合 ACID。现在,如果您一次更新一个集合,那么应该没问题。否则,如果您愿意的话,盒子上的警告标签上会显示非 ACID 合规性。


0
投票

UpdateMany() 或 uodateOne() 不是一个一体化操作,因为如果在多个文档中进行更新时,操作超时或断电。该文档的更新不会自动完成。您需要重新运行该操作以确保其完成。

也就是说,updateOne() 和 updateMany() 不遵守数据库的 ACID 法则,即原子性、一致性、隔离性、持久性。

© www.soinside.com 2019 - 2024. All rights reserved.