Overview Example

This code mounts a MongoDB server, creates a new database and collection, and then saves a snapshot of the current process list and memory usage. It also demonstrates some advanced features of the provider, such as server-side cursor use.

# import the mongo module
import-module mongo;

# mount the server as a drive
new-psdrive -name m -root '' -psprovider mongo -connectionString 'mongodb://mongoserver';

# CREATING ITEMS #############
# create the database
new-item m:/Instrumentation;

# create the collection
new-item m:/Instrumentation/Processes;

# create documents in the collection
get-process | select-object -prop PSResources | new-item m:/Instrumentation/Processes;

# NAVIGATING MONGODB ###################
cd m:/        # explore the server
dir             # list all databases on the server
cd ./Instrumentation   # explore the Instrumentation database
dir                            # list all users and collections in the Instrumentation database
cd ./Processes           # explore the Processes collection
dir                            # list all documents and indexes in the Processes collection

# GETTING AND SEARCHING ITEMS #############
# fetch and work with the documents at your leisure (even in another PowerShell session):
dir | sort VirtualMemorySize | format-table *

# filter and cull your results using native server-side cursors:
dir -filter "this.VirtualMemorySize < 10000000" -limit 15 -fields Name,VirtualMemorySize

# execute server-side searches using an intuitive document-style syntax:
dir -find @{ 'Name' = 'svchost' };

# execute server-size regular expression searching:
dir -find @{ 'Name' = [regex]'svc.+' };

# UPDATING DOCUMENTS ##################
# modify and append fields on existing documents
$doc = get-item ./devenv;
$doc.Id = -1;
$doc | add-member NoteProperty -name "IsVisualStudio" -value $true;
$doc | set-item;

# perform server-side atomic updates
set-item ./devenv -set @{ 'Name' = "Visual Studio" }

# REMOVING DOCUMENTS #################
# remove one document
remove-item './Visual Studio';

# remove a subset of documents
dir -skip 10 -take 5 | remove-item;

# remove the collection (and any contained documents)
remove-item m:/Instrumentation/Processes -recurse -force;

# remove the database (and any collections, documents, etc)
remove-item m:/Instrumentation -recurse -force;

Last edited Mar 24, 2011 at 4:09 PM by beefarino, version 1

Comments

No comments yet.