[CSHARP-205] Add Insert Methods to MongoCollection<TDefaultDocument> for improved powershell support Created: 18/Apr/11  Updated: 02/Apr/15  Resolved: 25/Apr/11

Status: Closed
Project: C# Driver
Component/s: Feature Request
Affects Version/s: 1.0
Fix Version/s: 1.1

Type: Improvement Priority: Major - P3
Reporter: Justin Dearing Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: powershell
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Powershell



 Description   

As previously stated powershell cannot call generic methods of non-generic classes without nasty reflection. So Added these insert overloads to MongoCollection<TDefaultDocument> allows us to insert documents.

Powershell script to test:

  1. We assume that the driver is installed via the MSI.
    #[string] $mongoDriverPath = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\.NETFramework\v3.5\AssemblyFoldersEx\MongoDB CSharpDriver 0.11").'(default)';
    [string] $mongoDriverPath = "C:\Program Files (x86)\MongoDB\CSharpDriver 1.0\"
    Add-Type -Path "$($mongoDriverPath)\MongoDB.Bson.dll";
    [MongoDB.Bson.BsonDocument] $doc = @{
    "_id"= [MongoDB.Bson.ObjectId]::GenerateNewId();
    "FirstName"= "Justin";
    "LastName"= "Dearing";
    "PhoneNumbers"= [MongoDB.Bson.BsonDocument] @ { 'Home'= '718-641-2098'; 'Mobile'= '646-288-5621'; }

    ;
    };
    #$doc;

Add-Type -Path "$($mongoDriverPath)\MongoDB.Driver.dll";

$db = [MongoDB.Driver.MongoDatabase]::Create('mongodb://localhost/powershell');
[MongoDB.Driver.MongoCollection[MongoDB.Bson.BsonDocument]] $collection = $db['example1'];
$collection.Insert($doc);
$collection.FindOneById($doc['_id']);



 Comments   
Comment by Robert Stam [ 25/Apr/11 ]

Added new non-generic Insert methods.

Comment by Robert Stam [ 25/Apr/11 ]

I tested a slightly different script to confirm that the new MongoCollection constructor is not needed. Here's the script I used:

$mongoDriverPath = "C:\work\10gen\mongodb\mongo-csharp-driver\Driver\bin\Debug"
add-type -path "$($mongoDriverPath)\MongoDB.Bson.dll"
add-type -path "$($mongoDriverPath)\MongoDB.Driver.dll"

$server = [MongoDB.Driver.MongoServer]::Create("mongodb://localhost/?safe=true")
$database = $server["test"]
$collection = $database["test"]

$document = @{
FirstName = "John";
LastName = "Doe";
}

$collection.RemoveAll()
$collection.Insert($document)

$fetched = $collection.FindOne()
$json = $fetched.ToString()
[System.Console]::WriteLine($json)

I think the general rule for Powershell friendliness is: "For every generic method there should be a equivalent non-generic method that Powershell users can call".

This rule will also help C# users when they are writing meta data driven code where the types of the documents being inserted are not known at compile time.

Comment by Robert Stam [ 21/Apr/11 ]

Still don't see why the constructor is needed. The return value of $db["name"] is MongoCollection<BsonDocument>, NOT MongoCollection. So no cast should be required (and therefore no special constructor needed to support the cast). Is there some weird Powershell thing going on that I'm not seeing?

Comment by Justin Dearing [ 21/Apr/11 ]

Robert,

I agree we should try to understand the ramification of supporting powershell. I'll move that general discussion to the dev list. To touch on your specific points.

I don't think the new keyword is needed, but I will test some more.

The constructor is needed, take it away and my code doesn't work. What the constructor does (like the HashTable/Idictionary BsonDocument constructor) is allow Powershell to cast objects. In this case it is casting from a MongoCollection to a MontoCollection<TDefaultDocument>.

I will make InsertBatch work as well. I will make separate tickets for update and remove as well.

As far as being "sure," we can write more unit tests for ambiguous cases. However, without an explicit or implicit case between the two types I don't see how ambiguity can creep in.

Comment by Robert Stam [ 20/Apr/11 ]

I'm going to hold off on this until we fully understand all the ramifications of supporting Powershell.

Comments:

  • The two new Insert members in MongoCollection<TDefaultDocument> have compile time warnings (regarding the new keyword)
  • Not sure why the new constructor is needed (in your sample code the value being assigned to $collection is already of the needed type, should be a straight assignment)
  • I was expecting the new non-generic Insert methods to have an additional nominalType parameter
  • If we do this, we should do it for InsertBatch also
  • We need to be sure any new methods don't introduce compile time ambiguities
Generated at Wed Feb 07 21:36:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.