Page cover

MongoDB

MongoDB is the most commonly used NoSQL database. Here is everything you need to know to connect it to any of your other systems using Stacksync.

Prerequisites

circle-check
circle-info

We strongly suggest using MongoDB's built-in readarrow-up-right or readWritearrow-up-right roles. For more information on how to createarrow-up-right and grant rolesarrow-up-right to users, check out MongoDB's official documentationarrow-up-right.

// Grant user roles without overwriting existing ones (specific DBs)
use admin

const user = db.getUser("<user>");  // Change to your username

const updatedRoles = user.roles.concat([
        { role: "read", db: "local" }, // required to read the oplog and therefore read changes from mongodb
        { role: "readWrite", db: "<db1>" }, // when need to read and write to mongodb
        { role: "read", db: "<db2>" }, // when do not need to write to mongodb
        // Add the database(s) you want Stacksync to have access to
      ]);

db.updateUser("<user>", { roles: updatedRoles });
circle-exclamation

If a collection you see in your database does not appear in Stacksync, or changes are not correctly replicated, one of the previous conditions is not met. Please double-check your setup in that case.

If you have any questions, don't hesitate to contact us at [email protected]envelope.

Connecting your MongoDB instance

To connect your MongoDB database to Stacksync, you'll need the following data:

circle-check
  • Protocol: must be mongodb+srv:// or mongodb://.

  • Host: the URI or IP where the server can be found.

  • Port (optional): the default is usually 27017.

  • Authentication method

    • Username: name used to authenticate with the server.

    • Password: the password used to authenticate with the server.

    or

  • Database: the name of the database where data is present.

  • Connection options:

circle-info

Other connection options are not currently supported. If you need special assistance, please contact us at [email protected]envelope.

  • SSH Tunnel:

    • Username: The user Stacksync can identify as to establish the tunnel.

    • Host: IP of the machine to SSH into.

    • Public Key: Copy into your machine's ~/.ssh/authorized_keys file. You can use ssh-copy-idarrow-up-right or simply SSH into the server and paste it using your preferred text editor.

circle-info

SSH tunneling is currently only supported for single, self-hosted MongoDB instances via the mongodb:// protocol. For any further questions, please contact us at [email protected]envelope.

The connection setup screen for user+password
The connection setup screen for TLS/SSL

Connection strings

MongoDB has 2 formats for their connection stringsarrow-up-right. Make sure to pick the one that best suits your use case.

circle-check
  • SRV: mongodb+srv://[username:password@]host[/[defaultauthdb][?options]] (elements in brackets [] are optional)

    • Example: mongodb+srv://myDatabaseUser:D1fficultP%[email protected]/?retryWrites=true&w=majority

  • Standard: mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]] (elements in brackets [] are optional)

    • Example: mongodb://myDatabaseUser:D1fficultP%[email protected]:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl

Find more examples herearrow-up-right.

Getting your connection string

To find out your connection string, check out MongoDB's official guide for MongoDB Atlasarrow-up-right, or their guide for self-hosted clustersarrow-up-right.

If you have any questions, don't hesitate to contact us at [email protected]envelope, we're happy to help!

Last updated