Let's talk about Mongolia — mongo db as a rest API

Kevin Da Silva
3 min readMay 11, 2021

Hi!! If you start to read this article thinking that it would about the country Mongolia, sorry to disappoint you, but today's article is about data gateways.

Photo by Joshua Sortino on Unsplash

— First of all:

“What's a data gateway?”

In simple terms is an interface to generate easy access to a database (normally using a rest API).

— Second:

“What's Mongolia?”

Mongolia is a data gateway for mongo db, built with node js and express and heavily inspired by datastax stargate(Cassandra) and prest(postgres).

“Why would I use such a tool like mongolia?”

In fact in the most part of applications developers normally use the drivers that communicate with the database(ex mongoose in nodejs and MongoDB in Haskell). But there are reasons why you might use a data gateway like Mongolia for:

→The language you are currently using doesn't support / have drivers for mongodb or the existent drivers are not very good or have a lack of documentation

→You are developing a poc, at a hackathon like event or just need simple storage without a complex backend.

→You might need an easy and simple composable instance of a database access

“How can I get started with Mongolia?”

You can get more information as well as documentation in the official repository: https://github.com/KevinDaSilvaS/mongolia

But Mongolia also can be started with ease using docker:

1-First of all: let's create an instance of mongo db:

docker run -e MONGO_INITDB_ROOT_USERNAME=mongolia -e MONGO_INITDB_ROOT_PASSWORD=123 MONGO_INITDB_DATABASE=admin -p 27017:27017

2-Lets create an instance of Mongolia:

docker run -e MONGO_USERNAME=mongolia -e MONGO_PASSWORD=123 -e MONGODB_NAME=admin -e MONGODB_HOST=localhost -e MONGODB_PORT=27017 -p 3170:3170 kevindasilvas/mongolia

3-Authenticate in Mongolia:

//make a POST request to: localhost:3170/auth
//send a body containing the mongo username and password:
{ "username": "mongolia", "password": "123" }
//if successful you will receive a 201 response code
//with a response body containing something like this:
{
"code": 201,
"details": {
"mongolia_auth_token": "b61fddb0-622a-4b56-8144-4b116aa480cf"
}
}
//keep the mongolia_auth_token we are going to need it in the next steps

3-Creating a collection:

//make a POST request to localhost:3170/collections/
// containing the header:
"mongolia_auth_token": "b61fddb0-622a-4b56-8144-4b116aa480cf"
//and a body:
{
"collectionName": "users",
"collectionProperties": {
"name": {
"type": "String",
"required":true
},
"age": {
"type": "Number" }
}
}
//if successful you will receive a 204 response code

4-Inserting in collection:

//make a POST request to localhost:3170/collections/users
// containing the header:
"mongolia_auth_token": "b61fddb0-622a-4b56-8144-4b116aa480cf"
//and a body:
{ "name": "kevin", "age": 21 }
//if successful you will receive a 201 response code
//with a response body containing something like this:
{
"code": 201,
"details": {
"_id": "60843c79632f1dc33f3dbeaa",
"name": "kevin",
"age": 21, "__v": 0
}
}

5-Get info in collection:

//make a GETrequest to localhost:3170/collections/users
// containing the header:
"mongolia_auth_token": "b61fddb0-622a-4b56-8144-4b116aa480cf"
//and a query:
localhost:3170/collections/users?name=kevin
localhost:3170/collections/users?age=@GTE>18
localhost:3170/collections/users?_id=60843c79632f1dc33f3dbeaa
//more about queries in here:https://github.com/KevinDaSilvaS/mongolia#comparitive-queries
//if successful you will receive a 200 response code
//with a response body containing something like this:
{
"code": 200,
"details": [
{
"_id": "6091d23d373a4e4be96243ef",
"name": "kevin",
"age": 21,
"__v": 0
}
]
}

6-Update info in collection

//make a PATCH request to localhost:3170/collections/users?name=kevin// containing the header:
"mongolia_auth_token": "b61fddb0-622a-4b56-8144-4b116aa480cf"
//and a body:
{
"name": "kevin updated this record"
}
//if successful you will receive a 200 response code
//with a response body containing something like this:
{
"code": 200,
"details": {
"n": 3,
"nModified": 3,
"ok": 1
}
}

7-Delete info in collection

//make a DELETE request to localhost:3170/collections/users?name=kevin// containing the header:
"mongolia_auth_token": "b61fddb0-622a-4b56-8144-4b116aa480cf"
//if successful you will receive a 200 response code
//with a response body containing something like this:
{
"code": 200,
"details": {
"n": 0,
"ok": 1,
"deletedCount": 0
}
}

Congrats now you've learned how to get started with Mongolia, thank you so much for reading, and again if you want to know more about Mongolia feel free to read the documentation here: https://github.com/KevinDaSilvaS/mongolia#welcome-to-mongolia

If you found a bug, a feature request, or get any doubts about Mongolia your issue will be welcomed in the Github repository and of course, we are always open for contributions

Bye!

--

--

Kevin Da Silva

I'm a back-end developer, functional programming lover, fascinated by computer science and languages. From the south part of Brazil to the world