studio
latest
false
- 入门指南
- 设置和配置
- 自动化项目
- 依赖项
- 工作流类型
- 控制流程
- 文件比较
- 自动化最佳实践
- 源代码控件集成
- 调试
- 日志记录
- 诊断工具
- 工作流分析器
- 变量
- 参数
- 导入的命名空间
- 编码自动化
- 基于触发器的 Attended 自动化
- 对象存储库
- ScreenScrapeJavaSupport 工具
- 扩展程序
- Studio 测试
- 故障排除
重要 :
新发布内容的本地化可能需要 1-2 周的时间才能完成。

Studio 用户指南
上次更新日期 2026年1月13日
本教程介绍如何从 UiPath 已编码自动化连接到 MongoDB Atlas 数据库。 MongoDB Atlas 是一项完全托管的云数据库服务,可提供可靠且可扩展的数据库解决方案,无需本地安装。
以下是实现 MongoDB 连接代码的方法:
-
将代码复制并粘贴到目标项目的
CS文件中。 -
使用自定义活动包和示例
NUPKG文件
当选择使用示例
NUPKG文件时,您可以将双因素身份验证作为活动添加到您的XAML文件中。
提示:无论您选择将 MongoDB 连接代码集成到 CS 文件(用于编码自动化)还是 XAML 文件(用于低代码自动化)中,请记住,您可以将编码自动化调用为低代码自动化,反之亦然。 有关混合自动化,请参阅“创建混合自动化 - 将编码和低代码工作流相结合”。
- 确保您拥有 UiPath Studio(2024.10 或更高版本 - 推荐)。
- 确保您有 MongoDB Atlas 帐户。
- 在您的项目中安装
MongoDB.DriverNuGet 包。- 在 Studio 中打开您的 UiPath 项目。
- 在“设计”功能区中,转到“管理包”。
- 选择“所有包”或“.NET”选项卡。
- 搜索
MongoDB.Driver。 - 选择,然后选择安装。
- 接受许可协议和依赖项。
要在编码自动化中使用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 相关错误
使用示例
NUPKG文件,您可以将双因素代码包含在自动化中。
- 下载
NUPKG文件。 - 将下载的
NUPKG文件上传到 Orchestrator 主机或租户订阅源,您可以通过 Studio 实例访问这些订阅源。 有关将NUPKG文件作为自定义库上传到 Orchestrator 的更多信息,请参阅“手动将库上传到 Orchestrator”。 - 打开 Studio 项目,然后打开“管理包”菜单。
- 搜索您之前保存到 Orchestrator 主机或 Orchestrator 租户订阅源中的
MongoDB.Coded.Workflows.SampleNUPKG文件,然后安装该文件。图 1. “管理包”菜单中的自定义库
- 安装文件后,导航到“活动”面板,然后找到
MongoDB.Coded.Workflows.Sample。 将“获取数据库”活动拖放到您的XAML文件中,以测试 MongoDB 连接,并获取可用数据库的列表。图 2. “活动”面板中的“获取数据库”活动