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

Studio 用户指南

上次更新日期 2025年11月26日

Connecting to MongoDB Atlas with coded automations

This tutorial shows how to connect to a MongoDB Atlas database from your UiPath coded automations. MongoDB Atlas is a fully-managed cloud database service that provides a reliable and scalable database solution without the need for local installations.

Context

Here are the ways to implement the MongoDB connection code:

  • Copy and paste the code into a CS file of your target project.
  • Use a custom activity package and the sample NUPKG file
When opting to use the sample NUPKG file, you have the capability to add the two-factor authentication as an activity within your XAML files.
Tip: Regardless of whether you choose to integrate the MongoDB connection code in your CS files (for coded automations) or XAML files (for low-code automations), remember that you can invoke a coded automation into a low-code one and vice versa. For more information about hybrid automations, visit Creating hybrid automations - Combining Coded and Low-code Workflows.

先决条件

  1. Ensure you have UiPath Studio (version 2024.10 or later - recommended).
  2. Ensure you have a MongoDB Atlas account.
  3. Install the MongoDB.Driver NuGet package in your project.
    1. Open your UiPath project in Studio.
    2. In the Design ribbon, go to Manage Packages.
    3. Select All Packages or the .NET tab.
    4. 搜索MongoDB.Driver
    5. Select MongoDB.Driver, and then select Install.
    6. Accept the license agreement and dependencies.

Option 1: Copy and paste the code in CS files

To use the MongoDB Atlas connection in your coded automation, you can copy and paste the following sample code in a CS file from your target project. Make sure the namespace of the file matches with the one of your project name.
Note: The sample code connects to MongoDB Atlas using the connection string format specific to cloud deployments. Make sure to replace the placeholder credentials with your actual MongoDB Atlas credentials.
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;
        }
    }
}
Connection Parameters
  • username: The database user you created in MongoDB Atlas
  • password: The password for your database user
  • cluster: Your cluster hostname from the connection string (e.g., cluster0.abc123.mongodb.net)

The sample returns a list of all databases for that specific set of credentials.

Connection String Format - MongoDB Atlas uses the mongodb+srv:// protocol, which:
  • Automatically discovers all nodes in your cluster
  • Provides automatic failover support
  • Includes retry logic for write operations (retryWrites=true)
  • Sets the write concern to majority (w=majority)

错误处理
  • MongoAuthenticationException: Occurs when credentials are incorrect
  • MongoConnectionException: Occurs when network access is blocked or cluster is unavailable
  • General exceptions: Catches any other MongoDB-related errors

Option 2: Use a custom activity package and the sample NUPKG file

Using a sample NUPKG file enables you to include the two-factor code in your automations.
  1. Download the NUPKG file.
  2. Upload the downloaded NUPKG file to your Orchestrator Host or Tenant feed, which are accessible through your Studio instance. For more information on uploading the NUPKG file as a custom library to Orchestrator, refer to Manually uploading a library to Orchestrator.
  3. 打开 Studio 项目,然后打开“管理包”菜单。
  4. Search for the MongoDB.Coded.Workflows.Sample NUPKG file you previously saved to your Orchestrator Host or Orchestrator Tenant feed, and install it.
    Figure 1. Custom library in the Manage Packages menu

  5. After you install the file, navigate to the Activities panel and locate MongoDB.Coded.Workflows.Sample. Drag and drop the GetDatabases activity into your XAML files to test your MongoDB connection and fetch a list of the available databases.
    Figure 2. GetDatabases activity in the Activities panel

此页面有帮助吗?

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