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

Studio 用户指南
上次更新日期 2025年12月15日
本教程演示如何从 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.Driver,然后选择“安装” 。 - 接受许可协议和依赖项。
要在编码自动化中使用 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 相关的错误
使用示例
NUPKG文件,您可以在自动化中包含双重代码。
- 下载
NUPKG文件。 - 将下载的
NUPKG文件上传到Orchestrator 主机或租户订阅源,您可通过 Studio 实例访问这些订阅源。有关将NUPKG文件作为自定义库上传到 Orchestrator 的更多信息,请参阅手动将库上传到 Orchestrator 。 - 打开 Studio 项目,然后打开“管理包”菜单。
- 搜索您先前保存到Orchestrator 主机或Orchestrator 租户订阅源的
MongoDB.Coded.Workflows.SampleNUPKG文件,并安装该文件。图 1.“管理包”菜单中的自定义库
- 安装该文件后,导航至“活动”面板并找到
MongoDB.Coded.Workflows.Sample。将“获取数据库”活动拖放到XAML文件中,以测试您的 MongoDB 连接并获取可用数据库的列表。图 2. “活动”面板中的 “获取数据库”活动