I am building some update statements with the MongoDB C# driver. The C# API includes both Wrapped and "Un-Wrapped" methods in the Builder
namespace.
On the surface, it appears that these differ by generics and not having to use a BSON wrapper. However, both method types allow me to pass in a non-Bson-Wrapped parameter. Is there a functional difference between the two?
For example (using driver v1.2), here are two uses of Update.Set
:
var myCollection = database.GetCollection<MyObject>(typeof(MyObject).Name);
myCollection.Update(
Query.EQ( "_id", myId ),
Update.Set( "Message", "My message text"));
// And now the same call with "Wrapped" method
myCollection.Update(
Query.EQ( "_id", myId ),
Update.SetWrapped( "Message", "My message text"));
What is the difference between these two calls? If only syntactic sugar - why the need for a Wrapped version?
There is no difference when you set a string value. Wrapped methods are needed when you work with complex types: classes, lists, etc (which is not BsonValue
). They just correctly convert the objects to BsonValue type.
Small example:
With Set you can't do following:
var item = new MyDemo();
Update.Set("Item", item)
You have to use Wrapped method or ToBsonDocument() extention:
var item = new MyDemo();
Update.SetWrapped("Item", item)
That's it!
One note, the driver and most pieces around mongodb are open source. So it isn't a big deal to look at :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With