Skip to content

Commit

Permalink
Add Many to AddDataCommand, #77
Browse files Browse the repository at this point in the history
  • Loading branch information
nightroman committed Apr 23, 2024
1 parent 7e7babe commit ca6beab
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Module/en-US/Mdbc-Help.ps1
Expand Up @@ -530,6 +530,9 @@ an IEnumerable collection of documents.
NewId = $NewIdParameter
Convert = $ConvertParameter
Property = $PropertyParameter
Many = @'
Tells to collect input data and insert using InsertMany().
'@
}
inputs = $DocumentInputs
links = @(
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -106,6 +106,7 @@ See also tests, for example:
| Watch | Watch-MdbcChange -Database | cursor
| **Collection** | |
| InsertOne | Add-MdbcData | none
| InsertMany | Add-MdbcData -Many | none
| Find | Get-MdbcData | documents
| CountDocuments | Get-MdbcData -Count | count
| Distinct | Get-MdbcData -Distinct | values
Expand Down
4 changes: 4 additions & 0 deletions Release-Notes.md
@@ -1,6 +1,10 @@
# Mdbc Release Notes
[C# driver releases]: https://github.com/mongodb/mongo-csharp-driver/releases

## v6.7.2

`Add-MdbcData -Many` uses `InsertMany()`, #77

## v6.7.1

C# driver 2.25.0
Expand Down
30 changes: 29 additions & 1 deletion Src/Commands/AddDataCommand.cs
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Roman Kuzmin
// http://www.apache.org/licenses/LICENSE-2.0

using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
Expand All @@ -24,14 +25,22 @@ public sealed class AddDataCommand : AbstractCollectionCommand
[Parameter]
public ScriptBlock Convert { get; set; }

[Parameter]
public SwitchParameter Many { get; set; }

[Parameter]
public object[] Property { set { if (value == null) throw new PSArgumentNullException(nameof(value)); _Selectors = Selector.Create(value); } }
IList<Selector> _Selectors;

bool _done;

List<BsonDocument> _manyDocuments;

protected override void BeginProcessing()
{
if (Many)
_manyDocuments = [];

if (!MyInvocation.ExpectingInput)
{
var collection = LanguagePrimitives.GetEnumerable(InputObject);
Expand Down Expand Up @@ -61,7 +70,11 @@ void Process(object InputObject)
var document = DocumentInput.NewDocumentWithId(NewId, Id, InputObject);

document = Actor.ToBsonDocument(document, InputObject, Convert, _Selectors);
Collection.InsertOne(Session, document);

if (Many)
_manyDocuments.Add(document);
else
Collection.InsertOne(Session, document);
}
catch (ArgumentException ex)
{
Expand All @@ -72,4 +85,19 @@ void Process(object InputObject)
WriteException(ex, InputObject);
}
}

protected override void EndProcessing()
{
if (Many)
{
try
{
Collection.InsertMany(Session, _manyDocuments);
}
catch (MongoException ex)
{
WriteException(ex, null);
}
}
}
}
36 changes: 31 additions & 5 deletions Tests/Add-MdbcData.test.ps1
Expand Up @@ -66,12 +66,38 @@ task BadSameId {
equals $data[0].Name 'Hello'
}

### Array and Many

$ManyCount = 20

function Get-DocumentMany {
foreach($_ in 1 .. $ManyCount) {@{_id=$_}}
}

function Assert-DocumentMany {
$r = Get-MdbcData
equals $r.Count $ManyCount
equals ($r[0].ToString()) '{ "_id" : 1 }'
equals ($r[-1].ToString()) "{ `"_id`" : $ManyCount }"
}

# https://github.com/nightroman/Mdbc/issues/51
task AddArray {
Connect-Mdbc -NewCollection
Add-MdbcData @{_id=1}, @{_id=2}
$r = Get-MdbcData
equals $r.Count 2
equals ($r[0].ToString()) '{ "_id" : 1 }'
equals ($r[1].ToString()) '{ "_id" : 2 }'
Add-MdbcData (Get-DocumentMany)
Assert-DocumentMany
}

# https://github.com/nightroman/Mdbc/issues/77
task AddManyArray {
Connect-Mdbc -NewCollection
Add-MdbcData (Get-DocumentMany) -Many
Assert-DocumentMany
}

# https://github.com/nightroman/Mdbc/issues/77
task AddManyPipeline {
Connect-Mdbc -NewCollection
Get-DocumentMany | Add-MdbcData -Many
Assert-DocumentMany
}

0 comments on commit ca6beab

Please sign in to comment.