简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM