Skip to content

Latest commit

 

History

History
91 lines (63 loc) · 2.93 KB

README.md

File metadata and controls

91 lines (63 loc) · 2.93 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.

  • Mdbc v5 for PowerShell v3-5 is built with C# driver v2 and requires .NET Framework 4.5.2
  • Mdbc v5 for PowerShell v6 (.NET Core) may be found at Releases.
  • Mdbc v4 is built with C# driver v1 (suitable for PowerShell 2.0)

Quick Start

Step 1: Get and install for Windows PowerShell.

Mdbc is distributed as the PowerShell Gallery module Mdbc. In PowerShell 5.0 or with PowerShellGet you can install it by this command:

Install-Module Mdbc

Mdbc is also distributed as the NuGet package Mdbc. Download it by NuGet tools or directly. In the latter case save it as ".zip" and unzip. Use the package subdirectory "tools/Mdbc".

Copy the directory Mdbc to one of the PowerShell module directories, see $env:PSModulePath, for example 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 examples provided there for 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 toys but may be useful. More technical examples can be found in Tests in the repository.

Mdbc cmdlets are designed for very basic tasks. For advanced tasks the C# driver API may have to be used directly. In many cases this is easy in PowerShell. See the C# driver manuals for its API details.