MongoDB connection format issues

I’m using C# drivers on Unity.

This is the connection format that I’ve been using:

mongodb+srv://<user>:<password>@<host_url>

It works great in the editor. However, when building to android, I get an ArgumentOutOfRangeException and the MongoClient initialization fails:

03-11 12:08:05.918 28988 29017 E Unity   : ArgumentOutOfRangeException: List of configured name servers must not be empty.
03-11 12:08:05.918 28988 29017 E Unity   : Parameter name: servers
03-11 12:08:05.918 28988 29017 E Unity   :   at DnsClient.LookupClient.QueryInternal (DnsClient.DnsQuestion question, DnsClient.DnsQuerySettings queryOptions, System.Collections.Generic.IReadOnlyCollection`1[T] servers) [0x00000] in <00000000000000000000000000000000>:0
03-11 12:08:05.918 28988 29017 E Unity   :   at MongoDB.Driver.Core.Misc.DnsClientWrapper.ResolveTxtRecords (System.String domainName, System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0
03-11 12:08:05.918 28988 29017 E Unity   :   at MongoDB.Driver.Core.Configuration.ConnectionString.Resolve (System.Boolean resolveHosts, System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0
03-11 12:08:05.918 28988 29017 E Unity   :   at MongoDB.Driver.MongoUrl.Resolve (System.Boolean resolveHosts, System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0
03-11 12:08:05.918 28988 29017 E Unity   :   at MongoDB.Driver.MongoClientSettings.FromUrl (MongoDB.Driver.MongoUrl url) [0x00000] in <0000000000000000000000000000000

I’ve tried removing the “+srv” part of the connection string like this:

mongodb://<user>:<password>@<host_url>

… but when doing this, the MongoClient initialization is successful but when communicating with the database afterwards, I get an System.TimeoutException. This happens both inside the Editor and device builds:

03-11 12:14:06.783 30434 30457 I Unity   : System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/production.vjcfb.mongodb.net:27017" }", EndPoint: "Unspecified/production.vjcfb.mongodb.net:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: Could not resolve host 'production.vjcfb.mongodb.net'
03-11 12:14:06.783 30434 30457 I Unity   :   at System.Net.Dns.Error_11001 (System.String hostName) [0x00015] in <1e1a09bd7c4e407788af79f6b1d8f250>:0
03-11 12:14:06.783 30434 30457 I Unity   :   at System.Net.Dns.G

Any idea what’s going on here? Thanks in advance!

Hi @TemeS,

If you want to use MongoDB in your Unity games, there are two options:

  1. You can continue to use the C# driver, but within a middleware web application that your Unity game communicates to using HTTP, sockets, etc.
  2. You can use the Realm SDK for Unity and enable sync so your data syncs with MongoDB Atlas.

Even if the C# driver were to work directly in Unity, it probably isn’t a good idea because anyone can just decompile your game and extract your sensitive credentials. This is true for any database or sensitive information in general when it comes to client facing applications.

If you need a starting point with Realm, I have a few tutorials on the subject. This might be a good starting point though:

Hopefully that helps :slight_smile:

2 Likes

Thanks for the reply!

I actually gave Realm a try recently as well but ran into issues using IL2CPP (which is required for at least Google Play submissions and probably App Store as well) there as well. I posted about it here: Realm on a Unity IL2CPP build - #2 by nirinchev

Is there actually a real risk using the C# drivers though? I have a server handling the more delicate parts of the database and communicating that to the client, whereas the client MongoDB user only has read-only access to less sensitive parts of the database (such as item configs). Seems like I’ll probably need to come up with an alternative solution anyway, since the IL2CPP code stripping seems to cause never-ending issues, no matter how much I try to get in between it and preserve MongoDB assemblies.

Basically the client in my game only needs to download config data at the start of the session and then let the server handle the rest. Would you recommend I use some kind of middleware to handle that or use something other than Mongo for that use case all together?

I don’t know much on the IL2CPP stuff, but I’m going to summon @nirinchev since he’ll probably know.

This is more personal opinion, but I think even if you’re planning to do readonly in the game, you are still introducing risk because you’d have to add an open network access rule to Atlas or wherever you’re choosing to host MongoDB. Just like with giving users only the access that they need, the same would apply to outside client connections.

If you’ve already got a server handling the rest, is there anything preventing you from having that same sever distributing the configuration data via an HTTP request and say a JWT token or similar?

I don’t know anything about the IL2CPP stuff, but I made a game with Realm and Unity for Android and it worked out alright.

Maybe you have another dependency with problems?

Happy to pull in the right people to help you get what you need :-).