Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Setting Up NoSQL Databases

Introduction to NoSQL Databases

NoSQL databases are designed to handle large volumes of unstructured data. They provide flexible schemas, horizontal scaling, and high performance for various applications. In this tutorial, we will cover how to set up a NoSQL database, specifically focusing on MongoDB and Cassandra as examples.

Prerequisites

Before we begin, ensure you have the following:

  • A computer with internet access.
  • Basic knowledge of databases and command line usage.
  • Node.js installed (optional, for MongoDB setup).

Setting Up MongoDB

Step 1: Download and Install MongoDB

Visit the MongoDB Community Server download page and download the installer suitable for your operating system. Follow the installation instructions provided on the website.

Step 2: Start MongoDB Server

Once MongoDB is installed, you can start the server using the command line. Run the following command:

mongod

This command starts the MongoDB server. By default, it will run on localhost:27017.

Step 3: Connect to MongoDB

In a new command line window, type the following command to connect to the MongoDB shell:

mongo

You should see a prompt indicating that you are connected to the MongoDB server.

Step 4: Create a Database and Collection

To create a database and a collection, use the following commands:

use myDatabase
db.createCollection("myCollection")

This creates a database named myDatabase and a collection named myCollection.

Setting Up Cassandra

Step 1: Download and Install Cassandra

Visit the Apache Cassandra download page and download the appropriate version for your operating system. Follow the installation guide provided in the documentation.

Step 2: Start Cassandra

After installation, you can start Cassandra by executing the following command in the terminal:

cassandra -f

The -f flag runs Cassandra in the foreground, allowing you to see log messages.

Step 3: Connect to Cassandra

Open another terminal window and connect to the Cassandra shell using:

cqlsh

This opens the CQL shell where you can execute CQL commands.

Step 4: Create a Keyspace and Table

To create a keyspace and a table, use the following commands:

CREATE KEYSPACE myKeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
USE myKeyspace;
CREATE TABLE myTable (id UUID PRIMARY KEY, name TEXT);

This creates a keyspace named myKeyspace and a table named myTable.

Conclusion

In this tutorial, you learned how to set up MongoDB and Cassandra, two popular NoSQL databases. You explored the installation process, starting the database server, and creating databases or keyspaces. NoSQL databases are powerful tools for handling large volumes of unstructured data and can be tailored to specific application needs.