Using MongoDB in WebGL

Hi, I’m trying to use MongoDB in my game while building it in WebGL
It works perfectly in the editor, but it starts getting errors as I build it and run on chrome

6th.framework.js:2 NotSupportedException: C:\Program Files\Unity\Hub\Editor\2021.1.0f1\Editor\Data\il2cpp\libil2cpp\icalls\mscorlib\System.Reflection\Module.cpp(112) : Unsupported internal call for IL2CPP:Module::GetPEKind - "This icall is not supported by il2cpp."
_JS_Log_Dump @ 6th.framework.js:2
6th.framework.js:2 UnloadTime: 0.440000 ms
6th.loader.js:1 64
6th.loader.js:1 64
printErr @ 6th.loader.js:1
6th.loader.js:1 Invoking error handler due to
abort(64) at Error
    at jsStackTrace (http://localhost:5309/Build/6th.framework.js:2:16043)
    at stackTrace (http://localhost:5309/Build/6th.framework.js:2:16214)
    at abort (http://localhost:5309/Build/6th.framework.js:2:748)
    at http://localhost:5309/Build/6th.wasm:wasm-function[52432]:0xd7167a
    at http://localhost:5309/Build/6th.wasm:wasm-function[16532]:0x669250
    at http://localhost:5309/Build/6th.wasm:wasm-function[24703]:0x81d25a
    at http://localhost:5309/Build/6th.wasm:wasm-function[28193]:0x89c3b0
    at http://localhost:5309/Build/6th.wasm:wasm-function[25575]:0x842830
    at dynCall_iiii (http://localhost:5309/Build/6th.wasm:wasm-function[52319]:0xd70c8e)
    at Object.dynCall_iiii (http://localhost:5309/Build/6th.framework.js:2:457083)
    at invoke_iiii (http://localhost:5309/Build/6th.framework.js:2:333956)
    at http://localhost:5309/Build/6th.wasm:wasm-function[25573]:0x842572
    at http://localhost:5309/Build/6th.wasm:wasm-function[24877]:0x822fc1
    at http://localhost:5309/Build/6th.wasm:wasm-function[4148]:0x198feb
    at http://localhost:5309/Build/6th.wasm:wasm-function[4146]:0x198cc3
    at http://localhost:5309/Build/6th.wasm:wasm-function[8091]:0x2fc1d0
    at http://localhost:5309/Build/6th.wasm:wasm-function[8089]:0x2fb3b9
    at http://localhost:5309/Build/6th.wasm:wasm-function[10183]:0x3c20c7
    at http://localhost:5309/Build/6th.wasm:wasm-function[8326]:0x3166bf
    at http://localhost:5309/Build/6th.wasm:wasm-function[10607]:0x3f0ef9
    at http://localhost:5309/Build/6th.wasm:wasm-function[10286]:0x3cbe95
    at http://localhost:5309/Build/6th.wasm:wasm-function[10286]:0x3cbeaa
    at http://localhost:5309/Build/6th.wasm:wasm-function[10281]:0x3cb9c0
    at http://localhost:5309/Build/6th.wasm:wasm-function[10274]:0x3c9c36
    at dynCall_v (http://localhost:5309/Build/6th.wasm:wasm-function[52340]:0xd70fff)
    at Object.dynCall_v (http://localhost:5309/Build/6th.framework.js:2:466481)
    at browserIterationFunc (http://localhost:5309/Build/6th.framework.js:2:174144)
    at Object.runIter (http://localhost:5309/Build/6th.framework.js:2:177205)
    at Browser_mainLoop_runner (http://localhost:5309/Build/6th.framework.js:2:175667)
6th.loader.js:1 Not implemented: Class::FromIl2CppType
6th.framework.js:2 MethodAccessException: Attempt to access method 'MongoDB.Driver.IMongoDatabase.GetCollection' on type '' failed.
_JS_Log_Dump @ 6th.framework.js:2

below is my script

public class m_item
{
    public ObjectId Id { get; set; }

    public string item { get; set; }
    public int cost { get; set; }

    public override string ToString()
    {
        return "item : "+ item + " cost : " + cost;
    }
}

public class DataTest : MonoBehaviour
{
    static string connString = "MYADDRESS";
    public MongoClient client = new MongoClient(connString);

    public IMongoDatabase database;
    public IMongoCollection<m_item> collection;

    // Start is called before the first frame update
    void Start()
    {
        database = client.GetDatabase("test");
        collection = database.GetCollection<m_item>("inventory");

        var filter = Builders<m_item>.Filter.Eq("item", "ccc");
        var result = collection.Find(filter).ToList();
        foreach (var doc in result)
        {
            UnityEngine.Debug.Log(doc.cost);
            UnityEngine.Debug.Log(doc.ToString());
        }

      
    }
 
}

I cannot change the type of DB I’m using cause it’s part of a big project with many other people
Is there anything I can do to make it work properly??ㅠㅠ

Hi,

I think you are running into an incompatibility between IL2CPP and the MongoDB C# driver.
I found a thread on the MongoDB forum describing the same issue: MongoDB and Unity IL2CPP mobile builds - #6 by Stefan_Bock - Drivers - MongoDB Developer Community Forums.
A user there posted some workarounds to get it running. Maybe this will help to solve your issue.

Best wishes
Marcel

Where is your MongoDb hosted?

It’s bad practice for your application to have direct access to the database.

A better solution if you’re hosting your MongoDb in Azure (Azure Cosmos Db), is to have an App Service layer between your game and your database. So your game talks to the App Service layer, and the App Service talks to the db.

You can also use Azure Functions for this. They are pretty much API endpoint functions that can talk to your db on Azure.

If you’re hosting on AWS, a similar solution would be to use AWS Lambda to talk to your MongoDb (DynamoDB on AWS)

Once you create your Azure Function/AWS Lambda endpoint, you make an http request (UnityWebRequest) to the function URL, and let the function make any db calls and return any results to your game.