Skip to content

MongoDB

MongoDB is a document store that saves data in a tree-like structure. Data is represented using a JSON-style format.

Key Features

  • Designed to support large volumes of data
  • Supports distributing data across multiple servers (horizontal scaling)
  • Widely used in large-scale web applications and big data scenarios

Document Structure

A MongoDB document can be nested — a property can contain one or more additional levels of properties. For example, a preferences field can hold an object with multiple settings, and a roles field can contain a list of values.

json
{
  "_id": ObjectId("63e362b9415552734e896049"),
  "username": "john.doe",
  "email": "john@example.com",
  "preferences": {
    "theme": "dark",
    "language": "en"
  },
  "roles": ["user", "editor"]
}

Design Principles

MongoDB stores all information about a single entity in one document. This means you don't create separate "tables" for related data like settings or roles — everything directly related to the entity lives in the same document.

Example

In an online store, a product document would also contain its reviews and other directly related data. This reduces the number of queries needed to fetch complete information.

Separate entities should stay separate

Clearly distinct entities should not be in the same document. For example, a product and a customer, or a product and an order, should each be their own document.


Installation

MongoDB Community Server

Download from: https://www.mongodb.com/try/download/community

Also install MongoDB Compass if it's not included in the installer — it's a GUI tool for working with your database.

Connecting with MongoDB Compass

Use the following connection string to connect to a local MongoDB server:

mongodb://localhost:27017

Existing databases are listed on the left side. You can create a new database by clicking the + button in Compass. Note that MongoDB requires you to create a collection at the same time as the database.

A collection is a group of related documents — similar to a table in a relational database.

Importing CSV Data with Compass

Compass has a built-in import feature for CSV and JSON files. The following example uses HSL city bike trip data.

Sample Data

Download the HSL city bike trip dataset: od-trips-2025.zip

The data is sourced from Helsinki Region Infoshare (HRI) and contains trips made with Helsinki and Espoo city bikes.

Extract the ZIP file — it contains CSV files with columns such as Departure, Return, Departure station name, Return station name, Covered distance (m), and Duration (sec.).

Import Steps

  1. Connect to your MongoDB server in Compass
  2. Create a new database by clicking the + button (e.g., bikedata) and name the collection trips
  3. Select the trips collection
  4. Click Add Data > Import JSON or CSV file
  5. Select one of the extracted CSV files
  6. Compass shows a preview of the data — check the detected field types and adjust if needed (e.g., ensure Covered distance (m) and Duration (sec.) are set to Number)
  7. Click Import to insert the data

TIP

You can repeat the import with additional CSV files. New documents are added to the existing collection without removing previous data.

Cleaning the data

The data seems to have incorrectly nested duration data afer the import, this is how to fix it. This adds duration field that has the duration value directly without nesting.

js
db.trips.updateMany(
  {},
  [{
    $set: {
      "duration": {
        $getField: {
          field: ")",
          input: {
            $getField: {
              field: "Duration (sec",
              input: "$$ROOT"
            }
          }
        }
      }
    }
  }]
)

Dropping the original malformed field

js
db.trips.updateMany(
  { },
  { $unset: { "Duration (sec": ""  } }
)

Further Reading

Lapland University of Applied Sciences

© 2026 Juha Petäjäjärvi

© 2026 Juha Petäjäjärvi