Integrate with MongoDB
MongoDB is a flexible document database system that stores data in JSON-like documents.
Official Website: https://www.mongodb.com
- Node.js
- Python
Documentation: http://mongodb.github.io/node-mongodb-native/
NPM: https://www.npmjs.com/package/mongodb
Source Code: https://github.com/mongodb/node-mongodb-native
Getting started
-
Create a MongoDB client in
/less/shared/mongodb_client/index.js
./less/shared/mongodb_client/index.jsconst { MongoClient } = require('mongodb');
const MONGODB = {
URI: 'mongodb+srv://YOUR_MONGODB_URI',
DATABASE_NAME: 'YOUR_DATABASE_NAME',
COLLECTION_NAME: 'YOUR_DATABASE_COLLECTION'
};
let client;
const connect = async () => {
if (!client) {
client = new MongoClient(MONGODB.URI);
await client.connect();
}
};
const insert_item = async (item) => {
await connect();
const db = client.db(MONGODB.DATABASE_NAME);
const collection = db.collection(MONGODB.COLLECTION_NAME);
return await collection.insertOne(item);
};
const list_items = async () => {
await connect();
const db = client.db(MONGODB.DATABASE_NAME);
const collection = db.collection(MONGODB.COLLECTION_NAME);
return await collection.find().toArray();
}
module.exports = {
insert_item,
list_items
}; -
Create a
POST /items
route in/less/apis/demo/items/post.js
./less/apis/demo/items/post.jsconst { insert_item } = require('mongodb_client');
exports.process = async (request, response) => {
const payload = JSON.parse(request.body);
await insert_item(payload);
response.body = JSON.stringify(payload);
response.statusCode = 201;
return response;
}; -
Create a
GET /items
route in/less/apis/demo/items/get.js
./less/apis/demo/items/get.jsconst { list_items } = require('mongodb_client');
exports.process = async (request, response) => {
const result = await list_items();
response.body = JSON.stringify(result);
response.statusCode = 200;
return response;
}; -
Deploy!
less-cli deploy YOUR_PROJECT_NAME
Documentation: https://pymongo.readthedocs.io/en/stable/
Pypi Package: https://pypi.org/project/pymongo/
Source Code: https://github.com/mongodb/mongo-python-driver
Getting started
-
Create a Python Mongodb client in
/less/shared/mongodb_client_py/__init__.py
.import os
from pymongo import MongoClient
MONGODB_URI = os.environ.get('MONGODB_URI')
MONGODB_DATABASE_NAME = os.environ.get('MONGODB_DATABASE_NAME')
client = MongoClient(MONGODB_URI)
db = client[MONGODB_DATABASE_NAME]
collection = db.['my_collection']
def insert_item(item):
post_id = collection.insert_one(item)
def get_all_items():
return collection.find() -
Create a
GET /hello/py
route in/less/apis/demo/hello/py/get.py
to test.from mongodb_client_py import insert_item, get_all_items
def process(request, response):
# Insert item.
insert_item({ 'bar': 'baz' })
# Get all items.
result = get_all_items()
# Return HTTP response.
response.body = result
return response -
Deploy!
npx @chuva.io/less-cli deploy YOUR_PROJECT_NAME