I was trying to implement MongoDB in my Unity WebGL game and everything is working fine in the editor: I managed to insert and retrieve documents from my Mongo database with no issues.

Everything seemed perfect until I compiled the project and realized that my WebGL build won't communicate with my database at all!

I took a look at the console and I see this error:

MissingMethodException: Default constructor not found for type MongoDB.Bson.Serialization.BsonClassMap`1[[UserAccount, Assembly-CSharp, Version=, Culture=neutral, PublicKeyToken=null]]

I have no clue what's happening. I tried compiling the project for Windows/Linux and I got the same error, then I changed API compatibility level to 4.x and it was working fine on the Windows build, but not on WebGL ( API compatibility level=4.x is checked for WebGL build!)

Can someone lend me a hand with that?

  • \$\begingroup\$ You need your app to connect to websocket/http server(Preferably node.js) which will then connect to mongodb. This will also add security to the app. \$\endgroup\$
    – Nick
    May 6 at 16:17

What you are trying to do here unfortunately can not work. The MongoDB protocol is based directly on TCP/IP. But a web application running in a web browser can not create raw TCP/IP connections. There is no API in JavaScript for doing that. The closest it gets are HTTP requests ("AJAX") and WebSockets (which are intentionally incompatible with regular sockets). A WebGL build is a JavaScript application, so if the JavaScript API can't do something, then Unity won't magically allow you to do it.

If you want to use a MongoDB database from your Unity application running in the browser, then you will have to use a middleware which provides a REST API for MongoDB which you can then access from your game by using the UnityWebRequest class.

  • \$\begingroup\$ that explains everything, it seems I have plenty of work ahead :(, thank you a lot for your help! you saved me! \$\endgroup\$ Apr 10 '19 at 13:19
  • \$\begingroup\$ @roseicollis Are you already committed to using MongoDB as your database? If you are looking for a document-oriented NoSQL database which offers a REST API out-of-the-box, then you might want to take a look at CouchDB. \$\endgroup\$
    – Philipp
    Apr 10 '19 at 13:29
  • \$\begingroup\$ I'll take a look at that, thanks for the suggestion! \$\endgroup\$ Apr 10 '19 at 14:06
  • \$\begingroup\$ @roseicollis When this post answered your question, please accept it by clicking the checkmark-icon next to it. \$\endgroup\$
    – Philipp
    Apr 11 '19 at 8:06
  • \$\begingroup\$ For future reference, I faced the same problems myself, and implemented what Philipp suggested quite recently. I have written a short github repo that details how to connect Unity to a MongoDB Atlas VM here \$\endgroup\$ May 6 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.