NoSQL Dev Tools – API Documentation & Cheat Sheets

API documentation and cheat sheets are essential tools for developers working with databases and backend services. They provide clear, quick access to the commands, snippets, and configurations needed to interact with databases effectively. This article covers ready-to-use code snippets for CRUD operations, connection string formats for popular programming environments, and Docker-compose files to simplify development environment setup.

Ready-to-Copy Snippets for CRUD Operations

CRUD stands for Create, Read, Update, and Delete — the four basic operations you perform on data in a database. Having ready-made snippets speeds up development and reduces errors. Similar CRUD operations can be adapted for other languages and databases with minor syntax changes.

Here’s a brief overview with examples in Node.js using a popular database driver:

Create

Create

db.collection(‘users’).insertOne({ name: ‘Alice’, age: 30 }, (err, result) => {

  if (err) throw err;

  console.log(‘User created:’, result.insertedId);

});

Read

Read

db.collection(‘users’).find({ age: { $gte: 18 } }).toArray((err, users) => {

  if (err) throw err;

  console.log(‘Adult users:’, users);

});

Update

Update

db.collection(‘users’).updateOne(

 { name: ‘Alice’ },

 { $set: { age: 31 } },

 (err, result) => {

  if (err) throw err;

  console.log(‘User updated:’, result.modifiedCount);

 }

);

Delete

Delete

db.collection(‘users’).deleteOne({ name: ‘Alice’ }, (err, result) => {

  if (err) throw err;

  console.log(‘User deleted:’, result.deletedCount);

});

Connection String Formats for Various Environments

Connecting to a database requires a connection string — a formatted string containing your server’s address, credentials, and other options. Here are common formats for Node.js, Python, Java, and Go.

Node.js

Node.js

const uri = “mongodb+srv://username:password@cluster0.mongodb.net/mydatabase?retryWrites=true&w=majority”;

Python

Python

uri = “mongodb+srv://username:password@cluster0.mongodb.net/mydatabase?retryWrites=true&w=majority”

client = pymongo.MongoClient(uri)

Java

Java

String uri = “mongodb+srv://username:password@cluster0.mongodb.net/mydatabase?retryWrites=true&w=majority”;

MongoClient mongoClient = MongoClients.create(uri);

Go

Go

uri := “mongodb+srv://username:password@cluster0.mongodb.net/mydatabase?retryWrites=true&w=majority”

client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))

Docker-Compose Files for Spinning Up Dev Environments

Docker-compose lets you define and run multi-container Docker applications with a simple YAML file. For development, it’s convenient to spin up a database alongside your app using Docker. Running docker-compose up in the folder containing this file will start the database container, accessible on localhost port 27017.

You can extend this file to include your backend service, linking it to the database container for easier testing and development.

A simple example of the docker-compose.yml file creating a development MongoDB instance is thus:

version: ‘3.8’

services:

 mongodb:

  image: mongo:latest

  container_name: dev-mongodb

  ports:

   – “27017:27017”

 volumes:

   – ./data/db:/data/db

 environment:

 MONGO_INITDB_ROOT_USERNAME: root

 MONGO_INITDB_ROOT_PASSWORD: examplepassword

Best Practices

Follow best practices to leverage the goodness of documentations and API sheets with cheats. One must update the snippets and connection strings with the latest versions of his database driver or API, ensuring that all code and connections are up-to-date. If something is outdated, it means that the example might cause harm or the wrong codes.

Set your cheat sheets before proceeding with these next steps in an organised way, so that most of the related operations and configurations are bunched up. You should always find what you need directly during development without having to see all the crap. Lastly, add notes or comments to your cheat-sheet document explaining things about things of your not-obviously understandable constructs in your code. Both save time when you get back to refactor it and make it better for you and your team.

Summary

These tools make development faster and allow less configuration errors in a consistent environment. Not only will these snippets speed up the development process, but they also reduce configuration errors and maintain consistency across various environments in which they are used.