SetDropDups()不会删除重复索引

时间:2015-02-13 00:35:12

标签: c# mongodb

我正在尝试为MoongoDB中的文档的特定字段(跟踪编号)创建索引。我不想将重复值插入我的数据库,并希望删除重复的索引。我的代码如下所示,但它不起作用。当我尝试插入重复值时,它会给我一个错误,而不是丢弃该索引。

var keys = IndexKeys.Ascending("TrackingNumber");
var options = IndexOptions.SetUnique(true).SetDropDups(true);
_collection.CreateIndex(keys, options);

我收到以下错误消息:

  

未处理的类型异常   'MongoDB.Driver.MongoDuplicateKeyException发生在   MongoDB.Driver.dll

     

其他信息:WriteConcern检测到错误。 (回应是   {“ok”:1,“code”:11000,“err”:“insertDocument ::由::引起::   11000 E11000重复键错误索引:   FedEx.FedExCollection2。$ TrackingNumber_1 dup key:{:null}“,”n“:   NumberLong(0)})

有人可以帮我解决问题吗?

0 个答案:

没有答案