Skip to content
  • Our Product
    • Namazu Elements
      • What is Elements?
      • Why open source?
      • Docs
        • Elements in Five Minutes or Less
        • RESTful APIs Library
        • Security Model
        • Accessing the Web UI (CMS)

    Our Product

    A logomark with three layered rhombuses adorning the lettermark that says Elements in bold all-caps sans-serif letters.
    • What is Namazu Elements? Discover our easy-to-use backend network solution built for online games. Rapidly enables full-scale multiplayer games or online solo adventures.
    • Why open source? Is there a truly open source server backend for connected games? There is now. Download and run a local copy of Namazu Elements and try it for yourself.
    Download Namazu Elements

    Get started

    • Quick start Read our Elements 5-minute quick start guide
    • Documentation Read our developer docs for learning more about Elements
    • RESTful APIs A full list of core API specs for working with the Elements framework
    • Security An overview of the server-authoritative security model of Elements
    • Accessing the CMS Manage your game with ease via the Namazu Elements CMS.

    Co-development Reimagined

    • Reduce your costs Would you rather outsource your backend development? Hire Namazu Studios to build your server backend with the power of Namazu Elements.
      Co-dev

    Recent Posts

    • The watercolor-styled Namazu Studios logo over a giant namazu lurking in the depth
      Namazu Studios Featured in San Diego Business Journal
      22 Sep 2025 Press
    • Namazu Elements 3.1 Released – Service Layer Fixes, Secure APIs, and Steam Bug Fix
      22 Apr 2025 Release Notes
  • Case Studies
  • About Us
  • News
  • Services
  • Book a call
namazu-studios-logo
Book a call

Getting Started

  • Elements in Five Minutes or Less
  • 🐧Ubuntu Linux Setup
  • 🍎 Mac OS Setup
  • 🖥️ Setup for Windows
  • Accessing the Web UI (CMS)
  • General Concepts
  • N-Tier Architecture
  • Security Model

Namazu Elements Core

  • User Authentication / Sign In
    • What is a User?
    • User Authentication in Elements
    • Auth Schemes
      • Auth Schemes
      • OAuth2
      • OIDC
  • Features
    • Applications
    • Sessions
    • Users and Profiles
    • Digital Goods
    • Progress and Missions
    • Progress and Missions (3.4+)
    • Leaderboards
    • Matchmaking – Comprehensive Guide
    • Followers
    • Friends
    • Reward Issuance
    • Save Data
    • Metadata
    • Metadata (3.4+)
    • Queries
    • Web3
      • Wallets
      • Vaults
      • Omni Chain Support
      • Smart Contracts
        • Smart Contracts
  • Queries
    • Advanced Operators
    • Object Graph Navigation
    • Boolean Queries
    • Base Query Syntax
  • Advanced Operators
    • .name
    • .ref

Custom Code

  • Custom Code Overview
  • Introduction to Guice and Jakarta in Elements
  • Structuring your Element
  • Packaging an Element with Maven
  • Deploying an Element
  • Preparing for Code Generation
  • Properties
  • Websockets
  • RESTful APIs
  • Direct MongoDB Access (3.5+)

Releases

  • 3.4 Release Notes
  • 3.3 Release Notes
  • 3.2 Release Notes
  • 3.1 Release Notes

Configuration

  • Matchmaking – Comprehensive Guide
  • Direct Database Access and Batch Configuration
  • Batch Samples
    • Mission Upload Bash Script Sample
    • Item Upload Bash Script Sample

RESTful APIs

  • RESTful APIs Library
  • Swagger and Swagger UI

Crossplay

  • Namazu Crossfire
  • Deploying Namazu Crossfire in Your Game
View Categories
  • Home
  • Docs
  • Custom Code
  • Direct MongoDB Access (3.5+)

Direct MongoDB Access (3.5+)

Est. read time: 4 min read

This document describes how to connect to the MongoDB instance provided by Namazu Elements. It explains the configuration flow, how SSL is handled, and how an individual Element can establish a connection using the MongoDB driver of its choice.

This is availble in Namazu Elements 3.5 and above.

📖 Using MongoDB Drivers

This guide only documents how to set up the connection to MongoDB. Making use of the drivers is beyond the scope of this document. To understand more about the drivers and their usages, please refer to the following from MongoDB’s official Documentation:

  • MongoDB Synchronous Driver
  • MongoDB Reactive Streams Driver
  • Hibernate ORM Extension for MongoDB
  • Morhpia

Overview #

Namazu Elements exposes a service named MongoConfigurationService that other Elements can use to retrieve the connection details for MongoDB. This service reads configuration from the application’s configuration layer and returns an instance of MongoConfiguration.

Each Element then uses its preferred MongoDB driver and the retrieved configuration to establish a secure connection to the shared MongoDB instance.

MongoConfigurationService #

MongoConfigurationService is the main entry point for obtaining your MongoDB configuration. Its responsibilities include:

  • Reading application configuration settings that define how MongoDB should be accessed.
  • Producing an initialized instance of MongoConfiguration.

