studio
latest
false
重要 :
新发布内容的本地化可能需要 1-2 周的时间才能完成。
UiPath logo, featuring letters U and I in white

Studio 用户指南

上次更新日期 2026年1月13日

使用已编码自动化连接到 MongoDB Atlas

本教程介绍如何从 UiPath 已编码自动化连接到 MongoDB Atlas 数据库。 MongoDB Atlas 是一项完全托管的云数据库服务,可提供可靠且可扩展的数据库解决方案,无需本地安装。

Context

以下是实现 MongoDB 连接代码的方法:

  • 将代码复制并粘贴到目标项目的CS文件中。
  • 使用自定义活动包和示例NUPKG文件
当选择使用示例NUPKG文件时,您可以将双因素身份验证作为活动添加到您的XAML文件中。
提示:无论您选择将 MongoDB 连接代码集成到 CS 文件(用于编码自动化)还是 XAML 文件(用于低代码自动化)中,请记住,您可以将编码自动化调用为低代码自动化,反之亦然。 有关混合自动化,请参阅“创建混合自动化 - 将编码和低代码工作流相结合”

先决条件

  1. 确保您拥有 UiPath Studio(2024.10 或更高版本 - 推荐)。
  2. 确保您有 MongoDB Atlas 帐户。
  3. 在您的项目中安装 MongoDB.DriverNuGet 包。
    1. 在 Studio 中打开您的 UiPath 项目。
    2. “设计”功能区中,转到“管理包”
    3. 选择“所有包”“.NET”选项卡。
    4. 搜索MongoDB.Driver
    5. 选择,然后选择安装。
    6. 接受许可协议和依赖项。

选项 1:复制代码并将其粘贴到 CS 文件中

要在编码自动化中使用MongoDB Atlas连接,您可以将以下示例代码复制并粘贴到目标项目中的文件中。确保文件的命名空间与您的项目名称中的命名空间匹配。

注意:示例代码使用Cloud部署专属的连接string格式连接到 MongoDB Atlas。确保将占位符凭据替换为实际的 MongoDB Atlas 凭据。
using System;
using System.Collections.Generic;
using System.Data;
using UiPath.CodedWorkflows;
using UiPath.Core;
using UiPath.Core.Activities.Storage;
using MongoDB.Driver;

namespace MongoDBCodedWorkflowsSample
{
    public class TestConnection : CodedWorkflow
    {
        [Workflow]
        public string Execute(string username, string password, string cluster)
        {
            List<string> databaseNames = new List<string>();
        try
            {
                // Create MongoDB Atlas connection string
                string connectionString = $"mongodb+srv://{username}:{password}@{cluster}/?retryWrites=true&w=majority";
                Console.WriteLine("Connecting to MongoDB Atlas...");
                // Initialize MongoDB client
                var client = new MongoClient(connectionString);
                // List all databases to verify connection
                Console.WriteLine("\nAvailable databases:");
                databaseNames = client.ListDatabaseNames().ToList();
                foreach (var dbName in databaseNames)
                {
                    Console.WriteLine($"- {dbName}");
                }
                Console.WriteLine("\nConnection successful!");
            }
            catch (MongoAuthenticationException authEx)
            {
                Console.WriteLine($"Authentication error: {authEx.Message}");
                Console.WriteLine("Please verify your username and password.");
                throw;
            }
            catch (MongoConnectionException connEx)
            {
                Console.WriteLine($"Connection error: {connEx.Message}");
                Console.WriteLine("Please check your network access settings in MongoDB Atlas.");
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"MongoDB connection error: {ex.Message}");
                throw;
            }
            return databaseNames;
        }
    }
}using System;
using System.Collections.Generic;
using System.Data;
using UiPath.CodedWorkflows;
using UiPath.Core;
using UiPath.Core.Activities.Storage;
using MongoDB.Driver;

namespace MongoDBCodedWorkflowsSample
{
    public class TestConnection : CodedWorkflow
    {
        [Workflow]
        public string Execute(string username, string password, string cluster)
        {
            List<string> databaseNames = new List<string>();
        try
            {
                // Create MongoDB Atlas connection string
                string connectionString = $"mongodb+srv://{username}:{password}@{cluster}/?retryWrites=true&w=majority";
                Console.WriteLine("Connecting to MongoDB Atlas...");
                // Initialize MongoDB client
                var client = new MongoClient(connectionString);
                // List all databases to verify connection
                Console.WriteLine("\nAvailable databases:");
                databaseNames = client.ListDatabaseNames().ToList();
                foreach (var dbName in databaseNames)
                {
                    Console.WriteLine($"- {dbName}");
                }
                Console.WriteLine("\nConnection successful!");
            }
            catch (MongoAuthenticationException authEx)
            {
                Console.WriteLine($"Authentication error: {authEx.Message}");
                Console.WriteLine("Please verify your username and password.");
                throw;
            }
            catch (MongoConnectionException connEx)
            {
                Console.WriteLine($"Connection error: {connEx.Message}");
                Console.WriteLine("Please check your network access settings in MongoDB Atlas.");
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"MongoDB connection error: {ex.Message}");
                throw;
            }
            return databaseNames;
        }
    }
}
连接参数
  • 用户名:您在 MongoDB Atlas 中创建的数据库用户
  • 密码:数据库用户的密码
  • cluster:来自连接string(例如,)的群集主机名

示例返回该特定凭据集的所有数据库的列表。

连接字符串格式 - MongoDB Atlas 使用 mongodb+srv:// 协议,该协议:
  • 自动发现群集中的所有节点
  • 提供自动故障转移支持
  • 包含写入操作的重试逻辑 (retryWrites=true)
  • 将写入关注点设置为多数 ()

错误处理
  • MongoAuthenticationException:当凭据不正确时发生
  • MongoConnectionException:当网络访问被阻止或群集不可用时
  • 常规异常:捕获任何其他 MongoDB 相关错误

选项 2:使用自定义活动包和示例NUPKG文件

使用示例NUPKG文件,您可以将双因素代码包含在自动化中。
  1. 下载 NUPKG文件
  2. 将下载的NUPKG文件上传到 Orchestrator 主机租户订阅源,您可以通过 Studio 实例访问这些订阅源。 有关将NUPKG文件作为自定义库上传到 Orchestrator 的更多信息,请参阅“手动将库上传到 Orchestrator”。
  3. 打开 Studio 项目,然后打开“管理包”菜单。
  4. 搜索您之前保存到 Orchestrator 主机Orchestrator 租户订阅源中的MongoDB.Coded.Workflows.SampleNUPKG文件,然后安装该文件。
    图 1. “管理包”菜单中的自定义库

  5. 安装文件后,导航到“活动”面板,然后找到 MongoDB.Coded.Workflows.Sample。 将“获取数据库”活动拖放到您的XAML文件中,以测试 MongoDB 连接,并获取可用数据库的列表。
    图 2. “活动”面板中的“获取数据库”活动

此页面有帮助吗?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath Logo
信任与安全
© 2005-2026 UiPath。保留所有权利。