How to specify an outgoing address in Java MongoClient

Is there a way to connect to a mongo DB via Java and specify the outgoing address/IP? I couldn’t find any documentation about this feature.

You do that by creating an instance of MongoClient.

See the documentation at MongoClient.

Hi Steeve,
thanks for your reply. It might be that I’m simply overlooking the obvious. I did look at the documentation before, but I cannot find a way to specify my outgoing (i.e., local) address I want to use to connect to a Mongo DB.

Welcome to the MongoDB Community @Michael_Bauland!

The outgoing IP address is implicit in the network route your requests take to get to a destination server, and cannot be set by a client.

Can you share more details about the problem you are trying to solve? Are you trying to determine what the inbound IP address should be for a firewall or allow list configuration? If so, is the deployment you are trying to connect to self-hosted or using a service like MongoDB Atlas?

Regards,
Stennie

Hi Stennie,

Is this a peculiarity of MongoDB? Because, when I connect to my Postgres DB I am able to specify from which IP I want to connect, e.g., via setting the localSocketAddress in my client software.

I am running a self hosted MongoDB instance. My client Java software is running on a server with multiple IPs. For the firewall configuration I need the source IP to be configurable to a certain service address (hostname or IP).

Hi Michael,

I think this may actually be a peculiarity of JDBC or Java, as I wasn’t able to find examples for other languages.

This isn’t a supported feature in the MongoDB Java driver at the moment, however you could raise a feature request in the MongoDB issue tracker: https://jira.mongodb.org/browse/JAVA.

I would normally limit outbound routing via network management: VLANs are commonly used to segment traffic by application or a VPN/VPC could be set up so there is a single outgoing route to your database deployment. This is the implicit routing I mentioned: your client application would not have to be aware of network interfaces or topology.

Regards,
Stennie

Hi Stennie,
thank you very much for your kind and helpful reply. I’ve created a feature request in the issue tracker.