10 / 10
Feb 2024

When I want to use the Realm SDK and the MongoDB Driver SDK in the same Unity project, I encounter the following error. Is there a solution to use both SDKs?

error CS0433: The type ‘BsonDocument’ exists in both ‘MongoDB.Bson, Version=2.23.1.0, Culture=neutral, PublicKeyToken=null’ and ‘Realm, Version=11.6.1.0, Culture=neutral, PublicKeyToken=null’

Hey Alexander, this is due to a bit of an awkwardness due to the lack of NuGet support in Unity. Essentially, MongoDB.Bson is a NuGet package and we can’t request it as a dependency in UPM, which is why we decided to cater to the majority use case, which is to bundle all dependencies of Realm into the dll distributed for Unity. For all of them except MongoDB.Bson we internalize the public members, so you shouldn’t get conflicts. We don’t do this for MongoDB.Bson because you need to be able to use some of the types there (e.g. ObjectId, Decimal128, etc.).

So there are two main ways to handle this - one approach would be to remove the MongoDB.Bson.dll that you’ve added to your project. Not sure how you’re installing the driver, but if you keep the dlls for the other driver packages, but remove MongoDB.Bson, Unity should be able to find it inside the Realm.dll. Alternatively, you can unpack the Realm upm package, remove the Realm.dll, then replace it with the Realm.dll from our NuGet package. You’ll also need to download the dlls for our other dependencies - as listed here: https://github.com/realm/realm-dotnet/blob/71158b7c46513c5574dcdd190190c1eca41d9632/Tools/SetupUnityPackage/CommandLineOptions/RealmOptions.cs#L51-L58 (except MongoDB.Bson, of course). Then you can add the modified package from disk to UPM and that should resolve the conflict.

And on a side note, the Realm SDK comes with a “Remote MongoDB Client”, which allows you to query Atlas data with driver-like API over HTTP. The API is definitely not as rich as the driver API, but it can cover many of the use cases the driver covers and comes with the benefit of going through App Services, which means that all your user permissions and rules will be applied on the data being fetched.

Hi Nikola,

thx for your answer.

I use GitHub - GlitchEnzo/NuGetForUnity: A NuGet Package Manager for Unity to install NuGet packages. My first attempt was to remove the MongoDB.Bson driver package, unfortunately this attempt ends in a reference error:

Assembly ‘Assets/Plugins/MongoDB.Driver.Core.2.23.1/lib/netstandard2.1/MongoDB.Driver.Core.dll’ will not be loaded due to errors:
Unable to resolve reference ‘MongoDB.Bson’. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.

I stumbled over the realm mongodb client but the mongodb driver package is for the game server build. This user permissions, auth and rules handling cannot be used on the server.

Not in the final build, but it’s one code base and for testing and development purposes the server and client runs on the same game instance.

I see, that makes sense. Unfortunately, it doesn’t seem like there’s a way to indicate that a plugin is not to be used on game server builds without also removing it from desktop builds. So I guess the only option that I can think of is to replace the Realm.dll from the Unity package with the one from the nuget package and then manually add the dlls of Realm’s dependencies as described in my first comment. Let me know if that still doesn’t work.

If I install realm via NuGet unity throws the next strange error.

Library/PackageCache/com.unity.scriptablebuildpipeline@1.21.21/Editor/Shared/BuildTasksRunner.cs(28,29): error CS0117: ‘BuildLogger’ does not contain a definition for ‘LogException’

and

error CS1061: ‘ILogger’ does not contain a definition for ‘Log’ and no accessible extension method ‘Log’ accepting a first argument of type ‘ILogger’ could be found (are you missing a using directive or an assembly reference?)

Yeah, I don’t expect Realm installed via NuGet to work out of the box since there are a handful of Unity customizations included in the Unity package. You likely need to add Realm.UnityUtils.dll from the Runtime folder of the Realm Unity package.

Any variant of the package build ends up in a lot of errors, too bad. I’d have really liked to use MongoDb for this project.