Permalink
Browse files

MongoDB 3.6 and .NET driver 2.5.0

  • Loading branch information...
1 parent 7e0359a commit 4525efe1ed0535ac62c7692f536139821b845349 @nightroman committed Dec 9, 2017
View
@@ -10,7 +10,8 @@ param(
)
$ModuleName = 'Mdbc'
-$ModuleRoot = "$env:ProgramFiles\WindowsPowerShell\Modules\$ModuleName"
+$ModuleRoot = if ($env:ProgramW6432) {$env:ProgramW6432} else {$env:ProgramFiles}
+$ModuleRoot = "$ModuleRoot\WindowsPowerShell\Modules\$ModuleName"
# Get version from release notes.
function Get-Version {
@@ -19,6 +20,7 @@ function Get-Version {
task Init Meta, {
exec {paket.exe install}
+ Remove-Item paket-files
}
$MetaParam = @{
@@ -31,7 +33,7 @@ task Meta @MetaParam {
$Version = Get-Version
$Project = 'https://github.com/nightroman/Mdbc'
$Summary = 'Mdbc module - MongoDB Cmdlets for PowerShell'
- $Copyright = 'Copyright (c) 2011-2017 Roman Kuzmin'
+ $Copyright = 'Copyright (c) 2011-2018 Roman Kuzmin'
Set-Content Module\$ModuleName.psd1 @"
@{
@@ -120,8 +122,9 @@ task TestHelpExample {
# Synopsis: Convert markdown files to HTML.
# <http://johnmacfarlane.net/pandoc/>
task Markdown {
- exec { pandoc.exe --standalone --from=markdown_strict --output=README.htm README.md }
- exec { pandoc.exe --standalone --from=markdown_strict --output=Release-Notes.htm Release-Notes.md }
+ function Convert-Markdown($Name) {pandoc.exe --standalone --from=gfm "--output=$Name.htm" "--metadata=pagetitle=$Name" "$Name.md"}
+ exec { Convert-Markdown README }
+ exec { Convert-Markdown Release-Notes }
}
# Synopsis: Set $script:Version.
@@ -134,7 +137,7 @@ task Version {
}
# Synopsis: Make the package in z\tools.
-task Package Markdown, (job UpdateScript -Safe), {
+task Package Markdown, ?UpdateScript, {
Remove-Item [z] -Force -Recurse
$null = mkdir z\tools\$ModuleName\en-US, z\tools\$ModuleName\Scripts
View
@@ -1,3 +1,4 @@
+.vs
*.chm
*.chw
*.dll
View
@@ -1,7 +1,7 @@

Mdbc module - MongoDB Cmdlets for PowerShell
-/* Copyright 2011-2017 Roman Kuzmin
+/* Copyright 2011-2018 Roman Kuzmin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -770,7 +770,7 @@ Mongo: { `$pull: { field: <value>|<query> } }
"@
PullAll = @'
Removes multiple values from an existing array. PullAll provides the inverse
-operation of the PushAll operator.
+operation of PushAll.
Mongo: { $pullAll: { field: [ value1, value2, ... ] } }
'@
View
@@ -1,6 +1,21 @@
# Mdbc Release Notes
+## v5.1.0
+
+MongoDB 3.6 and .NET driver 2.5.0
+
+**Potentially incompatible change:** `GuidRepresentation.Standard` is used for
+GUID's as the default value instead of `CSharpLegacy`. In order to use the old
+default value, set the environment variable `Mdbc_GuidRepresentation` to
+`CSharpLegacy` before using Mdbc.
+
+Updates in expressions:
+
+- retired `$pushAll` is still supported in Mdbc but internally uses `$push $each`.
+- `$push` supports negative positions as counted from the end.
+- `$pop` accepts 1 and -1.
+
## v5.0.1
- Require PS 3.0 in the module manifest.
View
@@ -37,6 +37,20 @@ public static void Register()
BsonSerializer.RegisterSerializer(typeof(PSObject), new PSObjectSerializer());
BsonTypeMapper.RegisterCustomTypeMapper(typeof(PSObject), new PSObjectTypeMapper());
+
+ var strGuidRepresentation = Environment.GetEnvironmentVariable("Mdbc_GuidRepresentation");
+ if (strGuidRepresentation == null)
+ {
+ BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;
+ }
+ else
+ {
+ GuidRepresentation valGuidRepresentation;
+ if (Enum.TryParse(strGuidRepresentation, out valGuidRepresentation))
+ BsonDefaults.GuidRepresentation = valGuidRepresentation;
+ else
+ throw new InvalidOperationException(String.Format(null, "Invalid environment variable Mdbc_GuidRepresentation = {0}", strGuidRepresentation));
+ }
}
// NB: Change of the global defaults affects ToString().
// `Strict` (like mongoexport) is tempting, other tools may read it.
@@ -329,6 +343,13 @@ static IMongoQuery IdToQuery(object id)
return Query.EQ(MyValue.Id, value);
}
+ //TODO PSCustomObject
+ // We can get data as MdbcDictionary or as PSCustomObject. The first
+ // can be used as a query, the second cannot. This is not "symmetric".
+ // Think. NB New-MdbcData converts PSCustomObject to MdbcDictionary.
+ //var custom = value as PSCustomObject;
+ //if (custom != null)
+ // return new QueryDocument(ToBsonDocument(custom));
public static IMongoQuery ObjectToQuery(object value)
{
if (value == null)
@@ -230,7 +230,11 @@ public PSObject[] PushAll
{
_PushAll = new UpdateBuilder();
foreach (var e in Convert(value, Actor.ToEnumerableBsonValue))
- _PushAll.Combine(Update.PushAll(e.Key, e.Value));
+ {
+ var doc = new BsonDocument();
+ doc.Add("$each", new BsonArray(e.Value));
+ _PushAll.Combine(Update.Push(e.Key, doc));
+ }
}
}
UpdateBuilder _PushAll;
View
@@ -209,6 +209,9 @@ UpdateCompiler Mul(BsonDocument document, string name, BsonValue value)
static Expression PopExpression(Expression that, Expression field, BsonValue value)
{
int pop = value.IsNumeric ? value.ToInt32() : 0;
+ if (pop != 1 && pop != -1)
+ throw new MongoException(string.Format(null, @"Value ""{0}"" must be 1 or -1.", value));
+
return Expression.Call(that, GetMethod("Pop"), Data, field, Expression.Constant(pop, typeof(int)));
}
UpdateCompiler Pop(BsonDocument document, string name, int value)
@@ -265,7 +268,7 @@ static void Pull(BsonDocument document, string name, BsonValue value, bool all)
value = wrapper.Clone();
var predicate = QueryCompiler.GetFunction(value.AsBsonDocument);
- for (int i = array.Count; --i >= 0; )
+ for (int i = array.Count; --i >= 0;)
{
var v = array[i];
if (v.BsonType == BsonType.Document && predicate(v.AsBsonDocument))
@@ -275,7 +278,7 @@ static void Pull(BsonDocument document, string name, BsonValue value, bool all)
else
{
BsonArray values = all ? value.AsBsonArray : null;
- for (int i = array.Count; --i >= 0; )
+ for (int i = array.Count; --i >= 0;)
{
if (values == null)
{
@@ -333,24 +336,29 @@ static void Push(BsonDocument document, string name, BsonValue value, bool all,
var array = v2.AsBsonArray;
if (all)
{
- if (position < 0 || position >= array.Count)
+ // Mongo 3.6 deals with negative, counts from the end
+ if (position < 0)
+ {
+ position = array.Count + position;
+ if (position < 0)
+ position = 0;
+ }
+
+ if (position >= array.Count)
{
array.AddRange(value.AsBsonArray);
}
else
{
var array2 = value.AsBsonArray;
- for (int i = array2.Count; --i >= 0; )
+ for (int i = array2.Count; --i >= 0;)
array.Insert(position, array2[i]);
}
}
else
{
- //TODO is position actually used in this case?
- if (position < 0 || position >= array.Count)
- array.Add(value);
- else
- array.Insert(position, value);
+ // position is not used in this case
+ array.Add(value);
}
}
static Expression PushExpression(Expression that, Expression field, BsonValue value)
@@ -371,7 +379,7 @@ static Expression PushExpression(Expression that, Expression field, BsonValue va
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
static Expression PushEachExpression(Expression that, Expression field, BsonDocument document)
{
- int position = -1;
+ int position = int.MaxValue;
BsonArray each = null;
BsonValue sort = null, slice = null;
foreach (var e in document.Elements)
@@ -396,8 +404,6 @@ static Expression PushEachExpression(Expression that, Expression field, BsonDocu
if (!e.Value.IsNumeric)
throw new ArgumentException("$position must be a numeric value.");
position = e.Value.ToInt32();
- if (position < 0)
- throw new ArgumentException("$position must not be negative.");
break;
default:
throw new ArgumentException(string.Format(null, "Unrecognized clause in $push: ({0}).", e.Name));
@@ -441,7 +447,7 @@ static Expression PushEachExpression(Expression that, Expression field, BsonDocu
UpdateCompiler Push(BsonDocument document, string name, BsonValue value)
{
- Push(document, name, value, false, -1);
+ Push(document, name, value, false, int.MaxValue);
return this;
}
static Expression PushAllExpression(Expression that, Expression field, BsonValue value)
@@ -450,7 +456,7 @@ static Expression PushAllExpression(Expression that, Expression field, BsonValue
throw new ArgumentException("Push all/each value must be array.");
return Expression.Call(that, GetMethod("PushAll"), Data, field,
- Expression.Constant(value, typeof(BsonValue)), Expression.Constant(-1, typeof(int)));
+ Expression.Constant(value, typeof(BsonValue)), Expression.Constant(int.MaxValue, typeof(int)));
}
UpdateCompiler PushAll(BsonDocument document, string name, BsonValue value, int position)
{
View
@@ -0,0 +1,25 @@
+
+<#
+.Synopsis
+ Covers some how-to cases.
+#>
+
+Import-Module Mdbc
+
+# Synopsis: Create unique index. #15
+task CreateUniqueIndex {
+ Connect-Mdbc -NewCollection
+
+ $keys = (New-Object MongoDB.Driver.Builders.IndexKeysBuilder).Ascending('tax-id')
+ $options = (New-Object MongoDB.Driver.Builders.IndexOptionsBuilder).SetName('tax-id').SetUnique($true)
+ $null = $Collection.CreateIndex($keys, $options)
+
+ $r = $Collection.GetIndexes()
+ $r | Out-String
+
+ equals $r.Count 2
+ equals $r[0].Name _id_
+ equals $r[0].IsUnique $false
+ equals $r[1].Name tax-id
+ equals $r[1].IsUnique $true
+}
@@ -16,7 +16,7 @@ task ErrorControl {
Connect-Mdbc -NewCollection
@{_id=1; n=1} | Add-MdbcData
- $pattern = '*Can only $pop from arrays.*'
+ $pattern = "*Command findAndModify failed: Path 'n' contains an element of non-array type 'int'.*"
$command = New-MdbcData
$command.findAndModify = 'test'
@@ -125,14 +125,14 @@ task All {
test { New-MdbcQuery Name -All $true } '{ "Name" : { "$all" : [true] } }'
test { New-MdbcQuery Name -All $date } '{ "Name" : { "$all" : [ISODate("2011-11-11T00:00:00Z")] } }'
test { New-MdbcQuery Name -All 1.1 } '{ "Name" : { "$all" : [1.1000000000000001] } }'
- test { New-MdbcQuery Name -All $guid } '{ "Name" : { "$all" : [CSUUID("12345678-1234-1234-1234-123456789012")] } }'
+ test { New-MdbcQuery Name -All $guid } '{ "Name" : { "$all" : [UUID("12345678-1234-1234-1234-123456789012")] } }'
test { New-MdbcQuery Name -All 1 } '{ "Name" : { "$all" : [1] } }'
test { New-MdbcQuery Name -All 1L } '{ "Name" : { "$all" : [NumberLong(1)] } }'
test { New-MdbcQuery Name -All text } '{ "Name" : { "$all" : ["text"] } }'
test { New-MdbcQuery Name -All $true, more } '{ "Name" : { "$all" : [true, "more"] } }'
test { New-MdbcQuery Name -All $date, more } '{ "Name" : { "$all" : [ISODate("2011-11-11T00:00:00Z"), "more"] } }'
test { New-MdbcQuery Name -All 1.1, more } '{ "Name" : { "$all" : [1.1000000000000001, "more"] } }'
- test { New-MdbcQuery Name -All $guid, more } '{ "Name" : { "$all" : [CSUUID("12345678-1234-1234-1234-123456789012"), "more"] } }'
+ test { New-MdbcQuery Name -All $guid, more } '{ "Name" : { "$all" : [UUID("12345678-1234-1234-1234-123456789012"), "more"] } }'
test { New-MdbcQuery Name -All 1, more } '{ "Name" : { "$all" : [1, "more"] } }'
test { New-MdbcQuery Name -All 1L, more } '{ "Name" : { "$all" : [NumberLong(1), "more"] } }'
test { New-MdbcQuery Name -All text, more } '{ "Name" : { "$all" : ["text", "more"] } }'
@@ -145,14 +145,14 @@ task In {
test { New-MdbcQuery Name -In $true } '{ "Name" : { "$in" : [true] } }'
test { New-MdbcQuery Name -In $date } '{ "Name" : { "$in" : [ISODate("2011-11-11T00:00:00Z")] } }'
test { New-MdbcQuery Name -In 1.1 } '{ "Name" : { "$in" : [1.1000000000000001] } }'
- test { New-MdbcQuery Name -In $guid } '{ "Name" : { "$in" : [CSUUID("12345678-1234-1234-1234-123456789012")] } }'
+ test { New-MdbcQuery Name -In $guid } '{ "Name" : { "$in" : [UUID("12345678-1234-1234-1234-123456789012")] } }'
test { New-MdbcQuery Name -In 1 } '{ "Name" : { "$in" : [1] } }'
test { New-MdbcQuery Name -In 1L } '{ "Name" : { "$in" : [NumberLong(1)] } }'
test { New-MdbcQuery Name -In text } '{ "Name" : { "$in" : ["text"] } }'
test { New-MdbcQuery Name -In $true, more } '{ "Name" : { "$in" : [true, "more"] } }'
test { New-MdbcQuery Name -In $date, more } '{ "Name" : { "$in" : [ISODate("2011-11-11T00:00:00Z"), "more"] } }'
test { New-MdbcQuery Name -In 1.1, more } '{ "Name" : { "$in" : [1.1000000000000001, "more"] } }'
- test { New-MdbcQuery Name -In $guid, more } '{ "Name" : { "$in" : [CSUUID("12345678-1234-1234-1234-123456789012"), "more"] } }'
+ test { New-MdbcQuery Name -In $guid, more } '{ "Name" : { "$in" : [UUID("12345678-1234-1234-1234-123456789012"), "more"] } }'
test { New-MdbcQuery Name -In 1, more } '{ "Name" : { "$in" : [1, "more"] } }'
test { New-MdbcQuery Name -In 1L, more } '{ "Name" : { "$in" : [NumberLong(1), "more"] } }'
test { New-MdbcQuery Name -In text, more } '{ "Name" : { "$in" : ["text", "more"] } }'
@@ -166,14 +166,14 @@ task NotIn {
test { New-MdbcQuery Name -NotIn $true } '{ "Name" : { "$nin" : [true] } }'
test { New-MdbcQuery Name -NotIn $date } '{ "Name" : { "$nin" : [ISODate("2011-11-11T00:00:00Z")] } }'
test { New-MdbcQuery Name -NotIn 1.1 } '{ "Name" : { "$nin" : [1.1000000000000001] } }'
- test { New-MdbcQuery Name -NotIn $guid } '{ "Name" : { "$nin" : [CSUUID("12345678-1234-1234-1234-123456789012")] } }'
+ test { New-MdbcQuery Name -NotIn $guid } '{ "Name" : { "$nin" : [UUID("12345678-1234-1234-1234-123456789012")] } }'
test { New-MdbcQuery Name -NotIn 1 } '{ "Name" : { "$nin" : [1] } }'
test { New-MdbcQuery Name -NotIn 1L } '{ "Name" : { "$nin" : [NumberLong(1)] } }'
test { New-MdbcQuery Name -NotIn text } '{ "Name" : { "$nin" : ["text"] } }'
test { New-MdbcQuery Name -NotIn $true, more } '{ "Name" : { "$nin" : [true, "more"] } }'
test { New-MdbcQuery Name -NotIn $date, more } '{ "Name" : { "$nin" : [ISODate("2011-11-11T00:00:00Z"), "more"] } }'
test { New-MdbcQuery Name -NotIn 1.1, more } '{ "Name" : { "$nin" : [1.1000000000000001, "more"] } }'
- test { New-MdbcQuery Name -NotIn $guid, more } '{ "Name" : { "$nin" : [CSUUID("12345678-1234-1234-1234-123456789012"), "more"] } }'
+ test { New-MdbcQuery Name -NotIn $guid, more } '{ "Name" : { "$nin" : [UUID("12345678-1234-1234-1234-123456789012"), "more"] } }'
test { New-MdbcQuery Name -NotIn 1, more } '{ "Name" : { "$nin" : [1, "more"] } }'
test { New-MdbcQuery Name -NotIn 1L, more } '{ "Name" : { "$nin" : [NumberLong(1), "more"] } }'
test { New-MdbcQuery Name -NotIn text, more } '{ "Name" : { "$nin" : ["text", "more"] } }'
@@ -131,17 +131,17 @@ task Pull {
task Push {
test { New-MdbcUpdate -Push @{Name = 1} } '{ "$push" : { "Name" : 1 } }'
- test { New-MdbcUpdate -PushAll @{Name = 1} } '{ "$pushAll" : { "Name" : [1] } }'
+ test { New-MdbcUpdate -PushAll @{Name = 1} } '{ "$push" : { "Name" : { "$each" : [1] } } }'
test { New-MdbcUpdate -Push @{Name = 1, 2} } '{ "$push" : { "Name" : [1, 2] } }'
- test { New-MdbcUpdate -PushAll @{Name = 1, 2} } '{ "$pushAll" : { "Name" : [1, 2] } }'
+ test { New-MdbcUpdate -PushAll @{Name = 1, 2} } '{ "$push" : { "Name" : { "$each" : [1, 2] } } }'
test { New-MdbcUpdate -Push @{Name = $mdbcArray} } '{ "$push" : { "Name" : [1, 2, 3] } }'
- test { New-MdbcUpdate -PushAll @{Name = $mdbcArray} } '{ "$pushAll" : { "Name" : [1, 2, 3] } }'
+ test { New-MdbcUpdate -PushAll @{Name = $mdbcArray} } '{ "$push" : { "Name" : { "$each" : [1, 2, 3] } } }'
test { New-MdbcUpdate -Push @{Name = $bsonArray} } '{ "$push" : { "Name" : [1, 2, 3] } }'
- test { New-MdbcUpdate -PushAll @{Name = $bsonArray} } '{ "$pushAll" : { "Name" : [1, 2, 3] } }'
+ test { New-MdbcUpdate -PushAll @{Name = $bsonArray} } '{ "$push" : { "Name" : { "$each" : [1, 2, 3] } } }'
test { New-MdbcUpdate -Push @{a = 1; b = 2}, @{c = 3} } '{ "$push" : { "a" : 1, "b" : 2, "c" : 3 } }'
- test { New-MdbcUpdate -PushAll @{a = 1; b = 2}, @{c = 3} } '{ "$pushAll" : { "a" : [1], "b" : [2], "c" : [3] } }'
+ test { New-MdbcUpdate -PushAll @{a = 1; b = 2}, @{c = 3} } '{ "$push" : { "a" : { "$each" : [1] }, "b" : { "$each" : [2] }, "c" : { "$each" : [3] } } }'
}
@@ -267,8 +267,8 @@ task Type {
query @{int=@{'$type'='int'}} 1 '{ "int" : { "$type" : "int" } }' 'Type(data, "int", Int32)'
query @{int=@{'$type'='number'}} 1 '{ "int" : { "$type" : "number" } }' 'TypeNumber(data, "int")'
- query @{int=@{'$type'=$null}} -QError '*$type is not a number or a string*' -EError '*$type argument must be number or string.*'
- query @{int=@{'$type'='16'}} -QError '*unknown string alias for $type*' -EError '*Unknown string alias of $type argument.*'
+ query @{int=@{'$type'=$null}} -QError '*type must be represented as a number or a string.*' -EError '*$type argument must be number or string.*'
+ query @{int=@{'$type'='16'}} -QError '*Unknown type name alias: 16.*' -EError '*Unknown string alias of $type argument.*'
}
task Mod {
@@ -31,7 +31,7 @@ task AddToSet {
equals $d.array[1]
}{
Connect-Mdbc -NewCollection
- $1310110108 = '*Cannot apply $addToSet to a non-array field.*'
+ $1310110108 = '*Cannot apply $addToSet to non-array field.*'
}{
Open-MdbcFile
$1310110108 = 'Value "value" must be array.'
@@ -285,8 +285,8 @@ task ChangeId {
Connect-Mdbc -NewCollection
@{_id = 1} | Add-MdbcData
- Test-Error { Update-MdbcData (New-MdbcUpdate -Set @{_id = 42; x = 1}) @{} } "*the (immutable) field '_id' was found*"
- Test-Error { Update-MdbcData (New-MdbcUpdate -Unset _id) @{} } "*the (immutable) field '_id' was found*"
+ Test-Error { Update-MdbcData (New-MdbcUpdate -Set @{_id = 42; x = 1}) @{} } "*the immutable field '_id'*"
+ Test-Error { Update-MdbcData (New-MdbcUpdate -Unset _id) @{} } "*the immutable field '_id'*"
$r = Get-MdbcData
equals $r.Count 1
equals $r._id 1
Oops, something went wrong.

0 comments on commit 4525efe

Please sign in to comment.