【问题标题】:MongoDB createIndexes exception on MongoDB.Driver.GridFS.DeleteMongoDB.Driver.GridFS.Delete 上的 MongoDB createIndexes 异常
【发布时间】:2015-12-15 16:12:37
【问题描述】:

我正在使用 C# MongoDB.Driver,版本=1.10.1.73

注意我知道this question,这不是我的问题。

错误信息:

MongoDB.Driver.MongoWriteConcernException:命令“createIndexes” 失败:异常:索引名称:filename_1_uploadDate_1 已经 存在不同的选项(响应:{ “createdCollectionAutomatically”:假,“numIndexesBefore”:4, “errmsg”:“异常:索引名称:filename_1_uploadDate_1 已经存在不同的选项", "code" : 85, "ok" : 0.0 })

MongoDatabase _database;
var query = MongoDB.Driver.Builders.Query.And(
    MongoDB.Driver.Builders.Query.EQ(ColumnFilename, filePath),
    MongoDB.Driver.Builders.Query.EQ(ColumnWidth, width),
    MongoDB.Driver.Builders.Query.EQ(ColumnHeight, height));

var mongoFileInfo = _database.GridFS.FindOne(query);
if (mongoFileInfo != null) { 
    var mongoStream = mongoFileInfo.OpenRead();
    if (/*some condition*/) {
        mongoStream.Close();
        mongoStream.Dispose();
        mongoFileInfo.Delete(); // exception here
    }

完整的堆栈跟踪:

MongoDB.Driver.MongoCollection.CreateIndex(IMongoIndexKeys 键, IMongoIndexOptions 选项)
MongoDB.Driver.MongoCollection.CreateIndex(IMongoIndexKeys 键)
MongoDB.Driver.MongoCollection.CreateIndex(String[] keyNames)
MongoDB.Driver.GridFS.MongoGridFS.EnsureIndexes(Int32 maxFiles)
MongoDB.Driver.GridFS.MongoGridFS.EnsureIndexes()
MongoDB.Driver.GridFS.MongoGridFSFileInfo.Delete()

【问题讨论】:

  • 我看不出这个错误与这段代码有什么关系。哪一行实际上引发了异常?
  • 问题中同时标记了堆栈跟踪和抛出异常...

标签: c# mongodb mongodb-query mongodb-.net-driver


【解决方案1】:

filename_1_uploadDate_1 索引是默认在我的 Mongo 服务器版本 3.0.7 上创建的。唯一的默认设置是'不唯一'。每当某些代码强制该索引是唯一的(我认为应该如此)时,从 Mongo 中删除时就会发生上述异常。

解决方案:如果索引不存在,则创建它,如果存在且唯一,则重新创建为不唯一。

            CommandResult cr = null;
            IndexOptionsDocument NotUnique = new IndexOptionsDocument("unique", false);
            IndexKeysDocument fileNameUploadDateIdx = new IndexKeysDocument(
                new List<BsonElement> { 
                        new BsonElement(ColumnFilename, 1), 
                        new BsonElement(ColumnUploadDate, 1) });
            var index = indexes.FirstOrDefault(
                i => i.Key.Equals(fileNameUploadDateIdx.ToBsonDocument()));

            if (index == null)
            {
                cr = files.CreateIndex(fileNameUploadDateIdx, NotUnique);
                if (!cr.Ok) {
                    //Cannot create uploadDate index
                }
            }
            else if (index.IsUnique)
            {
                cr = files.DropIndexByName(index.Name);
                if (!cr.Ok) {
                    // Cannot drop index filename_1_uploadDate_1. 
                }
                else
                {
                    cr = files.CreateIndex(fileNameUploadDateIdx, NotUnique);
                    if (!cr.Ok) {
                    //Cannot create uploadDate index
                    }
                }
            }

根据Mongo docs version 3.2,如果不删除并重新创建索引,就无法修改索引上的选项。 This question 也解决了这个问题,this answer 提供了一个扩展方法来包装这个 drop 并从头开始重新创建行为。

【讨论】:

  • 这对于已经包含数据的集合来说可能非常昂贵。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 2020-05-02
  • 1970-01-01
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多