Getting Started with MongoDB: Your First Project

The Tech Bro
3 min readFeb 7, 2024

--

Step 1: Install MongoDB

Before you start your MongoDB project, you need to install MongoDB on your machine. Follow the official installation guide for your operating system: MongoDB Installation Guide.

Step 2: Set Up a MongoDB Database

2.1 Start MongoDB Server

Once installed, start the MongoDB server. Open a terminal or command prompt and run:

mongod

This command starts the MongoDB server.

2.2 Connect to MongoDB

Open a new terminal or command prompt and run:

mongo

This will open the MongoDB shell, allowing you to interact with your MongoDB server.

2.3 Create a Database

In the MongoDB shell, create a new database:

use myfirstdb

Replace “myfirstdb” with the name you want for your database.

Step 3: Set Up Your Project

3.1 Install MongoDB Driver for Your Programming Language

Choose a programming language for your project and install the MongoDB driver.

JavaScript (Node.js)

Install the official MongoDB Node.js driver using npm:

npm install mongodb

3.2 Create a Connection to MongoDB

In your project file, establish a connection to the MongoDB database:

// JavaScript example (Node.js)
const { MongoClient } = require('mongodb');

const url = 'mongodb://localhost:27017';
const client = new MongoClient(uri, { useNewUrlParser: true,
useUnifiedTopology: true });

async function connectToDatabase() {
try {
await client.connect();
console.log('Connected to the database');
} catch (error) {
console.error('Error connecting to the database', error);
}
}

connectToDatabase();

Replace the url with your MongoDB connection string.

Step 4: Perform Basic Collection Operations

4.1 Create a Collection

Create a collection within your database:

// Creating a collection
async function createCollection() {
const database = client.db('myfirstdb');
const collectionName = 'mycollection';

try {
await database.createCollection(collectionName);
console.log('Collection created:', collectionName);
} catch (error) {
console.error('Error creating collection', error);
}
}

createCollection();

4.2 Insert Data into the Collection

Add data to your MongoDB collection:

// Inserting data into the collection
async function insertData() {
const database = client.db('myfirstdb');
const collection = database.collection('mycollection');

const data = { name: 'John Doe', age: 25, city: 'New York' };

try {
const result = await collection.insertOne(data);
console.log('Data inserted:', result.insertedId);
} catch (error) {
console.error('Error inserting data', error);
}
}

insertData();

4.3 Query Data from the Collection

Retrieve data from your MongoDB collection:

// Querying data from the collection
async function queryData() {
const database = client.db('myfirstdb');
const collection = database.collection('mycollection');

try {
const result = await collection.find({}).toArray();
console.log('Queried data:', result);
} catch (error) {
console.error('Error querying data', error);
}
}

queryData();

Step 5: Close the Connection

Always close the connection when you’re done with your MongoDB operations:

// Closing the connection
async function closeConnection() {
try {
await client.close();
console.log('Connection closed');
} catch (error) {
console.error('Error closing the connection', error);
}
}

// Call this function when you're done with MongoDB operations
closeConnection();

Congratulations! You’ve successfully created your first MongoDB project with a collection. Customize and expand upon this foundation as you continue to explore MongoDB and build more advanced applications.

--

--