mongodb - ReplaceOne 抛出重复键异常

标签 mongodb mongodb-.net-driver

我的应用从远程服务器接收数据,并调用 ReplaceOne 来插入新文档或使用 Upsert = true 的给定键替换现有文档。 ( key 使用 * 进行匿名化)该代码仅在单个线程中运行。

但是,应用程序偶尔会崩溃并出现以下错误:

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) 

我的问题是,为什么当我将 ReplaceOneUpsert = true 选项一起使用时可能会出现重复 key ?

该应用程序在以下环境和运行时运行:

.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

最佳答案

Upsert 基于过滤器查询进行工作。如果过滤器查询不匹配,它将尝试插入文档。

如果过滤器查询找到该文档,它将替换该文档。

就您而言,它可能以任何一种方式进行,即插入/更新。请检查数据来分析场景。

插入场景:-

如果过滤条件中不存在 _id,则更新插入会自动创建实际 _id。因此,_id 不应产生唯一性问题。如果其他一些字段是唯一索引的一部分,则会产生唯一性问题。

替换场景:-

您尝试更新的字段应该定义有唯一索引。请检查集合的索引及其属性。

Optional. When true, replaceOne() either: Inserts the document from the replacement parameter if no document matches the filter. Replaces the document that matches the filter with the replacement document.

To avoid multiple upserts, ensure that the query fields are uniquely indexed.

Defaults to false.

MongoDB will add the _id field to the replacement document if it is not specified in either the filter or replacement documents. If _id is present in both, the values must be equal.

关于mongodb - ReplaceOne 抛出重复键异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39464597/

相关文章:

json - 在这种情况下,我应该使用 Redis 还是 MongoDB 哪个数据库?

javascript - 迭代 mongodb 条目 javascript Node

c# - MongoDB 可以从 mongo 客户端连接,但不能从 C# 驱动程序连接

mongodb - Mongo Db 使用 C# api 记录检索非常慢

mongodb - 获取属性与数组中嵌套的值匹配的所有对象

c# - MongoDB C# 驱动程序 - 更改继承类的 Id 序列化

javascript - $inc 仅当新元素添加到集合中时

mongodb - 我如何订购列表并在 MongoDB 上为其项目添加位置?

node.js - 如何在mongodb中过滤两次之间的数据

c# - 具有 UUID (GUID) 的 MongoDB C# 过滤器返回 0 结果