# Connecting Locally to MongoDB

You can run your Empirica experiment locally, whilst still connecting it to a cloud MongoDB database.

To do so, you need to [create the database and obtain the URI](/guides/deploying-my-experiment/database.md).

## Via the settings file

As explained in the [guide for deploying to MongoDB](/guides/deploying-my-experiment/database.md#how-do-i-connect-my-app-to-mongodb-atlas), you will need to add these elements to your settings file:

```
    "galaxy.meteor.com": {
        "env": {
            "MONGO_URL": "mongodb+srv://<read&write username>:<read&write password>@<connection>/<database name>?retryWrites=true&w=majority",
            "MONGO_OPLOG_URL": "mongodb+srv://<oplog username>:<oplog password>@<connection>/local"
        }
    },
```

Fill in the <> parts with the appropriate elements.

Now you can run your app with:

```
meteor --settings <name of settings file>
```

{% hint style="info" %}
This might not always work for connecting locally to your MongoDB. The method below is more reliable.
{% endhint %}

## Via the command line

According to your OS, follow the instructions below to connect to your MongoDB via the command line.

{% tabs %}
{% tab title="Mac, Linux, Windows with WSL" %}
Run this command with the URI you have obtained but without the `?retryWrites=true&w=majority`

```
MONGO_URL=<MongoDB URI> meteor
```

{% endtab %}

{% tab title="Windows without WSL" %}
Create a `.bat` file (e.g., `start.bat`). **These files allow you to run multiple commands on Windows. Do not run commands you are not comfortable with or not convinced they are secure.**

Make sure that you have `*.bat` in your `.gitignore` because this file will contain sensitive information (your access to the database) and you don't want it to be sent to your repository.\
\
Add the following commands to the file with the URI you have obtained but without the `?retryWrites=true&w=majority`:

```
SET MONGO_URL=<MongoDB URI>
meteor
```

Run the file in the command line (e.g., `.\start.bat` ).
{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docsv1.empirica.ly/guides/the-settings-file/connecting-locally-to-mongodb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
