[CSHARP-1560] Expose db.collection.save() as SaveAsync on Collection Created: 11/Feb/16  Updated: 11/Mar/19  Resolved: 11/Mar/19

Status: Closed
Project: C# Driver
Component/s: Operations
Affects Version/s: 2.2.3
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Chad Kreimendahl Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-1249 What happened to MongoCollection.Save... Closed

 Description   

Previous (Legacy) versions of the driver included a .Save() method on the collection that would either insert a new record or update an existing (if _id matched). Mongo itself has a collection.save() method that should be exposed for those of us who made previous heavy use of Save()

https://docs.mongodb.org/manual/reference/method/db.collection.save/#db.collection.save



 Comments   
Comment by Ian Whalen (Inactive) [ 11/Mar/19 ]

Closing as Won't Fix - after discussion within the Drivers team we've decided not to pursue such a method for now.

Comment by Craig Wilson [ 14/Apr/16 ]

Thanks Chad.

I'm glad that is working for you. I'll ping you guys back if we come to the conclusion that this should be part of the drivers.

Craig

Comment by Chad Kreimendahl [ 14/Apr/16 ]

So the issue, when comparing old Save functionality to new functionality is that there is currently no good way to send an "_id"-less document for upsert (replaceone w/ upsert), and have it return the new Id. The Legacy .Save method does this very well.

It would be great, instead of building my own IMongoCollection<object> static extensions, if we could get a sync and async method to perform these steps for us in the new code.

Basically we want to take an object that may or may not have an _id assigned, and we want to either create (no _id) or upsert (has _id). If the object did not have an id prior, set it, so we can reuse it post-save.

For now, I've basically copied the code from your Save method in Legacy, and rewritten the Insert and Update to be InsertOneAsync and ReplaceOneAsync, with some minor tweaks.

I could get one of my team to possibly contribute what we've done back into a pull request, if this is something useful beyond just us.

Comment by Chad Kreimendahl [ 25/Mar/16 ]

Thanks... That makes sense.

Comment by Craig Wilson [ 25/Mar/16 ]

Good question. I should have given an explanation.

It is in the shell for backward compatibility and it is in the .NET driver's legacy API. However, none of our drivers have implemented "save" in their most recent major versions. Each driver follows the CRUD specification and we try very hard not to introduce methods that are not across all drivers. In this case, we'd need to have a majority of drivers add the method for consistency, which is why it's undecided.

We have had long discussions about this very method and thus far have decided to not add them to our drivers. In a vast majority of cases, a user will know whether or not the document is new or needs to be updated. In the case where you don't, replaceOne will generally suffice with it's upsert flag set to true.

Comment by Chad Kreimendahl [ 25/Mar/16 ]

Given that this command exists within mongo itself... why would implementing it remain undecided?

Comment by Chad Kreimendahl [ 11/Feb/16 ]

If it helps, we'd consider doing this and sending a pull request. I presume simply wiring up the same functionality that exists in the legacy would be acceptable to most...

That means accepting a document, if it doesn't have an ID, use the serializers' idgenerator, if it does have an id, use update/upsert.

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