Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

v2.1.0 Invoke-MdbcCommand + Mdbc.ps1 = interactive shell

  • Loading branch information...
commit 3343549e67c54639270ed95b3d07e532480dc250 1 parent 64ec706
Roman Kuzmin authored
5 .build.ps1
View
@@ -167,9 +167,8 @@ task Zip Package, Version, {
# Make NuGet package.
task NuGet Package, Version, {
$text = @'
-Mdbc is the Windows PowerShell module built on top of the official MongoDB C#
-driver. It provides a few cmdlets and PowerShell friendly features for basic
-operations on MongoDB data.
+Mdbc is the Windows PowerShell module based on the official MongoDB C# driver.
+It makes MongoDB scripting easy and represents yet another MongoDB shell.
'@
# nuspec
Set-Content z\Package.nuspec @"
2  Module/Mdbc.psd1
View
@@ -1,7 +1,7 @@
@{
Author = 'Roman Kuzmin'
- ModuleVersion = '2.0.0'
+ ModuleVersion = '2.1.0'
CompanyName = 'https://github.com/nightroman/Mdbc'
Description = 'Mdbc module - MongoDB Cmdlets for PowerShell'
Copyright = '(C) 2011-2012 Roman Kuzmin. All rights reserved.'
182 Module/en-US/Mdbc.dll-Help.ps1
View
@@ -180,6 +180,32 @@ names for a database.
)
}
+### AbstractDatabase
+$AbstractDatabase = @{
+ parameters = @{
+ Database = @'
+The database instance. If it is not specified then the variable $Database is
+used: it is defined by Connect-Mdbc or assigned explicitly before the call.
+'@
+ }
+}
+
+### AbstractCollection
+$AbstractCollection = @{
+ parameters = @{
+ Collection = $CollectionParameter
+ }
+}
+
+### AbstractWrite
+$AbstractWrite = Merge-Helps $AbstractCollection @{
+ parameters = @{
+ Safe = 'Tells to enable safe mode.'
+ SafeMode = 'Advanced safe mode options.'
+ Result = 'Tells to enable safe mode and output result objects.'
+ }
+}
+
### New-MdbcData
@{
command = 'New-MdbcData'
@@ -489,22 +515,6 @@ Deletes a given field.
)
}
-### AbstractCollection
-$AbstractCollection = @{
- parameters = @{
- Collection = $CollectionParameter
- }
-}
-
-### AbstractWrite
-$AbstractWrite = Merge-Helps $AbstractCollection @{
- parameters = @{
- Safe = 'Tells to enable safe mode.'
- SafeMode = 'Advanced safe mode options.'
- Result = 'Tells to enable safe mode and output result objects.'
- }
-}
-
### Add-MdbcData
Merge-Helps $AbstractWrite @{
command = 'Add-MdbcData'
@@ -669,39 +679,8 @@ Merge-Helps $AbstractWrite @{
)
}
-### Add-MdbcCollection
-@{
- command = 'Add-MdbcCollection'
- synopsis = 'Creates a new collection in a database.'
- description = @'
-This cmdlet is needed only for creation of collections with extra options, like
-capped collections. Ordinary collections do not have to be added explicitly.
-'@
- parameters = @{
- Database = @'
-The database where a new collection is created.
-If it is not specified then the current variable $Database is used.
-'@
- Name = @'
-The name of a new collection.
-'@
- MaxSize = @'
-Sets the max size of a capped collection.
-'@
- MaxDocuments = @'
-Sets the max number of documents in a capped collection in addition to MaxSize.
-'@
- AutoIndexId = @'
-It may be set to true or false to explicitly enable or disable automatic
-creation of a unique key index on the _id field.
-'@
- }
- inputs = @()
- outputs = @()
-}
-
### Invoke-MdbcMapReduce command help
-@{
+Merge-Helps $AbstractCollection @{
command = 'Invoke-MdbcMapReduce'
synopsis = 'Invokes a Map/Reduce command.'
description = ''
@@ -712,7 +691,6 @@ This parameter is used with inline output only.
AsCustomObject = $AsCustomObjectParameter, @'
This parameter is used with inline output only.
'@
- Collection = $CollectionParameter
First = @'
The maximum number of input documents.
It is used together with Query and normally with SortBy.
@@ -765,3 +743,109 @@ This parameter is used together with Query.
@{ text = 'MapReduce'; URI = 'http://www.mongodb.org/display/DOCS/MapReduce' }
)
}
+
+### Add-MdbcCollection
+Merge-Helps $AbstractDatabase @{
+ command = 'Add-MdbcCollection'
+ synopsis = 'Creates a new collection in a database.'
+ description = @'
+This cmdlet is needed only for creation of collections with extra options, like
+capped collections. Ordinary collections do not have to be added explicitly.
+'@
+ parameters = @{
+ Name = @'
+The name of a new collection.
+'@
+ MaxSize = @'
+Sets the max size of a capped collection.
+'@
+ MaxDocuments = @'
+Sets the max number of documents in a capped collection in addition to MaxSize.
+'@
+ AutoIndexId = @'
+It may be set to true or false to explicitly enable or disable automatic
+creation of a unique key index on the _id field.
+'@
+ }
+ inputs = @()
+ outputs = @()
+}
+
+### Invoke-MdbcCommand
+Merge-Helps $AbstractDatabase @{
+ command = 'Invoke-MdbcCommand'
+ synopsis = 'Invokes a command for a database.'
+ description = @'
+This cmdlet is normally used in order to invoke commands not covered by C#
+driver or Mdbc helpers. See MongoDB manuals for available commands and their
+parameters.
+'@
+ parameters = @{
+ Command = @'
+Either the name of command with no arguments or one argument or a JSON-like
+hashtable that defines a more complex command.
+'@
+ Value = @'
+The argument value required by the command with one argument.
+'@
+ }
+ inputs = @()
+ outputs = @{
+ type = 'Mdbc.Dictionary'
+ description = 'The response document wrapped by Mdbc.Dictionary.'
+ }
+ links = @(
+ @{ text = 'Commands'; URI = 'http://www.mongodb.org/display/DOCS/Commands' }
+ )
+ examples = @(
+ @{
+ code = {
+ # Invoke the command `serverStatus` just by name.
+
+ Connect-Mdbc . test
+ Invoke-MdbcCommand serverStatus
+ }
+ test = {
+ $response = . $args[0]
+ if ($response.host -ne $env:COMPUTERNAME) {throw}
+ }
+ }
+ @{
+ code = {
+ # Connect to the database `test` and invoke the command with a
+ # single parameter `global` with the `admin` database specified
+ # explicitly (because the current is `test` and the command is
+ # admin-only)
+
+ Connect-Mdbc . test
+ Invoke-MdbcCommand getLog global -Database $Server['admin']
+ }
+ test = {
+ $response = . $args[0]
+ if (!$response.log) {throw}
+ }
+ }
+ @{
+ code = {
+ # Commands with more then one argument use JSON-like hashes.
+ # The example command creates a capped collection with maximum
+ # set to 5 documents, adds 10 documents, then gets all back (5
+ # documents are expected).
+
+ Connect-Mdbc . test z -NewCollection
+ $null = Invoke-MdbcCommand @{create = 'z'; capped = $true; size = 1kb; max = 5 }
+
+ # set the default collection
+ $Collection = $Database['z']
+
+ # use it in two command implicitly
+ 1..10 | %{@{_id = $_}} | Add-MdbcData
+ Get-MdbcData
+ }
+ test = {
+ $data = . $args[0]
+ if ($data.Count -ne 5) {throw}
+ }
+ }
+ )
+}
40 Module/en-US/about_Mdbc.help.txt
View
@@ -3,16 +3,16 @@ TOPIC
about_Mdbc
SHORT DESCRIPTION
- MongoDB cmdlets built on top of the official C# driver.
+ MongoDB cmdlets based on the official C# driver.
LONG DESCRIPTION
- The module provides cmdlets for the following operations:
+ The module provides the following cmdlets and tools.
- Connect, get instance of a server, database, or collection:
+ Connect, set the default server, database, and collection:
Connect-Mdbc
- Add, get, remove, and update data:
+ Add, get, remove, and update documents:
Add-MdbcData
Get-MdbcData
@@ -27,19 +27,27 @@ LONG DESCRIPTION
New-MdbcUpdate
- Create queries for Get-MdbcData, Remove-MdbcData, and Update-MdbcData:
+ Create queries for Get-MdbcData, Update-MdbcData, and etc.:
New-MdbcQuery
+ Add collections with special properties, for example capped:
+
+ Add-MdbcCollection
+
+ Invoke map and reduce procedure:
+
+ Invoke-MdbcMapReduce
+
+ Invoke any command, including not covered by Mdbc and C# driver:
+
+ Invoke-MdbcCommand
+
+ Interactive profile adds more features for interactive sessions:
+
+ Mdbc.ps1
+
SEE ALSO
- MongoDB: http://www.mongodb.org
- Driver manuals: http://www.mongodb.org/display/DOCS/CSharp+Language+Center
- Driver repository: https://github.com/mongodb/mongo-csharp-driver
- Connect-Mdbc
- Add-MdbcData
- Get-MdbcData
- Remove-MdbcData
- Update-MdbcData
- New-MdbcData
- New-MdbcUpdate
- New-MdbcQuery
+ MongoDB http://www.mongodb.org
+ Driver manuals http://www.mongodb.org/display/DOCS/CSharp+Language+Center
+ Driver repository https://github.com/mongodb/mongo-csharp-driver
13 README.md
View
@@ -3,14 +3,9 @@ Mdbc module - MongoDB Cmdlets for PowerShell
![Powered by MongoDB](https://github.com/downloads/nightroman/Mdbc/PoweredMongoDBblue50.png)
-*Mdbc* is the *Windows PowerShell* module built on top of the official
+*Mdbc* is the *Windows PowerShell* module based on the official
[MongoDB C# driver](https://github.com/mongodb/mongo-csharp-driver).
-It provides a few cmdlets and PowerShell friendly features for basic
-operations on MongoDB data.
-
-The goal is not to replace the driver API with cmdlets for everything but to
-make it easier to use the driver in PowerShell scripts and interactively (see
-the helper script *Mdbc.ps1*).
+It makes MongoDB scripting easy and represents yet another MongoDB shell.
## Quick Start
@@ -77,8 +72,8 @@ 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 the scripts in the *Scripts* directory. They are rather toys but
-can be useful, too, at least for learning.
+Take a look at the scripts in the *Scripts* directory, the interactive profile
+*Mdbc.ps1* in the first place. Other scripts are rather toys but may be useful.
Even more examples can be found in the *Tests* directory. Download the sources.
These tests cover all the cmdlets and most of other helper features.
12 Release-Notes.md
View
@@ -1,6 +1,18 @@
Mdbc Release Notes
==================
+## v2.1.0
+
+New cmdlet `Invoke-MdbcCommand` for invoking any MongoDB commands including not
+covered by Mdbc or C# driver helpers. Mdbc becomes an interactive MongoDB shell
+on PowerShell steroids. NOTE: The parameters are not perhaps stabilized, they
+may depend on the feedback and suggestions.
+
+Interactive profile *Mdbc.ps1*
+
+- New global function `Get-MdbcHelp` gets help for MongoDB commands.
+- New aliases `imc ~ Invoke-MdbcCommand, gmh ~ Get-MdbcHelp`.
+
## v2.0.0
### New concept of the default server, database, and collection
85 Scripts/Mdbc.ps1
View
@@ -1,29 +1,34 @@
<#
.Synopsis
- Connects to a database and adds interactive helpers.
+ Interactive profile with helpers for the Mdbc module.
.Description
- Use it only as the example and base for your own interactive helpers. This
- script reflects personal preferences, its features may not be suitable for
- all scenarios and they may change at any time.
+ NOTE: This script is a profile for interactive use, it reflects personal
+ preferences, features may not be suitable for all scenarios and they may
+ change. Consider this as the base for your own interactive profiles.
The script imports the Mdbc module, connects to the server and database,
- and installs helper aliases and variables designed for interactive use.
+ and installs aliases, functions, and variables for interactive use.
- Global aliases:
+ Aliases:
amd - Add-MdbcData
gmd - Get-MdbcData
- rmd - Remove-MdbcData
- umd - Update-MdbcData
+ gmh - Get-MdbcHelp
+ imc - Invoke-MdbcCommand
nmd - New-MdbcData
nmq - New-MdbcQuery
nmu - New-MdbcUpdate
+ rmd - Remove-MdbcData
+ umd - Update-MdbcData
+
+ Functions:
+ Get-MdbcHelp
- Global variables:
+ Variables:
$Server - connected server
- $Database - connected database
- $Collection - connected collection
+ $Database - default database
+ $Collection - default collection
$m<name> - collection <name> (for each collection)
$<operator> - operator shortcuts for JSON-like expressions
@@ -40,8 +45,8 @@
default name is 'test'.
.Parameter CollectionName
- The name of a collection to be connected, that is stored as $Collection.
- The default is 'test', not necessarily existing.
+ Name of the default collection which instance is refrenced by
+ $Collection. The default is 'test', not necessarily existing.
#>
param
@@ -54,16 +59,60 @@ param
Import-Module Mdbc
-# Aliases
+<#
+.Synopsis
+ Gets help information for MongoDB command(s).
+
+.Description
+ Command format: {Name} {L}{S}{A} {Help}.
+ L - lockType R:read-lock W:write-lock
+ S - slaveOk S:slave-ok
+ A - adminOnly A:admin-only
+
+.Parameter Name
+ Command name or wildcard pattern.
+ The default is '*' (all commands).
+
+.Parameter Database
+ Target database.
+ The default is $Database.
+
+.Parameter All
+ Tells to get all commands including internal.
+
+.Link
+ http://www.mongodb.org/display/DOCS/Commands
+#>
+function global:Get-MdbcHelp([Parameter()]$Name='*', $Database=$Database, [switch]$All)
+{
+ $commands = (Invoke-MdbcCommand listCommands -Database $Database).commands
+ foreach($cmd in $commands.Keys | .{process{if ($_ -like $Name) {$_}}}) {
+ $c = $commands[$cmd]
+ $help = $c.help.Trim()
+ if (!$All -and ($cmd[0] -eq '_' -or $help -match '^Internal')) {continue}
+ $lock = switch($c.lockType) {-1 {'R'} 1 {'W'} 0 {'-'}}
+ $slave = if ($c.slaveOk) {'S'} else {'-'}
+ $admin = if($c.adminOnly) {'A'} else {'-'}
+ @"
+$('-'*($cmd.Length))
+$cmd $lock$slave$admin
+$help
+"@
+ }
+}
+
+### Aliases
Set-Alias -Scope global -Name amd -Value Add-MdbcData
Set-Alias -Scope global -Name gmd -Value Get-MdbcData
-Set-Alias -Scope global -Name rmd -Value Remove-MdbcData
-Set-Alias -Scope global -Name umd -Value Update-MdbcData
+Set-Alias -Scope global -Name gmh -Value Get-MdbcHelp
+Set-Alias -Scope global -Name imc -Value Invoke-MdbcCommand
Set-Alias -Scope global -Name nmd -Value New-MdbcData
Set-Alias -Scope global -Name nmq -Value New-MdbcQuery
Set-Alias -Scope global -Name nmu -Value New-MdbcUpdate
+Set-Alias -Scope global -Name rmd -Value Remove-MdbcData
+Set-Alias -Scope global -Name umd -Value Update-MdbcData
-# Query operators
+### Query operators
$global:all = '$all'
$global:and = '$and'
$global:elemMatch = '$elemMatch'
@@ -84,7 +133,7 @@ $global:regex = '$regex'
$global:size = '$size'
$global:type = '$type'
-# Update operators
+### Update operators
$global:addToSet = '$addToSet'
$global:bit = '$bit'
$global:each = '$each'
2  Src/AssemblyInfo.cs
View
@@ -4,7 +4,7 @@
using System;
[assembly: AssemblyProduct("Mdbc")]
-[assembly: AssemblyVersion("2.0.0")]
+[assembly: AssemblyVersion("2.1.0")]
[assembly: AssemblyTitle("Mdbc module - MongoDB Cmdlets for PowerShell")]
[assembly: AssemblyCompany("https://github.com/nightroman/Mdbc")]
[assembly: AssemblyCopyright("Copyright (c) 2011-2012 Roman Kuzmin")]
42 Src/Commands/AbstractDatabaseCommand.cs
View
@@ -0,0 +1,42 @@
+
+/* Copyright 2011-2012 Roman Kuzmin
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System.Management.Automation;
+using MongoDB.Driver;
+namespace Mdbc.Commands
+{
+ public abstract class AbstractDatabaseCommand : PSCmdlet
+ {
+ [Parameter]
+ public MongoDatabase Database
+ {
+ get
+ {
+ if (_Database == null)
+ {
+ _Database = GetVariableValue(Actor.DatabaseVariable) as MongoDatabase;
+ if (_Database == null) throw new PSArgumentException("Specify database or set default $Database.", "Database");
+ }
+ return _Database;
+ }
+ set
+ {
+ _Database = value;
+ }
+ }
+ MongoDatabase _Database;
+ }
+}
22 Src/Commands/AddCollectionCommand.cs
View
@@ -14,36 +14,16 @@
* limitations under the License.
*/
-using System;
using System.Management.Automation;
-using MongoDB.Driver;
using MongoDB.Driver.Builders;
namespace Mdbc.Commands
{
[Cmdlet(VerbsCommon.Add, "MdbcCollection")]
- public sealed class AddCollectionCommand : PSCmdlet
+ public sealed class AddCollectionCommand : AbstractDatabaseCommand
{
[Parameter(Position = 0, Mandatory = true)]
public string Name { get; set; }
[Parameter]
- public MongoDatabase Database
- {
- get
- {
- if (_Database == null)
- {
- _Database = GetVariableValue(Actor.DatabaseVariable) as MongoDatabase;
- if (_Database == null) throw new PSArgumentException("Specify database or set default $Database.", "Database");
- }
- return _Database;
- }
- set
- {
- _Database = value;
- }
- }
- MongoDatabase _Database;
- [Parameter]
public long MaxSize { get; set; }
[Parameter]
public long MaxDocuments { get; set; }
53 Src/Commands/InvokeCommandCommand.cs
View
@@ -0,0 +1,53 @@
+
+/* Copyright 2011-2012 Roman Kuzmin
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System.Collections;
+using System.Management.Automation;
+using MongoDB.Driver;
+namespace Mdbc.Commands
+{
+ [Cmdlet(VerbsLifecycle.Invoke, "MdbcCommand")]
+ public sealed class InvokeCommandCommand : AbstractDatabaseCommand
+ {
+ [Parameter(Position = 0, Mandatory = true)]
+ public PSObject Command { get; set; }
+ [Parameter(Position = 1)]
+ public object Value { get; set; }
+ protected override void BeginProcessing()
+ {
+ CommandDocument document;
+ var commandName = Command.BaseObject as string;
+ if (commandName != null)
+ {
+ if (Value == null)
+ document = new CommandDocument(commandName, 1);
+ else
+ document = new CommandDocument(commandName, Actor.ToBsonValue(Value));
+ }
+ else
+ {
+ var dictionary = Command.BaseObject as IDictionary;
+ if (dictionary != null)
+ document = new CommandDocument(dictionary);
+ else
+ throw new PSArgumentException("Invalid command type.", "Command");
+ }
+
+ var result = Database.RunCommand(document);
+ WriteObject(new Dictionary(result.Response));
+ }
+ }
+}
2  Src/Mdbc.csproj
View
@@ -73,11 +73,13 @@
<ItemGroup>
<Compile Include="Actor.cs" />
<Compile Include="Commands\AbstractCollectionCommand.cs" />
+ <Compile Include="Commands\AbstractDatabaseCommand.cs" />
<Compile Include="Commands\AbstractWriteCommand.cs" />
<Compile Include="Commands\AddDataCommand.cs" />
<Compile Include="Commands\ConnectCommand.cs" />
<Compile Include="Commands\GetDataCommand.cs" />
<Compile Include="Commands\AddCollectionCommand.cs" />
+ <Compile Include="Commands\InvokeCommandCommand.cs" />
<Compile Include="Commands\InvokeMapReduceCommand.cs" />
<Compile Include="Commands\NewDataCommand.cs" />
<Compile Include="Commands\NewQueryCommand.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.