Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the MongoDB C# Driver: Wrapped or Un-Wrapped?

Tags:

c#

mongodb

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?

like image 754
SethO Avatar asked Oct 12 '12 16:10

SethO


1 Answers

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 :)

like image 50
Andrew Orsich Avatar answered Oct 18 '22 10:10

Andrew Orsich