简体   繁体   English

如何使用C#驱动程序从现有索引中创建Mongo索引定义?

[英]How can I create a Mongo index definition from an existing one using the C# driver?

I am trying to recreate indexes from one collection for another. 我正在尝试从一个集合为另一个创建索引。

I can get the existing index from the source collection by: 我可以通过以下方式从源集合中获取现有索引:

var sourceCollectionName = "source";
var targetCollectionName = "target";

var sourceCollection = db.GetCollection<BsonDocument>(sourceCollectionName);
var targetCollection = db.GetCollection<BsonDocument>(targetCollectionName);

List<BsonDocument> sourceCollectionIndexes = await(await sourceCollection.Indexes.ListAsync()).ToListAsync();

But I cannot figure out where to go from there. 但是我不知道从那里去哪里。

It becomes a bit cumbersome if you want to run strongly typed .NET driver API like targetCollection.Indexes.CreateOne() since it requires options to be specified as CreateIndexOptions type which expectes all the options like Sparse or Unique to be specified in a strongly typed way. 如果您要运行诸如targetCollection.Indexes.CreateOne()类的强类型.NET驱动程序API,它将变得有些麻烦,因为它要求将选项指定为CreateIndexOptions类型,从而期望在强类型中指定所有SparseUnique选项方式。

Alternatively you can create multiple indexes using createIndexes database command. 或者,您可以使用createIndexes数据库命令创建多个索引。 To run it from C# you can remove ns from obtained BsonDocuments since the namespace will be different and then pass all your definitions to that command. 要从C#运行它,您可以从获得的BsonDocuments删除ns ,因为名称空间会有所不同,然后将所有定义传递给该命令。 Try: 尝试:

var sourceCollectionName = "source";
var targetCollectionName = "target";

var sourceCollection = Db.GetCollection<BsonDocument>(sourceCollectionName);

List<BsonDocument> sourceCollectionIndexes = await (await sourceCollection.Indexes.ListAsync()).ToListAsync();

foreach(var indexDef in sourceCollectionIndexes)
{
    indexDef.Remove("ns");
}

await Db.RunCommandAsync<BsonDocument>(new BsonDocument()
{
    { "createIndexes", targetCollectionName },
    {
        "indexes", new BsonArray(sourceCollectionIndexes)
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# mongo 驱动程序跟踪 mongo 命令 - How can I trace mongo commands using C# mongo driver 如何将此控制台mongodb查询转换为C#mongo驱动程序v2? - How can I convert this console mongodb query to a C# mongo driver v2 one? 如何通过一次调用使用mongo db C#驱动程序添加嵌套元素或更新属性 - How can you add a nested element or update a property using mongo db C# driver with one call 如何在不使用 Builders 的情况下使用 .net(c#) 驱动程序更新 mongo db 中的文档? - How can i update document in mongo db using .net(c#) driver without using Builders? 如何使用本机 MongoDB.Driver 将 mongo 脚本转换为纯 C# 代码? - How can I convert mongo script to pure C# code by using native MongoDB.Driver? 在 C# 中,使用 Mongo 驱动程序,如何设置“最后修改字段”? - In C#, using the Mongo Driver, how can I set a "last modified field"? mongodb:如何使用C#驱动程序创建文本索引? - mongodb : How do I create text index with C# driver? 如何使用 Mongo.Driver.Linq 和 Mongo C# 驱动程序 2.3 返回带有过滤子文档的文档? - How do I return a document with filtered sub-documents using Mongo.Driver.Linq with the Mongo C# driver 2.3? 我无法从C#驱动程序执行mongo命令 - Can't execute mongo command from C# driver when I permisions 是否可以使用Mongo DB C#驱动程序序列化基类中的属性或字段? - Can I exclude properties or fields in a base class from being serialized with Mongo DB C# driver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM