내 응용 프로그램은 원격 서버에서 데이터를 받고 ReplaceOne
을 호출하여 새 키를 삽입하거나 기존 키를 Upsert = true
으로 대체합니다. (키는 *
으로 익명 처리됩니다.) 코드는 단일 스레드에서만 실행됩니다.ReplaceOne이 중복 키 예외를 throw합니다
그러나, 때때로 다음과 같은 오류와 응용 프로그램의 충돌 :
Unhandled Exception: MongoDB.Driver.MongoWriteException: A write operation resulted in an error.
E11000 duplicate key error collection: ****.orders index: _id_ dup key: { : "****-********-********-************" } ---> MongoDB.Driver.MongoBulkWriteException`1[MongoDB.Bson.BsonDocument]: A bulk write operation resulted in one or more errors.
E11000 duplicate key error collection: ****.orders index: _id_ dup key: { : "****-********-********-************" }
at MongoDB.Driver.MongoCollectionImpl`1.BulkWrite(IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionBase`1.ReplaceOne(FilterDefinition`1 filter, TDocument replacement, UpdateOptions options, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at MongoDB.Driver.MongoCollectionBase`1.ReplaceOne(FilterDefinition`1 filter, TDocument replacement, UpdateOptions options, CancellationToken cancellationToken)
at Dashboard.Backend.AccountMonitor.ProcessOrder(OrderField& order)
at Dashboard.Backend.AccountMonitor.OnRtnOrder(Object sender, OrderField& order)
at XAPI.Callback.XApi._OnRtnOrder(IntPtr ptr1, Int32 size1)
at XAPI.Callback.XApi.OnRespone(Byte type, IntPtr pApi1, IntPtr pApi2, Double double1, Double double2, IntPtr ptr1, Int32 size1, IntPtr ptr2, Int32 size2, IntPtr ptr3, Int32 size3)
Aborted (core dumped)
내 질문에, 왜 내가 Upsert = true
옵션 ReplaceOne
을 사용할 때 DUP 키를 가질 수 있습니다?
응용 프로그램은 다음과 같은 환경과 런타임에서 일하고 :
.NET Command Line Tools (1.0.0-preview2-003121)
Product Information:
Version: 1.0.0-preview2-003121
Commit SHA-1 hash: 1e9d529bc5
Runtime Environment:
OS Name: ubuntu
OS Version: 16.04
OS Platform: Linux
RID: ubuntu.16.04-x64
그리고 MongoDB.Driver 2.3.0-rc1
합니다.
내 필터는'_id'하고있다 정확히 같은'_id' 교체/upsert 할 문서에 있습니다. –
고유 인덱스가 정의되어 있는지 여부를 업데이트하려는 필드를 확인 했습니까? – notionquest
컬렉션에는 고유 인덱스로 '_id' 만 있습니다. –