[CSHARP-274] The overloads of Insert and Save in MongoCollection should match Created: 21/Jul/11  Updated: 02/Apr/15  Resolved: 22/Jul/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.1
Fix Version/s: 1.2

Type: Improvement Priority: Minor - P4
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Right now Insert has two more overloads than Save. Make them match.



 Comments   
Comment by Robert Stam [ 22/Jul/11 ]

Not likely we could retire the Save method. All the drivers have one, and we want consistency as much as possible between drivers.

Comment by Roman Kuzmin [ 22/Jul/11 ]

Just a thought to consider, not really a suggestion: an alternative might be to retire the whole Save() method set and to introduce an option in some Insert() methods that tells to perform save/upsert. Advantages: this ticket would not exist , less API, less almost duplicated code, and no need in this:

if (...)
collection.Insert(...);
else
collection.Save(...);

Instead, it might be:

InsertOptions options = ...;
options.Save = ...;
collection.Insert(..., options);

The second version presumably will have advantages in some scenarios.

P.S. My PowerShell module (just a few helper cmdlets) follows this idea: it has the Add-Mongo cmdlet with the -Save switch. There is no Save-Mongo spouse there.

Comment by Robert Stam [ 21/Jul/11 ]

OK, thanks. Now I know why you encountered this. A C# user probably wouldn't have noticed, but it needs to be addressed nonetheless.

Comment by Roman Kuzmin [ 21/Jul/11 ]

Here is the example PowerShell code that shows the issue:

#####

Add-Type -Path MongoDB.Driver.dll
$server = [MongoDB.Driver.MongoServer]::Create("mongodb://localhost")
$database = $server.GetDatabase("test")
$collection = $database.GetCollection("test")

$doc = New-Object MongoDB.Bson.BsonDocument
$doc['Text'] = 'Hello world'

  1. works
    $collection.Insert($doc)
  1. works but this is awkward
    $collection.Save([MongoDB.Bson.BsonDocument], $doc)
  1. fails: Cannot find an overload for "Save" and the argument count: "1".
    $collection.Save($doc)

#####

Generated at Wed Feb 07 21:36:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.