Get Started with Hasura DDN and MongoDB
Prerequisites
Install the DDN CLI
- macOS and Linux
- Windows
Simply run the installer script in your terminal:
curl -L https://graphql-engine-cdn.hasura.io/ddn/cli/v4/get.sh | bash
- Download the latest DDN CLI installer for Windows.
- Run the
DDN_CLI_Setup.exe
installer file and follow the instructions. This will only take a minute. - By default, the DDN CLI is installed under
C:\Users\{Username}\AppData\Local\Programs\DDN_CLI
- The DDN CLI is added to your
%PATH%
environment variable so that you can use theddn
command from your terminal.
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
// 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 },
]);
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
ddn auth login
Step 3. Scaffold out a new local project
ddn supergraph init my-project && cd my-project
Step 4. Initialize your MongoDB connector
ddn connector init my_mongo -i
From the dropdown, start typing MongoDB
and hit enter.
Set your environment variables:
Variable | Value | Required |
---|---|---|
MONGODB_DATABASE_URI | The connection string to your source in this format: mongodb://username:password@hostname:port/dbname | Yes |
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
ddn connector introspect my_mongo
Step 6. Add your model
ddn models add my_mongo users
You can quickly grab them all using:
ddn models add my_mongo "*"
Step 7. Create a new build
ddn supergraph build local
Step 8. Start your local services
ddn run docker-start
Step 9. Run your first query
ddn console --local
query {
users {
name
age
}
}
{
"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.