PowerShell, MongoDB and Searching GridFS

Posted on Posted in MongoDB, PowerShell

Following on from my previous post;

PowerShell, MongoDB and GridFS File Uploads
http://kowalski.ms/2012/07/25/powershell-mongodb-and-gridfs-file-uploads/

You’re storing your files on GridFS but how can you search for anything in there? Normally you’d need to jump into your MongoDB Shell and perform something like;


use mydatabase
db.fs.files.find({});

{ "_id" : ObjectId("4fe855bd355b900fd8ddeecc"), "chunkSize" : 262144, "contentTy
pe" : "text/xml", "filename" : "xmlfile-016593.xml", "l
ength" : NumberLong(1740), "md5" : "5cb2502a0a5f7de34bdb03f66ddc16eb", "uploadDa
te" : ISODate("2012-06-25T12:12:45.336Z") }

Since we’re working in the PowerShell world, that’s not really much use to us, so lets go ahead and create something that will allow us to search for terms inside of GridFS.

I’ve written a Search-MongoGridFS function that will use the GridFS.Opentext() method to open the file and then we match a term using the Select-String cmdlet. The output will list the matching files that will contain the desired term.

The Function


#
# Search for strings in MongoDB's GridFS function
#

function Search-MongoGridFS
{
Param (
[CmdletBinding()]

[Parameter(
Mandatory=$true,
HelpMessage='Path to the MongoDB C# Driver? MongoDB.Bson.dll and MongoDB.Driver.dll')]
[string]$Driver,

[Parameter(
Mandatory=$true,
HelpMessage='What server do you want to search?')]
[Alias('s', 'svr')]
[string]$Server,

[Parameter(
Mandatory=$true,
HelpMessage='What database do you want to search?')]
[string]$Database,

[parameter(
Mandatory=$false,
HelpMessage='What term do you want to search?')]
[Alias('t')]
[string]$Term
)

Begin {
Clear-Host
# Load MongoDB C# Driver
Add-Type -Path "$path\MongoDB.Bson.dll"
Add-Type -Path "$path\MongoDB.Driver.dll"

# Connect to MongoDB Server and Database
#$db = [MongoDB.Driver.MongoDatabase]::Create('mongodb://localhost/komplex?safe=true;slaveok=true')
$connection = 'mongodb://' + $server + '/' + $database + '?safe=true'
$mongo = [MongoDB.Driver.MongoDatabase]::Create($connection)
# localhost, komplex
}

Process {
[array]$found = @()
$total = 0
$files = $mongo.GridFS.FindAll()
foreach ($file in $files) {
$total++
$perc = ($total / $files.Count()) * 100
Write-Progress -activity "Searching for $term..." -status "Searching..." -PercentComplete $perc
$read = $file.GridFS.OpenText($file.Name)
$string = Select-String -InputObject $($read.ReadToEnd()) -SimpleMatch $term
if ($string -ne $null) {
#$file.Name
$found += $file.Name
}
else {
# ignore
}
$read.Dispose()
}
}

End {
$found
Write-Host
Write-Host "Found " -ForegroundColor DarkCyan -NoNewline
Write-Host $found.Count -ForegroundColor Cyan -NoNewline
Write-Host " files of " -ForegroundColor DarkCyan -NoNewline
Write-Host $files.Count() -ForegroundColor Cyan -NoNewline
Write-Host " containing the term " -ForegroundColor DarkCyan -NoNewline
Write-Host $term -ForegroundColor White -NoNewline
Write-Host "." -ForegroundColor DarkCyan
Write-Host
$found.Clear()
}
}

To perform a search all you need to do is supply the path to your C# driver, the server to search, the database to search and finally the term. The syntax is as follows;


Search-MongoGridFS -driver D:\Kristof\mongodb -server server01 -database mydatabase-term Awesome

Hopefully this will allow you to search your MongoDB GridFS stores and save you some headaches. Obviously you can expand this script to search files and other properties super easy.

Enjoy,

Kristof

2 thoughts on “PowerShell, MongoDB and Searching GridFS

Leave a Reply

Your email address will not be published. Required fields are marked *