Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

83 lines (55 sloc) 2.809 kB

MongoDB Cmdlets for PowerShell

Mdbc is the Windows PowerShell module based on the official MongoDB C# driver. Mdbc makes MongoDB scripting in PowerShell easier and provides some extra features like bson/json file collections which do not even require MongoDB.

Quick Start

Step 1: Get and install. Mdbc is distributed as the NuGet package Mdbc. Download it to the current location as the directory "Mdbc" by this PowerShell command:

Invoke-Expression (New-Object Net.WebClient).DownloadString('https://raw.github.com/nightroman/Mdbc/master/Download.ps1')

Alternatively, download it by NuGet tools or directly. In the latter case rename the package to ".zip" and unzip. Use the package subdirectory "tools/Mdbc".

Copy the directory Mdbc to a PowerShell module directory, see $env:PSModulePath, normally like this:

C:/Users/<User>/Documents/WindowsPowerShell/Modules/Mdbc

Step 2: In a PowerShell command prompt import the module:

Import-Module Mdbc

Step 3: Take a look at help:

help about_Mdbc
help Connect-Mdbc -full
...

Step 4: Invoke these operations line by line, reading the comments (make sure that mongod is started, otherwise Connect-Mdbc fails):

# Load the module
Import-Module Mdbc

# Connect the new collection test.test
Connect-Mdbc . test test -NewCollection

# Add some test data
@{_id=1; value=42}, @{_id=2; value=3.14} | Add-MdbcData

# Get all data as custom objects and show them in a table
Get-MdbcData -As PS | Format-Table -AutoSize | Out-String

# Query a document by _id using a query expression
$data = Get-MdbcData (New-MdbcQuery _id -EQ 1)
$data

# Update the document, set the 'value' to 100
$data._id | Update-MdbcData (New-MdbcUpdate -Set @{value = 100})

# Query the document using a simple _id query
Get-MdbcData $data._id

# Remove the document
$data._id | Remove-MdbcData

# Count remaining documents, 1 is expected
Get-MdbcData -Count

If the code above works then the module is installed and ready to use.

Next Steps

Read cmdlet help topics and take a look at their examples, they show some basic use cases to start with.

Take a look at scripts in the directory Scripts, especially the interactive profile Mdbc.ps1. Other scripts are rather toys but may be useful. Even more examples can be found in the directory Tests of the project repository.

Mdbc cmdlets are designed for rather simple jobs. For advanced operations the C# driver API should be used directly. Some API was specifically designed with PowerShell in mind. See the C# driver manuals.

Jump to Line
Something went wrong with that request. Please try again.