Scripts/Mdbc.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

<#
.Synopsis
    Mdbc module helpers.
 
.Description
    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 profile.
 
    The script imports the module, sets aliases, functions, and variables for
    interactive use and optionally connects to a specified server and database.
 
    Aliases:
        amd - Add-MdbcData
        emd - Export-MdbcData
        gmd - Get-MdbcData
        gmh - Get-MdbcHelp
        imc - Invoke-MdbcCommand
        imd - Import-MdbcData
        nmd - New-MdbcData
        nmq - New-MdbcQuery
        nmu - New-MdbcUpdate
        omf - Open-MdbcFile
        rmd - Remove-MdbcData
        smf - Save-MdbcFile
        umd - Update-MdbcData
 
    Functions:
        Get-MdbcHelp
 
    Variables:
        $Server - connected server
        $Database - default database
        $Collection - default collection
        $m<name> - collection <name> (for each collection)
        $<operator> - read only operator shortcuts for JSON-like expressions
 
    With a large number of collections their names are not displayed. Command
    Get-Variable m*..* is useful for finding a collection by its name pattern.
 
.Parameter ConnectionString
        Connection string, see Connect-Mdbc.
        The default is empty, the script does not connect.
 
.Parameter DatabaseName
        Database name or wildcard pattern. If it is not resolved to an existing
        database name then the script prints all database names and exits. The
        default name is 'test'.
 
.Parameter CollectionName
        Name of the default collection which instance is referenced by
        $Collection. The default is 'test', not necessarily existing.
#>


param
(
    [Parameter()]
    $ConnectionString,
    $DatabaseName = 'test',
    $CollectionName = 'test'
)

Import-Module Mdbc

<#
.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
    Invoke-MdbcCommand
#>

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 emd -Value Export-MdbcData
Set-Alias -Scope global -Name gmd -Value Get-MdbcData
Set-Alias -Scope global -Name gmh -Value Get-MdbcHelp
Set-Alias -Scope global -Name imc -Value Invoke-MdbcCommand
Set-Alias -Scope global -Name imd -Value Import-MdbcData
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 omf -Value Open-MdbcFile
Set-Alias -Scope global -Name rmd -Value Remove-MdbcData
Set-Alias -Scope global -Name smf -Value Save-MdbcFile
Set-Alias -Scope global -Name umd -Value Update-MdbcData

### Operators
@(
    'addToSet'
    'all'
    'and'
    'bit'
    'each'
    'elemMatch'
    'exists'
    'gt'
    'gte'
    'in'
    'inc'
    'lt'
    'lte'
    'mod'
    'ne'
    'nin'
    'nor'
    'not'
    'options'
    'or'
    'pop'
    'pull'
    'pullAll'
    'push'
    'pushAll'
    'regex'
    'rename'
    'set'
    'setOnInsert'
    'size'
    'slice'
    'sort'
    'type'
    'unset'
    'where'
) | .{process{ New-Variable -Name $_ -Value "`$$_" -Scope global -Option ReadOnly -Force }}

# Not connected
if (!$ConnectionString) {return}

# Server variable
Connect-Mdbc $ConnectionString
$global:Server = $Server
Write-Host "Server `$Server $($Server.Settings.Server)"

# Database variable
$name = @($Server.GetDatabaseNames() -like $DatabaseName)
if ($name.Count -ne 1) {
    Write-Host "Server databases: $($Server.GetDatabaseNames())"
    return
}
Write-Host "Database `$Database $name"
$global:Database = $Server.GetDatabase($name)

# Collection variables
$global:Collection = $Database.GetCollection($CollectionName)
$collections = @($Database.GetCollectionNames())
Write-Host "$($collections.Count) collections"
$global:MaximumVariableCount = 32kb
foreach($name in $collections) {
    if (!$name.StartsWith('system.')) {
        if ($collections.Count -lt 50) { Write-Host "Collection `$m$name" }
        New-Variable -Scope global -Name "m$name" -Value $Database.GetCollection($name) -ErrorAction Continue -Force
    }
}