Skip to main content

LeanDB MongoDB Guide

LeanDB MongoDB is the hosted database provided by Cloud Engine. You can have your project connect to the database using MongoDB libraries and have access to all the functions provided by MongoDB. See Cloud Engine Overview to learn about the other hosted database services provided by Cloud Engine.

Creating and Managing Instances

You can create and manage LeanDB MongoDB instances on Dashboard > LeanEngine > Database > MongoDB.

Creating Instances

caution
Your account will start to be billed as soon as you create a LeanDB instance. Please make sure there’s enough balance in your account.

You will see the following options when you click Create instance:

  • Specification You can choose from 512M, 1GB, 2GB, 4GB, and 8GB.

Each specification has its limit on the number of connections and the space. Upgrade to a higher specification to allow for more connections and space.

You’ll see the price of the current instance once you choose a specification.

More about the billing of LeanDB

LeanDB instances are charged every day. If you use an instance for less than one day, you will be charged for one day’s usage. You will be billed for your usage of the previous day every single day. You will be charged for your LeanDB instances based on the specification you chose. Even though you don’t use the service after you create an instance, you will still be billed. You will see the prices of different specifications when you create your LeanDB instance. You can also view the prices on the pricing page of the current region. You can view your bill history on the Consumption details page on the dashboard.

danger
If your account maintains a negative balance for more than a month, all the LeanDB instances in your account, including their data, will be deleted.

MongoDB Version

LeanDB only provides MongoDB 4.0 at this time.

Resizing Online

At this time, you can’t resize LeanDB MongoDB instances on your own. Please reach out to us if you need to have your instances resized.

Sharing Instances

You can use the “Manage sharing” function to share your LeanDB instances with other applications. When you share an instance with another application, the instance will appear in this application. The relevant environment variables will also be available in this application’s Cloud Engine instances.

Accessing From Cloud Engine

When you deploy a project to the Cloud Engine instances under an application, some environment variables containing information for connecting to MongoDB will be injected, including:

  • MONGODB_URL_<NAME>

Here <NAME> is the name you provided when creating your LeanDB instance. If the name of your LeanDB instance is MYDB, there will be an environment variable named MONGODB_URL_MYDB.

To connect to MongoDB from Node.js (assuming the instance name is MYDB):

app.js
const { MongoClient } = require("mongodb");

const mongoClient = new MongoClient(process.env["MONGODB_URL_MYDB"], {
useUnifiedTopology: true,
poolSize: 10,
});

mongoClient
.connect()
.then(() => {
console.log("Connected to MongoDB");
})
.catch((err) => {
console.eror("Connect to MongoDB failed", err.message);
});

app.get("/", (req, res) => {
const cats = mongoClient.collection("cats");

res.json(cats.find({}, { limit: 10 }));
});

Managing Data

Besides accessing LeanDB with your code from Cloud Engine, you can also use the following ways to manage, debug, or perform operations on your instances.

Connecting With the CLI

You can open an interactive shell connected to LeanDB using lean db shell, a command provided by the CLI:

$ lean db shell mysqldb
Welcome to the MySQL monitor.
Your MySQL connection id is 3450564
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test
Database changed
mysql> insert into users set name = 'leancloud';
Query OK, 1 row affected (0.04 sec)
mysql> select * from users;
+------+-----------+
| id | name |
+------+-----------+
| 1 | zhenzhen |
| 2 | leancloud |
+------+-----------+
2 rows in set (0.06 sec)

With lean db proxy, you can export LeanDB to a local port and have local programs or GUI clients connect to the database:

$ lean db proxy myredis
[INFO] Now, you can connect myredis via [redis-cli -h 127.0.0.1 -a hsdt9wIAuKcTZmpg -p 5678]

As long as you keep the terminal open, you’ll be able to access LeanDB from the port 5678. You can use a GUI client to browse and interact with LeanDB. While running your project with lean up, you can also have your program connected to LeanDB using this feature. You can set the environment variable (from the output of lean db proxy):

export REDIS_URL_myredis=redis://default:hsdt9wIAuKcTZmpg@127.0.0.1:5678
note

You should only use lean db for developing and debugging locally. Don’t use it for the production environment, as the connection might be interrupted occasionally.