Elements should request this service through the usual dependency injection or service lookup mechanisms used within Namazu Elements. Built in to Namazu Elements is a type

MongoConfiguration #

The MongoConfiguration object returned by the service contains the information needed to connect to MongoDB.

Connection String #

MongoConfiguration always includes a connection string. This connection string can include additional driver settings such as replica set information, authentication data, and flags for TLS.

Example:

mongodb+srv://example.mongodb.net/?retryWrites=true&w=majority

SSL Context #

If SSL is configured for your application, MongoConfiguration will also include a javax.net.ssl.SslContext. This SSL context is prepared with the required trust manager and key manager settings.

This means the Element does not need to manually configure certificates, keystores, or truststores. All SSL requirements for talking to the MongoDB server are handled for you.

If SSL is not configured, the SSL context will be absent.

Connecting Using the MongoDB Java Driver #

Elements can use any MongoDB driver they choose. This section demonstrates how to connect using the MongoDB Java Driver.

Below is a reference implementation illustrating how to:

  • Extract the connection string.
  • Build the SSL configuration.
  • Apply both to the client settings.
  • Create a MongoClient and verify the connection.

Example Code #

public MongoClient connect() {

    final ElementRegistry registry = /* fetch registry */;


    final MongoConfigurationService mongoConfigurationService = registry
        .find("dev.getelements.elements.sdk.mongo")
        .findFirst()
        .get()
        .getServiceLocator()
        .getInstance(MongoConfigurationService.class);


    final MongoConfiguration conf = 
        mongoConfigurationService.getMongoConfiguration();


    final ConnectionString connectionString = new ConnectionString(conf.connectionString());

    final SslSettings sslSettings = conf
        .findSslConfiguration()
        .map(MongoSslConfiguration::newSslContext)
        .map(sslContext -> SslSettings.builder()
            .enabled(true)
            .context(sslContext)
        )
        .orElseGet(() -> SslSettings.builder().applyConnectionString(connectionString))
        .build();


    final MongoClientSettings clientSettings = MongoClientSettings
        .builder()
        .applyConnectionString(connectionString)
        .applyToSslSettings(builder -> builder.applySettings(sslSettings))
        .build();


    return MongoClients.create(clientSettings);

}

How the Example Works #

  1. Connection String: Retrieved directly from the configuration.
  2. SSL Configuration: If present, an SSL context is created and injected into SslSettings.
  3. Fallback Behavior: If SSL is not configured, the SSL settings fall back to whatever is specified in the connection string.
  4. Client Settings: Both the connection string and SSL settings are applied to MongoClientSettings.
  5. Client Lifecycle: A MongoClient is created, used, and automatically closed via the try-with-resources block.
  6. Verification: The cluster description is logged to confirm that the connection was successful.

Using Guice Injection #

Elements that use Guice for dependency injection can simplify access to the MongoConfigurationService. Namazu Elements exposes its services using standard annotations.

package-info.java #

@ElementPublic
@ElementDependency("dev.getelements.elements.sdk.mongo")
package dev.getelements.elements.sdk.mongo;


import dev.getelements.elements.sdk.annotation.ElementDependency;
import dev.getelements.elements.sdk.annotation.ElementPublic;

Injecting the Service in Your Game Code #

You can inject the MongoConfigurationService directly into your Element or game service.

@Inject
public void setMongoConfigurationService(MongoConfigurationService service) {
    this.service = service;
}

Once injected, you can build your Mongo client as shown earlier by constructing the connection settings from the injected service.

Summary #

Connecting to MongoDB through Namazu Elements involves three main steps:

  1. Retrieve a MongoConfiguration instance using MongoConfigurationService.
  2. Extract the connection string and optional SSL context.
  3. Use your chosen MongoDB driver to configure and initialize a client.

Namazu Elements handles most of the complexity for you, especially around SSL, so Elements can focus on application logic while maintaining secure and consistent database access.

What are your Feelings
Still stuck? How can we help?

How can we help?

Updated on November 15, 2025
RESTful APIs
Table of Contents
  • Overview
  • MongoConfigurationService
  • MongoConfiguration
    • Connection String
    • SSL Context
  • Connecting Using the MongoDB Java Driver
    • Example Code
    • How the Example Works
  • Using Guice Injection
    • package-info.java
    • Injecting the Service in Your Game Code
  • Summary
  • Documentation
  • Terms of Service
  • Privacy Policy
  • Contact us
  • Linkedin
  • Join our Discord

Namazu Studios LLC is powered by Namazu Elements, an Elemental Computing Inc. product.

Elements
  • Download
  • About Elements
  • Open source
  • Documentation
  • Support
About Namazu
  • Case Studies
  • About Us
  • News
Get in Touch
  • info@namazustudios.com
  • Book a call
  • (619) 862-2890
  • Linkedin
  • Discord

©2008-2025 Namazu Studios. All Rights Reserved.