2017-05-12 1 views
0

これは時々崩壊し続けるものです。私はupsertとして行われた操作を持っていますが、たまには、このエラーに遭遇してサービスがクラッシュするため、どのように可能なのか理解できません。私は、一致させるのキーとしてSurveyIdを使用してアップサートをしよう:重複したID例外が発生すると、なぜこのアップセルが失敗するのですか?

await _surveyRepository.DatabaseCollection.UpdateOneAsync(
    Builders<SurveyData>.Filter.Eq(survey => survey.SurveyId, surveyData.SurveyId), 
    Builders<SurveyData>.Update 
     .Set(survey => survey.SurveyLink, surveyData.SurveyLink) 
     .Set(survey => survey.ClientId, surveyData.ClientId) 
     .Set(survey => survey.CustomerFirstName, surveyData.CustomerFirstName) 
     .Set(survey => survey.CustomerLastName, surveyData.CustomerLastName) 
     .Set(survey => survey.SurveyGenerationDateUtc, surveyData.SurveyGenerationDateUtc) 
     .Set(survey => survey.PortalUserId, surveyData.PortalUserId) 
     .Set(survey => survey.PortalUserFirst, surveyData.PortalUserFirst) 
     .Set(survey => survey.PortalUserLast, surveyData.PortalUserLast) 
     .Set(survey => survey.Tags, surveyData.Tags), 
    new UpdateOptions { IsUpsert = true }) 
.ConfigureAwait(false); 

そして、私は時折、このエラーを取得します:

Message: A write operation resulted in an error. E11000 duplicate key error collection: surveys.surveys index: SurveyId dup key: { : "" }

IDにGUIDの文字列表現で、 mongoではユニークに設定されています。

これはどうしてですか?キーを見つけたら定義されたプロパティを更新し、そうでなければ挿入することは私の理解です。それは間違っていますか?なぜなら、それは私が必要とする効果です。

C#のドライバのバージョンは2.4.1.18

答えて

0

It is my understanding that if it finds the key, it'll update the defined properties, and if not, it'll insert. Is that not correct

ですはい、それはアップサートが何をするかです。また、新しく挿入されたドキュメントには、基準の部分(あなたのケースsurveyId)と、更新クエリの更新の変更部分(他のすべての指定フィールド)のすべてのフィールドが含まれます。
クエリにupsert = falseを設定する必要があります。その後、条件に合致するドキュメントのみが更新され、一致が見つからない場合は更新が失敗します。

+0

意図した効果ではないこと。それは単にアップデートに過ぎません。私はアップセートが必要です。 IDが見つかった場合は、指定されたデータでレコードを更新し、見つからない場合は、指定されたフィールドで新しいレコードを挿入する必要があります。それは決して失敗しないはずです。 – Sinaesthetic

1

このJira ticketによるので、これが起こる:

During an update with upsert:true option, two (or more) threads may attempt an upsert operation using the same query predicate and, upon not finding a match, the threads will attempt to insert a new document. Both inserts will (and should) succeed, unless the second causes a unique constraint violation.

+0

これは刺激的です...ありがとう – Sinaesthetic

関連する問題