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

Studio 用户指南

上次更新日期 2025年12月12日

使用已编码自动化连接到 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.Driver NuGet 包。
    1. 在 Studio 中打开 UiPath 项目。
    2. “设计”功能区中,转到“管理包”
    3. 选择“所有包”“.NET”选项卡。
    4. 搜索MongoDB.Driver
    5. 选择MongoDB.Driver ,然后选择“安装”
    6. 接受许可协议和依赖项。

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

要在编码自动化中使用 MongoDB Atlas 连接,可以将以下示例代码复制并粘贴到目标项目的CS文件中。确保文件的命名空间与您的项目名称之一匹配。
注意:示例代码使用特定于云部署的连接字符串格式连接到 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 中创建的数据库用户
  • Password :数据库用户的密码
  • cluster : 连接字符串中的集群主机名(例如cluster0.abc123.mongodb.net

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

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

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

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

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

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

此页面有帮助吗?

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