You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C#实现MongoDB Atlas跨集群索引列表读取与写入

实现MongoDB Atlas索引跨集群同步的C#方案

核心思路

  • 用MongoDB .NET Driver连接源、目标两个Atlas集群
  • 遍历源集群的所有数据库、集合,拉取完整索引信息
  • 将索引同步到目标集群(自动跳过默认的_id_索引)

步骤与代码实现

1. 安装依赖

先在项目中引入MongoDB官方的.NET驱动:

dotnet add package MongoDB.Driver

2. 全量同步核心代码

using MongoDB.Driver;
using MongoDB.Bson;
using System;
using System.Linq;

class IndexSyncTool
{
    static async System.Threading.Tasks.Task Main(string[] args)
    {
        // 替换为你的Atlas集群连接串
        string sourceConn = "mongodb+srv://<source-username>:<source-password>@<source-cluster>.mongodb.net/";
        string targetConn = "mongodb+srv://<target-username>:<target-password>@<target-cluster>.mongodb.net/";

        var sourceClient = new MongoClient(sourceConn);
        var targetClient = new MongoClient(targetConn);

        // 获取源集群所有数据库名称
        var dbNames = await sourceClient.ListDatabaseNamesAsync().ToListAsync();

        foreach (var dbName in dbNames)
        {
            var sourceDb = sourceClient.GetDatabase(dbName);
            var targetDb = targetClient.GetDatabase(dbName);

            // 获取当前数据库的所有集合
            var collNames = await sourceDb.ListCollectionNamesAsync().ToListAsync();

            foreach (var collName in collNames)
            {
                var sourceColl = sourceDb.GetCollection<BsonDocument>(collName);
                var targetColl = targetDb.GetCollection<BsonDocument>(collName);

                // 获取源集合的所有索引
                var indexDocs = await sourceColl.Indexes.ListAsync().ToListAsync();

                foreach (var indexDoc in indexDocs)
                {
                    // 跳过默认主键索引,避免重复创建报错
                    if (indexDoc["name"].AsString == "_id_")
                        continue;

                    try
                    {
                        await targetColl.Indexes.CreateOneAsync(new CreateIndexModel<BsonDocument>(indexDoc));
                        Console.WriteLine($"已同步: DB[{dbName}] 集合[{collName}] 索引[{indexDoc["name"]}]");
                    }
                    catch (MongoCommandException ex)
                    {
                        // 索引已存在时跳过,其他错误记录日志
                        if (ex.Code == 85)
                            Console.WriteLine($"索引已存在,跳过: DB[{dbName}] 集合[{collName}] 索引[{indexDoc["name"]}]");
                        else
                            Console.WriteLine($"同步失败: {ex.Message}");
                    }
                }
            }
        }

        Console.WriteLine("全量索引同步完成");
    }
}

关键注意事项

  • 权限配置:源集群用户需拥有listDatabases、listCollections、listIndexes权限;目标集群用户需拥有createIndex权限
  • 增量追踪优化:如果要实时监控开发创建索引的事件(而非全量同步),可以用MongoDB的**变更流(Change Streams)**监听createIndexes操作,只同步新增索引
  • 错误处理扩展:可根据需求添加重试机制、异常告警等逻辑,比如同步失败时写入本地日志文件
  • 性能适配:针对超大规模集群,建议分批遍历数据库和集合,避免一次性加载过多数据导致内存占用过高

增量追踪实现(实时监控索引创建)

如果不需要全量同步,只想追踪新创建的索引,用变更流替代全量遍历:

// 监听源数据库的索引创建事件
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>()
    .Match(change => change.OperationType == ChangeStreamOperationType.CreateIndexes);

using var cursor = await sourceDb.WatchAsync(pipeline);
while (await cursor.MoveNextAsync())
{
    foreach (var change in cursor.Current)
    {
        var collName = change.Namespace.CollectionName;
        var targetColl = targetDb.GetCollection<BsonDocument>(collName);
        
        // 提取本次创建的所有索引
        var newIndexes = change.UpdateDescription["createdIndexes"].AsBsonArray;
        foreach (var indexDoc in newIndexes)
        {
            try
            {
                await targetColl.Indexes.CreateOneAsync(new CreateIndexModel<BsonDocument>(indexDoc.AsBsonDocument));
                Console.WriteLine("已同步新增索引: DB[{dbName}] 集合[{collName}] 索引[{indexDoc["name"]}]");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"新增索引同步失败: {ex.Message}");
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者bullmaca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 16:15:49