MongoDB.Driver 2.18 .net > 4.72

Writing a simple POC for a project I’m working on and it seems if you need to include the mongocsharpdriver. From what I read I thought that this was deprecated, but if I attempt to run my code without this dependency I get an error message:

Could not load file or assembly 'MongoDB.Driver, Version=2.18.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified

This goes away after I add the mongocsharpdriver from nuget. Am I doing something wrong? or is this somehow expected behaviour?

Hi,

You should have another kind of problem because it works.

<ItemGroup>
    <PackageReference Include="MongoDB.Driver" Version="2.18.0" />
</ItemGroup>

You can try this project and see if it works.

Thanks, I’ll look at that one…I’m still getting that load file error after adding the csharpdriver, which is weird as it worked immediately after i added it, but now it’s not working…so i’m game to look at anything! Also, not sure how much this matters, i’m targetting .net 6 and a console app

Reinstall .NET

That seems a little drastic…but I"m not against it…I’ve just cloned the repo you suggested and the code appears to be working, I was able to connect to my instance. What I’m confused about is what the difference is between the code that I wrote and what you provided. Guess I know what my weekend is going to be like!

Thanks!

The re-install didn’t work, which I didn’t think it would since the cloned repo worked just fine. Not really sure what the big difference is, my code is super simple as I was just getting started, the only real difference is the repo is outputting an exe, and i’m building a dll.

using MongoDB.Driver;

namespace MongoDBDriver
{
    public class MongoDB
    {
        public string? ConnectionString { get; set; }
        public MongoClient? Client { get; set; }
        public MongoDatabaseBase? DatabaseBase { get; set; }

        public MongoDB(string ConnectionString)
        {
            MongoClientSettings settings = MongoClientSettings.FromConnectionString(ConnectionString);
            Client = new MongoClient(settings);
        }
    }
}

literally the extent of my code, was just trying to test a connection to the server.

Remi,

Looking for a bit of insight as I’m struggling with this, ultimately I’d like to write a PowerShell module to handle the mongo stuff for me, and for other projects I can simply create a class library project and things work out fine. But it appears that for MongoDb.Driver to work it needs to be a console app? I’ve copied the code above into a new ‘Console App’ project and it just works.

Do you, or anyone, have insight into why this would be true?

Not sure you can user PowerShell, it can use .NET lib but you lose the app context.

If you need to execute script now you can have very simple .NET Console structure, works like Powershell.

mkdir test
cd test
dotnet new console
dotnet add package MongoDB.Driver

Open Program.cs and do your script here
Better use VSCode and type
code .

using MongoDB.Bson;
using MongoDB.Driver;
// See https://aka.ms/new-console-template for more information
var client = new MongoClient();
Console.WriteLine(client.GetDatabase("test").GetCollection<BsonDocument>("test").FindSync(_=>true).ToList().ToJson());

This is not powershell but this is very simple.