Skip to main content
Version: v3.x

Get Started with Hasura DDN and MongoDB

Prerequisites

Install the DDN CLI

Simply run the installer script in your terminal:

curl -L https://graphql-engine-cdn.hasura.io/ddn/cli/v4/get.sh | bash

Install Docker

The Docker based workflow helps you iterate and develop locally without deploying any changes to Hasura DDN, making the development experience faster and your feedback loops shorter. You'll need Docker Compose v2.27.1 or later.

Validate the installation

You can verify that the DDN CLI is installed correctly by running:

ddn doctor

Guide

In this guide, you'll learn how to get started with Hasura DDN and a MongoDB database. You'll set up a new Hasura DDN project, connect it to a new MongoDB database, and run your first query. This guide assumes you're starting from scratch, but you can easily follow the steps if you already have data seeded; Hasura will never modify your source schema.

Step 1. Create a collection in your MongoDB database

First, you'll create a new collection in your MongoDB database:
// Create the collection and insert some data
db.createCollection("users");
db.users.insertMany([
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 35 },
]);
Connect to cloud local data sources

Hasura DDN can connect to hosted or local data sources. If you have a cloud-hosted MongoDB provider, you can connect to your database and seed the table with the above data. Alternatively, you can run MongoDB locally using Docker.

Step 2. Authenticate your CLI

Before you can create a new Hasura DDN project, you need to authenticate your CLI:
ddn auth login

Step 3. Scaffold out a new local project

Next, you'll create a new local project. Run the following command:
ddn supergraph init my-project && cd my-project

Step 4. Initialize your MongoDB connector

Initialize a new MongoDB connector. Run the following command:
ddn connector init my_mongo -i

From the dropdown, start typing MongoDB and hit enter.

Set your environment variables:

VariableValueRequired
MONGODB_DATABASE_URIThe connection string to your source in this format: mongodb://username:password@hostname:port/dbnameYes
Connecting a local database?

Hasura DDN uses a unique domain to resolve local services when using Docker. In the connection string above, you can replace hostname with local.hasura.dev if you're connecting to a local database running inside of a Docker container.

Step 5. Introspect your MongoDB database

Next, you'll introspect your MongoDB database:
ddn connector introspect my_mongo

Step 6. Add your model

Now, you'll track the collections as models in your MongoDB database:
ddn models add my_mongo users
Have multiple collections?

You can quickly grab them all using:

ddn models add my_mongo "*"

Step 7. Create a new build

Next, you'll create a new build:
ddn supergraph build local

Step 8. Start your local services

Start your local Hasura DDN Engine and MongoDB connector:
ddn run docker-start

Step 9. Run your first query

Open your local console:
ddn console --local
And, if you write a query like this:
query {
users {
name
age
}
}
You'll get a response like this:
{
"data": {
"users": [
{
"name": "Alice",
"age": 25
},
{
"name": "Bob",
"age": 30
},
{
"name": "Charlie",
"age": 35
}
]
}
}

Next steps

Congratulations! You've successfully set up a Hasura DDN project and connected it to a MongoDB database. Next, you can explore more advanced features like relationships, permissions, and more.

Take a look at our MongoDB docs to learn more about how to use Hasura DDN with MongoDB.