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 Developer Center > Your game > Game Services > Cloud Services > Cloud Engine > Database > MongoDB.
Creating Instances
You will see the following options when you click Create instance:
- Specification You can choose from
512M
,1GB
,2GB
,4GB
, and8GB
.
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. You can view your bill history on the Transactions page on the Developer Center.
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
.
- Node.js
- .NET (C#)
To connect to MongoDB from Node.js (assuming the instance name is MYDB
):
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 }));
});
- Make sure to install the dependencies used in the code above with
npm install mongodb
- See MongoDB Node Driver’s docs for more information
To connect to MongoDB from .NET (assuming the instance name is MYDB
):
string url = Environment.GetEnvironmentVariable("MONGODB_URL_MYDB");
MongoClient client = new MongoClient(url);
IMongoCollection<BsonDocument> collection = client.GetDatabase("leancloud")
.GetCollection<BsonDocument>("hello");
FilterDefinition<BsonDocument> filter = Builders<BsonDocument>.Filter.Empty;
Console.WriteLine(collection.Find(filter).ToList().ToJson());
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 tds db shell
, a command provided by the CLI:
$ tds 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 tds db proxy
, you can export LeanDB to a local port and have local programs or GUI clients connect to the database:
$ tds 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 tds up
, you can also have your program connected to LeanDB using this feature. You can set the environment variable (from the output of tds db proxy
):
export REDIS_URL_myredis=redis://default:[email protected]:5678
You should only use tds db
for developing and debugging locally. Don’t use it for the production environment, as the connection might be interrupted occasionally.