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

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

Mini Program Limitation

Using wx.cloud.database() in mini programs calls the default database instance. wx native API does not support specifying connection to other database instances temporarily.

To access Tencent Cloud MongoDB database:

  • Set Tencent Cloud MongoDB configuration as "Default Instance" in CloudBase console
  • Access your own database via JS SDK and Mini Program Adapter

JSSDK+Mini Program Adapter Example

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

// Use WeChat mini program adapter
cloudbase.useAdapters(adapter);

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
});

// 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