Getting Started with MongoDB Atlas Local Search Experience Using Docker
Rate this tutorial
This tutorial will walk you through setting up MongoDB Atlas Local Search with Docker, populating a local database with sample data, and configuring a search index. This is ideal for developers looking to streamline their search-driven application workflows, test full-text or AI-powered search locally, and improve their productivity by working with rapid, low-latency feedback loops directly in their preferred environment.
Before diving into Docker-based local setups, it's worth noting that the MongoDB Atlas CLI offers a streamlined approach to managing MongoDB Atlas deployments. This command-line tool simplifies tasks such as creating clusters, managing indexes, and configuring databases, allowing developers to handle these operations efficiently from the terminal.
The Atlas CLI also enables the creation and management of local environments, which are delivered using Docker. For those who prefer hands-on control over local environments, this tutorial leverages Docker directly to demonstrate the flexibility and power of MongoDB Atlas Local Search, enabling rapid prototyping and local experimentation.
Modern application development increasingly demands advanced search-driven features that combine flexible indexing, high performance, and rapid iteration. MongoDB Atlas Local Search is designed to meet these needs by providing developers with robust tools that emphasize developer ergonomics and fast feedback loops, essential for efficient and effective application prototyping.
MongoDB Atlas Local Search streamlines the development process by enabling seamless integration with your preferred local environment. This eliminates the friction often encountered when switching between local development and remote systems. Developers can configure, query, and test search features directly within their familiar tools, benefiting from MongoDB’s flexible schema model and powerful query capabilities. Moreover, the local setup allows for immediate adjustments to index configurations and search parameters, reducing the overhead associated with remote deployment and iteration. With Docker-based setups, the environment is easily reproducible, ensuring consistent development experiences across teams and projects.
Speed is critical during the development and testing phases, and MongoDB Atlas Local Search delivers unparalleled efficiency in this regard. Local clusters are exceptionally fast to deploy and dismantle, allowing developers to quickly spin up instances for testing new features or debugging. The absence of network latency ensures near-instantaneous read and write operations, enabling developers to test changes in real-time. Whether experimenting with full-text search queries, vector-based search for AI-driven applications, or refining index configurations, the rapid feedback cycle accelerates development workflows and minimizes downtime. This agility fosters an iterative process where developers can make incremental improvements with confidence, significantly enhancing overall productivity.
By combining these advantages, MongoDB Atlas Local Search provides a powerful solution for building modern search-driven applications and empowers to focus on innovation and functionality without being hindered by the complexities of remote infrastructure or slow feedback cycles.
By combining these advantages, MongoDB Atlas Local Search provides a powerful solution for building modern search-driven applications and empowers to focus on innovation and functionality without being hindered by the complexities of remote infrastructure or slow feedback cycles.
Before starting, ensure you have the following:
- Docker installed
- A basic understanding of MongoDB and Docker
- MongoDB Compass installed to follow along with the examples
- Mongosh installed
The repository includes Docker configurations, sample data, and example code to simplify getting started with MongoDB Atlas Local Search.
Follow these step-by-step instructions to get started:
Step #1: Pull the MongoDB Atlas Local Docker Image
Run the following command to download the latest MongoDB Atlas Local Docker image:
1 docker pull mongodb/mongodb-atlas-local:latest
If you run
docker pull mongodb/mongodb-atlas-local
without specifying a version tag, Docker automatically retrieves the latest version of the image (mongodb/mongodb-atlas-local:latest
).To pull a specific version of the Docker image, use this command, replacing
<tag>
with the desired version tag:1 docker pull mongodb/mongodb-atlas-local:<tag>
Step #2: Run the Database
Start the MongoDB container:
1 docker run -p 27017:27017 mongodb/mongodb-atlas-local
Step #3: Connect to the Database
Open a second terminal (since the MongoDB container is running in foreground mode) and connect using
mongosh
. For this basic example, authentication is not enabled:1 mongosh "mongodb://localhost/?directConnection=true"
Congratulations! You now have a fully functional local Atlas deployment with search capabilities.
To verify the setup, let’s create a search index and test it.
Step #1: Insert Sample Data
Use the following commands to insert some sample data into a collection:
1 use test 2 db.sampleAtlasSearch.insertMany([ 3 { item: "card", qty: 15 }, 4 { item: "envelope", qty: 20 }, 5 { item: "stamps", qty: 30 } 6 ]);
Step #2: Create a Search Index
Create a simple dynamic search index:
1 db.sampleAtlasSearch.createSearchIndex({ mappings: { dynamic: true } });
Step #3: Verify the Index
Check that the search index was created successfully:
1 db.sampleAtlasSearch.getSearchIndexes();
Voilà! Your first search index in a local Atlas deployment:
Now that you’ve covered the basics, let’s explore how MongoDB Atlas Local Search can add value to your day-to-day work.
Next, we will:
Next, we will:
- Set up a Local Atlas Deployment with Docker Compose
- Learn how to seed data
- Create and run search indexes
To make this tutorial convenient to follow and help you get started with MongoDB Atlas Local Search, I have prepared a repository containing everything you need, including Docker configuration, sample data, and example code. To follow along, please clone the MongoDB Local Atlas repository.
Let’s take a look at the
docker-compose.yml
file to understand how the environment is configured:1 services: 2 mongodb: 3 image: mongodb/mongodb-atlas-local 4 environment: 5 - MONGODB_INITDB_ROOT_USERNAME=user 6 - MONGODB_INITDB_ROOT_PASSWORD=pass 7 - RUNNER_LOG_FILE=/dev/stderr 8 ports: 9 - 27018:27017 10 volumes: 11 - ./init:/docker-entrypoint-initdb.d 12 - ${PWD}/upload/restaurants.json:/tmp/restaurants.json
- Service Definition
Themongodb
service uses themongodb/mongodb-atlas-local
image, which includes MongoDB server, Atlas Search, and Vector Search. You can specify a MongoDB version by appending a version tag to the image name, e.g.,mongodb/mongodb-atlas-local:8.0
. For available versions, refer to the MongoDB repository on Docker Hub. - Environment Variables
MONGODB_INITDB_ROOT_USERNAME
andMONGODB_INITDB_ROOT_PASSWORD
define the default credentials for the root user.RUNNER_LOG_FILE
is optional but useful for logging errors during initialization to the console.
- Port Mapping
The database is exposed to the local host on port27018
. - Volumes for Seeding
- Initialization scripts for seeding the database are loaded from the
./init
directory, mounted as/docker-entrypoint-initdb.d
inside the container. - The sample data file
restaurants.json
is mounted to/tmp/restaurants.json
inside the container.
- Health Check
A health check is not required, as it is built into themongodb-atlas-local
image.
To start the deployment, run the following command:
1 docker compose up
- The local environment is created, including the core database and Atlas Search functionality.
- Initialization scripts for database seeding are executed in alphanumeric order from the
./init
directory.
You should see that the
local-atlas-mongodb
container has started:To verify, run:
1 docker container list
This will display a list of running containers and their statuses:
As mentioned earlier, initialization scripts are loaded from the
./init
directory and executed in alphanumeric order inside the container's /docker-entrypoint-initdb.d
.Here’s how the seeding process works in this example:
Step #1: 00-import-from-export.sh
This script uses
mongoimport
to load data exported from another MongoDB database/collection. This is ideal for replicating a reference installation for development or testing. The file must be accessible in the container through the mounted volume. Alternatively, you can download the file directly into the container using curl
and then import it:1 curl -o /tmp/mydatafile.json https://data.somewhere/mydatafile.json 2 mongoimport --uri $CONNECTION_STRING --collection=restaurants --db=myplaces --file=/tmp/mydatafile.json
Step #2: 01-import-explicit-inserts.js
This script seeds data by executing
insertMany
commands in the MongoDB shell. For example:1 db.getSiblingDB('myplaces').getCollection('restaurantsExtra').insertMany([ 2 { /* data */ }, 3 { /* data */ } 4 ]);
Step #3: 02-create-search-index.js
This script creates a search index on the
name
field of the restaurants
collection. Example:1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "name": { 6 "type": "string", 7 "analyzer": "lucene.standard" 8 } 9 } 10 } 11 }
With the database running, you can connect to it using various tools:
MongoDB Shell
Connect using the following command:
1 mongosh "mongodb://user:pass@localhost:27018/?directConnection=true"
MongoDB Compass
Add the connection string in Compass to connect:
Data Verification
00-import-from-export.sh
should populate themyplaces.restaurants
collection.01-import-explicit-inserts.js
should add data tomyplaces.restaurantsExtra
.
Both collections should be visible in Compass:
Search Index
02-create-search-index.js
should create a search index on the name
field of the restaurants
collection. You can verify the index in Compass:Now, let’s run an Atlas Search query using MongoDB Compass. This query performs a fuzzy search on the
name
field of the restaurants
collection and limits the results to the top three.1 [ 2 { 3 "$search": { 4 "index": "default", 5 "text": { 6 "query": "Food", 7 "path": "name", 8 "fuzzy": { "maxEdits": 2 } 9 } 10 } 11 }, 12 { "$limit": 3 }, 13 { 14 "$project": { 15 "_id": 0, 16 "name": 1, 17 "cuisine": 1 18 } 19 } 20 ]
If you have an existing Atlas implementation using the official
mongo
Docker image, refer to this guide for a checklist to transition to the mongodb-atlas-local
image.This tutorial has provided a comprehensive introduction to setting up and leveraging MongoDB Atlas Local Search within a Dockerized environment. By following the steps outlined, you have configured a local deployment, inserted sample data, created dynamic search indexes, and tested the setup through Atlas Search queries. These processes empower you to iterate rapidly, optimize workflows, and build robust search-driven applications with minimal overhead.
The integration of MongoDB Atlas Local Search with Docker enhances developer productivity by enabling a seamless local testing environment that mirrors production-like capabilities. This streamlined approach eliminates the latency and complexity often associated with remote environments, allowing for precise configuration and real-time experimentation. The Docker Compose configurations and initialization scripts further simplify team collaboration, ensuring consistent environments across diverse systems.
As you continue to explore MongoDB Atlas Local Search, consider how its capabilities—like full-text search, AI-driven vector searches, and dynamic indexing—can enhance your application’s functionality and user experience. By incorporating advanced search features locally, you can focus on refining your application’s core logic, optimizing search performance, and delivering value faster.
The integration of MongoDB Atlas Local Search with Docker enhances developer productivity by enabling a seamless local testing environment that mirrors production-like capabilities. This streamlined approach eliminates the latency and complexity often associated with remote environments, allowing for precise configuration and real-time experimentation. The Docker Compose configurations and initialization scripts further simplify team collaboration, ensuring consistent environments across diverse systems.
As you continue to explore MongoDB Atlas Local Search, consider how its capabilities—like full-text search, AI-driven vector searches, and dynamic indexing—can enhance your application’s functionality and user experience. By incorporating advanced search features locally, you can focus on refining your application’s core logic, optimizing search performance, and delivering value faster.
Top Comments in Forums
There are no comments on this article yet.