DEV Community

Angel Daniel Munoz Gonzalez
Angel Daniel Munoz Gonzalez

Posted on

Powershell + Mongo?

Often one of the things I hear when people recommend you stuff for windows development is to switch to git-bash as a default terminal. While I do install git on my machines, I have always wonder why do you need git-bash as default at all? you have Powershell after all!

Powershell comes with a bunch of common bash aliases like ls, cd, cat, etc, you can find most common aliases with Get-Alias (or get-alias), I can see right of the bat wget, tee and some others. Plus Powershell Core is Cross Platform!


Sometimes I need to do some manual backups on mongo databases while I know there are thousands of solutions to this already available but one of the ways to learn is to reinvent the wheel for n-th time so I decided to see if there was some way to talk to mongo from powershell and found this project

GitHub logo nightroman / Mdbc

MongoDB Cmdlets for PowerShell

NuGet PSGallery

Mdbc

MongoDB Cmdlets for PowerShell


Mdbc is the PowerShell module based on the official MongoDB C# driver Mdbc makes MongoDB data and operations PowerShell friendly.

  • The PSGallery package is for PowerShell Core and PowerShell v5.1 .NET 4.7.2
  • The NuGet package is for PowerShell v3-v5.1, .NET 4.7.2

Quick start

Step 1: Get and install

Package from PSGallery

Mdbc for PowerShell Core and v5.1 is published as the PSGallery module Mdbc.

You can install the module by this command:

Install-Module Mdbc
Enter fullscreen mode Exit fullscreen mode

Package from NuGet

Mdbc for PowerShell v3-v5.1 is published 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
Enter fullscreen mode Exit fullscreen mode

and Decided to give it a go, so I installed the vscode extension for Powershell plus the module in my local modules

Install-Module Mdbc -Scope CurrentUser
Enter fullscreen mode Exit fullscreen mode

after that just created a test.ps1 file

Import-Module Mdbc;

function New-MongoExport {
  param (
    # Your Server's URL
    [string]
    $Url = "mongodb://localhost:27017",
    # Database to connect to
    [string]
    $Db = "test",
    # Collection Name
    [string] 
    $CollectionName = "users",
    # Where do we want to put that information
    [string] 
    $Path = ".\$collectionName.json",
    # Default Limit
    [int16] 
    $Limit = 10
  )
  # Remove the file if it exists
  Remove-Item $Path -ErrorAction Continue;
  # Connect and count
  Connect-Mdbc $Url $Db $CollectionName;
  $count = Get-MdbcData -Count;
  # Do some fancy output
  Write-Host "Exporting $($count) records" -ForegroundColor Yellow -BackgroundColor DarkCyan;
  for ($i = 0; $i -lt $count; $i += $Limit) {
    # You don't actually need to paginate
    # The module uses the mongo driver so it's quite fast
    # But anyways it reads like `Skip $i items and take the first $Limit items then, export those`
    Get-MdbcData -Skip $i -First $Limit | Export-MdbcData $Path -Append;
  }
  # We're Done, fancy output
  Write-Host "File Written: $Path" -ForegroundColor Black -BackgroundColor Green;
}

New-MongoExport
# Also you can use it like this
# New-MongoExport -Url mongodb://myotherserver:1234 -Db NotTestDB -CollectionName posts
Enter fullscreen mode Exit fullscreen mode

I think the module actually has a way to export collections directly, but like I said above I just wanted to toy out with this.

I don't know, but this appealed to me makes me wonder what I could do with it, besides automation, you know this language is supposed to be used to automate stuff also anyways

Have you done something in Powershell? share it in the comments!
Also if you don't like Powershell could you share why?
I mean I guess there's a reason why WSL is a thing nowdays.

Top comments (2)

Collapse
 
nightroman profile image
Roman Kuzmin

Hi Angel, I am glad you found Mdbc useful. You might be interested in my recent similar project for LiteDB, Ldbc -- github.com/nightroman/Ldbc

By the way, I got familiar with LiteDB from your AvaFunc project!

Collapse
 
tunaxor profile image
Angel Daniel Munoz Gonzalez

By the way, I got familiar with LiteDB from your AvaFunc project!

Ahh the small world we live in!
I need to update or clean up that project it's just a quick and dirty thing

Ldbc -- github.com/nightroman/Ldbc

this could be really neat for offline scripts!
I will take a look indeed 😋