PowerShell and MongoDB

At the company I am working we’re using MongoDB on the Windows machines. For administration and automating purposes it is common for us to use PowerShell as a scripting language. Out of the box it is not possible to connect to MongoDB via PowerShell. To make this possible you should use the MongoDB C# or .Net drivers. However I found it quite a struggle to get this working, so here I explain how you can achieve this.

You can download the C# and .Net MongoDB driver from the MongoDB website here. In this example I am using version 2.3 of the driver (this does not represent the version number of MongoDB but only the driver).

I am using this driver to connect to a ReplicaSet which is running on MongoDB version 3.2.

Make sure you’ve downloaded the drivers to the machine where you’re gonna run the PowerShell script. Unzip the drivers and place them e.g. in C:\MongoDB\Drivers.

Launch the PowerShell ISE. I am using PowerShell version 4. You can check your version with:

[code]

$host

[/code]

Copy the path where you’ve placed the drivers, in my case “C:\MongoDB\Drivers”.

Use the following command to load the drivers within you PowerShell script:

[code]

Add -Path ("$path\MongoDB.Bson.Dll")

[/code]

You can select this piece of code and run the selection with F8. You shound’t see any output, which is good.

What I found in most other posts, that this should be enough to get it working. However when I try to connect to a MongoDB instance, it is not yet working. See for yourself by using the following piece of code:

[code]

$client = New-Object MongoDB.Driver.MongoClient("mongodb://localhost:27017")

[/code]

If you select this piece of code and execute it (F8). You are probably gonna see the following error:

[code]

New-Object : Exception calling ".ctor" with "1" argument(s): "Could not load file or assembly ‘System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The system cannot find the file specified."
At line:1 char:11
+ $client = New-Object MongoDB.Driver.MongoClient("mongodb://AISC611N01:27017")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

[/code]

To fix is this error you should first get another driver and install it. In the example above I’ve downloaded the driver which uses .NET Core. However the machine where I am running PowerShell and MongoDB on, don’t have necessary tools installed for .NET Core and I don’t have the option to install them. Therefore you should use different drivers.

On the website of MongoDB you’ll find the following compatibility diagram:

Here you should select the one without .NET Core support. So in this case Version 2.2. You can select the version on the drivers website here. Download these drivers and place them on the client where you want to run the PowerShell command. Load the drivers in PowerShell with the following command:

[code]

Add-Type -Path "E:\CSharpDriver-2.2.4\MongoDB.Driver.dll"
Add-Type -Path "E:\CSharpDriver-2.2.4\MongoDB.Bson.dll"

[/code]

Of course replace the path with the ones on your pc.

When you now run the following command you won’t see any errors.

[code]

$client = New-Object MongoDB.Driver.MongoClient("mongodb://localhost:27017")

[/code]

So this is a way to get the MongoDB Drivers working with PowerShell.

Eén reactie

  1. After looking at many sites for answers, yours is the one that help me because what you posted worked. Unfortunately, I encounter errors when I try to query documents in MongoDB using Powershell.

    I hope you put up more information like you have for MongoDB queries using Powershell.

    Thanks for your blog!

Reacties zijn gesloten.