Is MongoDB The Best Database & GraphQL Backend for Your Project?
MongoDB in year 2021 is much improved than it was 5 years ago. In comparison to MySQL, MariaDB, and PostgreSQL, their feature set development tends to be more incremental. All of them are mature and stable technology.
MongoDB Disadvantages: The Bad Stuff
I’ll mention MongoDB disadvantages first as the advantages are more numerous compared to SQL/Relational DBMS.
In the past MongoDB disadvantage is transactions/atomicity but that has been handled since v4.2. As of March 2021, current MongoDB is 4.4.
The only remaining disadvantage I can think of is SQL compatibility with external services/apps. Many external apps support SQL out of the box, and MongoDB support is also very wide, but there are very specific use cases that not support MongoDB connector yet.
Is MongoDB good for FinTech?
Note that MariaDB and PostgreSQL also has been used by high-profile and fintech companies.
MongoDB Advantages: The Good Stuff
Some advantages of MongoDB Atlas+Realm:
- free 512 MB database during development
- horizontally scalable (MongoDB Atlas starts with 3-node replica set)
- MongoDB Realm provides GraphQL API with granular access control
- Server-side & Client-side SDK support for Node.js and mobile apps (React Native, iOS, and Android)
- Real-time (Change streaming on watched collections)
- Offline sync (most purely server-side apps do not need this feature. But sometimes in a future development, in a mobile app this is very useful and very hard feature to get right if we try to develop it ourselves)
- direct integration with Twilio (SMS services)
- integration with some AWS services
- integration with FCM push notifications (primarily needed for mobile apps, but can be used by web apps as well)
- full-text search (MariaDB and PostgreSQL also have this)
- geospatial indexes and queries (MariaDB is lacking here, while PostgreSQL has excellent support)
- cost is similar to MariaDB/PostgreSQL. in my experience MongoDB costs slightly less when given same load than SQL. When given higher load, MongoDB scales better while SQL usually requires vertical scaling.
- Currently MongoDB Atlas provides many coupons (I have verified myself) from $100 and up, so especially for a startup this is really helpful
Sign Up for MongoDB Atlas / MongoDB Realm
Sign Up for MongoDB Atlas / MongoDB Realm here.
Then you can apply one of these MongoDB Atlas coupon codes/promo/discount codes which should give your account some credits. Each coupon’s validity is usually one year. Since there is validity period, I suggest you apply one coupon and use it first until exhausted, before applying another coupon. Unless you’re sure you will use these credits soon.
- LAUREN200 – $200
- JOEK100 – $100
- GETATLAS – $100
- GOATLAS10 – $10
- PROVISIONING – $200
- GOATLAS25 – $25
FAQ: Can You Migrate Data from MongoDB to SQL RDBMS such as MySQL/MariaDB/PostgreSQL?
Here’s my plan if in the future an SQL database is needed:
- add or migrate only the subset of data that requires SQL, and with better understanding of experience and requirements to choose MariaDB or PostgreSQL
- Hasura is used to provide GraphQL integration/federation so frontend apps (web + mobile) can still use GraphQL as always, and reduce backend development time.
FAQ: Should I Deploy MongoDB Realm to Global Regions or Local Region?
If you’re not sure, Global should give better overall experience for all users around the world.
However, I noticed that as of March 2021, MongoDB Realm global regions includes eu-central-1 (Frankfurt) and ap-southeast-1 (Singapore) but excludes ap-south-1 (Mumbai). So if you’re planning to develop for Middle-East (me-south-1) and South Asia, you should use local region and choose ap-south-1 (Mumbai) instead.
Here’s my test on latency (in milliseconds) between specific AWS regions:
From | to Bahrain | to Mumbai | to Frankfurt |
---|---|---|---|
me-south-1 (Bahrain) | 24 | 162 | 522 |
Bandung, Indonesia (IndiHome) | 1520 | 730 | 1072 |
For example, if your visitor is mostly in Makkah and you use Global, it will connect to Frankfurt first, which if needs data will connect again to Mumbai. In that case it’s better to use Local so all requests go directly to Mumbai.
Testing MongoDB Realm – Region
You can create a small function in MongoDB Realm, named getRegion
:
exports = async function(arg){
/*
Accessing application's values:
var x = context.values.get("value_name");
Accessing a mongodb service:
var collection = context.services.get("mongodb-atlas").db("dbname").collection("coll_name");
var doc = collection.findOne({owner_id: context.user.id});
To call other named functions:
var result = context.functions.execute("function_name", arg1, arg2);
Try running in the console below.
*/
const response = await context.http.get({ url: "https://api.ipify.org?format=json" });
// The response body is a BSON.Binary object. Parse it and return.
const ipify = EJSON.parse(response.body.text());
return {request: context.request, ipify: ipify};
};
Go to Authentication and enable anonymous user.
Deploy the MongoDB Realm app.
Create a Node.js project that calls that function:
yarn add --dev typescript ts-node @types/node
yarn tsc --init
yarn add realm
Create file doconnect.ts
:
import Realm from "realm";
async function main() {
const app = new Realm.App({ id: "<Your App ID here>" });
const credentials = Realm.Credentials.anonymous();
try {
const user = await app.logIn(credentials);
const result = await user.functions.getRegion();
console.info("getRegion:", result);
} catch (err) {
console.error("Failed to log in", err);
}
}
main();
Run it:
yarn ts-node doconnect.ts