Skip to main content

Connect Your Own MongoDB

You can connect Tencent Cloud MongoDB database to the CloudBase platform, reuse existing database resources, and reduce data migration costs.

After connecting Tencent Cloud database, you can:

  • Seamless Integration: Directly manage data in your existing database without data migration
  • Data Models: Quickly generate data models based on existing collections for automated data management
  • SDK Support: Unified access using CloudBase Web SDK / Node SDK / Mini Program SDK
  • Secure and Controllable: Data remains in your database with full control over data security
Note

Currently only supports connecting "Tencent Cloud MongoDB Database (Shanghai Region)", other cloud service providers or self-built MongoDB databases are not supported yet.

Prerequisites

Before configuring Tencent Cloud MongoDB database connection, please ensure:

Check ItemRequirements
Database SourceMust be a Tencent Cloud MongoDB database instance
Network AccessDatabase has public network access capability or intranet access configured
User PermissionsDedicated database user account has been created with collection operation permissions
Firewall ConfigurationDatabase firewall has opened the corresponding port (MongoDB default 27017)
IP WhitelistCloudBase service IP whitelist has been configured (see end of document)

Create Connection Configuration

1. Access Configuration Page

Visit CloudBase Platform/Document Database, click the "Add Connection" button at the top

Document Database Add Page

2. Fill in Connection Information

ParameterDescriptionRequired
Configuration NameCustom name for identifying this connection in the platformYes
Configuration IdentifierEnglish identifier for specifying the connection in SDK (e.g., my-mongodb)Yes
Database NameTarget database name to connectYes
UsernameDatabase connection usernameYes
PasswordDatabase connection passwordYes
Connection ParametersAdditional MongoDB connection parameters (e.g., authSource=admin)No
TimeoutConnection and query timeout (unit: seconds)No

3. Set Default Instance (Optional)

After creating a connection configuration, you can set it as the default instance to simplify code calls.

Purpose of Default Instance

  • Data Model Support: The default database supports creating data models, which can be operated via data model APIs and used in WeDa low-code platform
  • Auto SDK Connection: All SDKs (JavaScript/Node.js/Mini Program) will point to the default instance's default database when calling relevant interfaces without explicitly specifying the target instance and database

How to Set

Visit CloudBase Platform/Document Database/Connection Management, click "Set as Default Instance" button on the target connection configuration.

Impact of Switching

Switching the default database may affect the calling method of the original database in business code. It is recommended to check the code and perform operations during off-peak business hours.

Affected SDK Call Examples:

  • Mini Program: wx.cloud.database() (without instance parameter)
  • Web SDK: app.database() (without instance parameter)
  • Node SDK: app.database() (without instance parameter)

Usage Methods

After connecting your own NoSQL database, you can operate data in the following two ways.

Method 1: Direct Database Collection Operations

Perform CRUD operations directly through SDK

Method 1: Using Default Instance (wx Native API)

Using wx.cloud.database() in mini programs calls the default database instance. To access Tencent Cloud MongoDB database, please first set it as the "Default Instance" in CloudBase console.

// Initialize CloudBase
wx.cloud.init({
env: 'your-env-id', // Replace with your environment ID
});

// Auto connect to default instance (no need to specify instance)
const db = wx.cloud.database();

const result = await db.collection("users").where({
status: "active"
}).get();

Method 2: Using JS SDK to Specify Instance

You can explicitly specify the database instance to access via @cloudbase/js-sdk.

import cloudbase from '@cloudbase/js-sdk';

const app = cloudbase.init({
env: 'your-env-id', // Replace with your environment ID
region: "ap-shanghai", // Default is ap-shanghai if region is not specified
});

// Login authentication
const auth = app.auth();
await auth.signInWithOpenId();

// Specify access to Tencent Cloud MongoDB instance
const db = app.database({
instance: "my-mongodb", // Configuration identifier
database: "test",
});

const result = await db.collection("users").where({
status: "active"
}).get();
Parameter Description
  • instance: The "Configuration Identifier" filled when creating the connection configuration
  • database: Target database name
  • For query syntax, please refer to: Web SDK or Node SDK

Method 2: Using Data Models

Suitable for scenarios requiring structured data management, automatic validation, relational queries, etc.

1. Create Data Model

Visit CloudBase Platform/Document Database/Connection Management, click "Data Model" on the corresponding connection configuration

Select an existing collection, and the system will automatically generate the corresponding data model.

2. Operate Data Models

// Import SDK
const { init } = require("@cloudbase/wx-cloud-client-sdk");

wx.cloud.init({
env: "your-env-id", // Replace with your environment ID
});

const client = init(wx.cloud);
const models = client.models;

// Create data (assuming the todos model has been created)
const { data, error } = await models.todos.create({
data: {
title: "Learn CloudBase",
completed: false,
},
});

// Query data
const todos = await models.todos.findMany({
where: {
completed: false,
},
orderBy: {
createdAt: "desc",
},
});

console.log(todos);
Tip

For detailed usage, please refer to Data Model Documentation

FAQ

Will data be synchronized to CloudBase?

No. After connecting your own database:

  • Data Storage: All data remains in your database
  • Access Method: CloudBase SDK directly connects to your database for operations
  • Data Control: You have full control over data storage and backup