Appearance
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.
- Compass download: https://www.mongodb.com/products/compass
Connecting with MongoDB Compass
Use the following connection string to connect to a local MongoDB server:
mongodb://localhost:27017Existing 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
- Connect to your MongoDB server in Compass
- Create a new database by clicking the + button (e.g.,
bikedata) and name the collectiontrips - Select the
tripscollection - Click Add Data > Import JSON or CSV file
- Select one of the extracted CSV files
- Compass shows a preview of the data — check the detected field types and adjust if needed (e.g., ensure
Covered distance (m)andDuration (sec.)are set to Number) - 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": "" } }
)