Skip to main content
MongoDB is a popular document-oriented NoSQL database that stores data in flexible, JSON-like documents. This guide covers installing the Countries States Cities database in MongoDB using the provided BSON dump files.
MongoDB installation typically takes 2-4 minutes and requires approximately 180MB of disk space.

Prerequisites

Before starting, ensure you have:
  • MongoDB 4.0+ installed (MongoDB 5.0+ recommended)
  • MongoDB server running on your system
  • At least 300MB free disk space
  • Downloaded the mongodb-dump/world-mongodb-dump.tar.gz file from our GitHub repository
MongoDB uses BSON (Binary JSON) format rather than SQL, so the installation process differs from relational databases.

Installation Steps

1

Extract MongoDB Archive

This extracts the MongoDB dump files to a mongodb-dump/world directory containing:
  • regions.bson and regions.metadata.json
  • countries.bson and countries.metadata.json
  • states.bson and states.metadata.json
  • cities.bson and cities.metadata.json
2

Restore Database

Adjust the host and port parameters according to your MongoDB configuration. Default is usually localhost:27017.
3

Verify Installation

Expected output:

Alternative Installation Methods

Using MongoDB Compass

1

Connect to Database

Open MongoDB Compass and connect to your MongoDB server.
2

Create Database

  • Click “Create Database”
  • Database Name: “world”
  • Collection Name: “countries” (initial collection)
3

Import Collections

For each collection (regions, countries, states, cities):
  • Select the collection
  • Click “ADD DATA” → “Import File”
  • Choose the corresponding .json file
  • Select JSON format and import

Using Docker

1

Run MongoDB Container

2

Copy Dump Files

3

Restore Inside Container

Database Structure

MongoDB stores the data in four main collections:

MongoDB-Specific Operations

Basic Queries

Create Indexes

1

Basic Indexes

2

Geospatial Indexes

3

Text Search Indexes

Connection Examples

Advanced Features

Geospatial Queries

Data Validation

Troubleshooting

Problem: MongoNetworkError: connect ECONNREFUSEDSolutions:
  1. Check if MongoDB service is running: sudo systemctl status mongod
  2. Start MongoDB: sudo systemctl start mongod
  3. Check MongoDB port: netstat -an | grep 27017
  4. Verify connection string format
  5. Check firewall settings
Problem: MongoServerError: Authentication failedSolutions:
  1. Check username/password in connection string
  2. Verify user exists: db.getUsers()
  3. Check user roles: db.getUser("username")
  4. Create user with proper permissions:
Problem: mongorestore fails with various errorsSolutions:
  1. Check MongoDB version compatibility
  2. Ensure sufficient disk space
  3. Verify dump file integrity: tar -tzf world-mongodb-dump.tar.gz
  4. Try importing individual collections:
  5. Use --drop flag to overwrite existing data
Problem: Queries are running slowlySolutions:
  1. Create appropriate indexes (see Performance section)
  2. Use explain() to analyze query performance
  3. Optimize aggregation pipelines
  4. Increase MongoDB cache size
  5. Consider sharding for very large datasets

Backup and Maintenance

Backup Strategies

Maintenance Tasks

MongoDB automatically manages most maintenance tasks, but periodic index optimization and backup verification are recommended.

Next Steps

After successful installation:
  1. Create appropriate indexes for your query patterns
  2. Set up replica sets for high availability
  3. Configure sharding if handling very large datasets
  4. Implement monitoring with MongoDB Compass or third-party tools
  5. Set up automated backups using the provided scripts

Need Help?

Join our community discussions for MongoDB-specific questions and NoSQL optimization tips.