有没有一种创建或更新MongoDB索引的方法? [英] Is there a way to create or update a MongoDB index?

查看:209
本文介绍了有没有一种创建或更新MongoDB索引的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 createIndexes命令上的文档:

如果您使用一组选项创建索引,然后使用相同的索引字段但使用不同的选项发布createIndexes,则MongoDB不会更改选项,也不会重建索引.

If you create an index with one set of options and then issue createIndexes with the same index fields but different options, MongoDB will not change the options nor rebuild the index.

解决方案是删除索引并从头开始创建索引,但这很昂贵.

The solution is to drop the index and create it from scratch, but that's costly.

有没有办法在没有索引的情况下创建索引,如果索引具有相同的选项,则不执行任何操作,如果选项已更改,则替换索引吗?

Is there a way to create an index when there isn't one, do nothing if there is an index with the same options, and replace the index if the options have changed?

这个问题最初是由 Phil Barresi 提出的. Questions/24811818/什么是最佳方法,以确保在此使用a-mongodb-index-c-sharp-driver/24812176#24812176>此处,但此后已被删除.

This question was originally raised by Phil Barresi here but has since been deleted.

推荐答案

查看驱动程序我已经实现了CreateOrUpdateIndex扩展方法,用于比较原始索引文档,如果索引选项已更改,则替换索引(只要索引名称保持不变):

Looking at the driver I've implemented a CreateOrUpdateIndex extension method that compares the raw index documents and if the index options have changed the index is replaced (as long as the index name stays the same):

public static WriteConcernResult CreateOrUpdateIndex(
    this MongoCollection mongoCollection,
    IMongoIndexKeys keys,
    IMongoIndexOptions options = null)
{
    if (mongoCollection.IndexExists(keys))
    {
        var indexDocument = mongoCollection.GenerateIndexDocument(keys, options);
        if (!mongoCollection.GetIndexes().RawDocuments.Any(indexDocument.Equals))
        {
            mongoCollection.DropIndex(keys);
        }
    }

    return mongoCollection.CreateIndex(keys, options);
}

生成原始索引文档:

public static BsonDocument GenerateIndexDocument(this MongoCollection mongoCollection, IMongoIndexKeys keys, IMongoIndexOptions options)
{
    var optionsDocument = options.ToBsonDocument();
    var keysDocument = keys.ToBsonDocument();
    var indexDocument = new BsonDocument
    {
        { "ns", mongoCollection.FullName },
        { "name", GenerateIndexName(keysDocument, optionsDocument) },
        { "key", keysDocument }
    };
    if (optionsDocument != null)
    {
        indexDocument.Merge(optionsDocument);
    }

    return indexDocument;
}

public static string GenerateIndexName(IEnumerable<BsonElement> keys, BsonDocument options)
{
    const string name = "name";
    if (options != null && options.Contains(name)) return options[name].AsString;

    return string.Join("_", keys.Select(element =>
    {
        var value = "x";
        switch (element.Value.BsonType)
        {
            case BsonType.Int32: value = ((BsonInt32)element.Value).Value.ToString(); break;
            case BsonType.Int64: value = ((BsonInt64)element.Value).Value.ToString(); break;
            case BsonType.Double: value = ((BsonDouble)element.Value).Value.ToString(); break;
            case BsonType.String: value = ((BsonString)element.Value).Value; break;
        }
        return string.Format("{0}_{1}", element.Name, value.Replace(' ', '_'));
    }));
}

这篇关于有没有一种创建或更新MongoDB索引的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