简体   繁体   English

我是否正确使用mongodb驱动程序执行此删除代码?

[英]Am I doing this deletion code using mongodb driver on right way?

I read MongoDB documentation but I didn't understand well! 我阅读了MongoDB文档,但我不太了解! I need to know if the following code is right! 我需要知道以下代码是否正确! I need get confirmation if operation was performed succesfully. 如果操作成功完成,我需要得到确认。 Is needed to call getLastError or the try-catch is enough? 是否需要调用getLastError或try-catch是否足够?

       public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            WriteConcernResult result = null;
            try
            {
                result = this.users.Remove(Query.And(Query.EQ("ApplicationName",
                    this.ApplicationName), Query.EQ("Username", username)), RemoveFlags.Single,
                    WriteConcern.Acknowledged);

                if (result.HasLastErrorMessage)
                {
                    return false;
                }

                return (result.DocumentsAffected == 1);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

Because you are using WriteConcern.Acknowledged, the try/catch is good enough. 因为您正在使用WriteConcern.Acknowledged,所以try / catch足够好了。 WriteConcern.Acknowledged will do the getLastError for you. WriteConcern.Acknowledged会为你做getLastError。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM