Question

I read MongoDB documentation but I didn't understand well! 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?

       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;
            }
        }
Était-ce utile?

La solution

Because you are using WriteConcern.Acknowledged, the try/catch is good enough. WriteConcern.Acknowledged will do the getLastError for you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top