使用MongoDB的C#驱动程序:包裹或未包裹? [英] Using the MongoDB C# Driver: Wrapped or Un-Wrapped?

查看:224
本文介绍了使用MongoDB的C#驱动程序:包裹或未包裹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立的MongoDB的C#驱动程序的一些更新语句。对于C#\"> C#的API 包括包裹和非包裹的方法生成器命名空间。



从表面上看,似乎这些被仿制药不同,没有使用的 BSON 包装。然而,这两种类型的方法让我在非BSON包裹的参数传递。有没有两者之间的功能差异?



例如(使用驱动程序V1.2),这里有两个用途 Update.Set



<预类=郎-CS prettyprint-覆盖> VAR MyCollection的= database.GetCollection<为MyObject>(typeof运算(为MyObject )。名称);

myCollection.Update(
Query.EQ(_id,身份识别码),
Update.Set(信息,我的消息文本));

//现在用痴迷的方法
myCollection.Update(
Query.EQ(_id,身份识别码),
相同的呼叫Update.SetWrapped (信息,我的消息文本));



是这两个调用之间的区别?如果只是语法糖 - 为什么需要一个裹版本


解决方案

有就是当你设置一个字符串值没有区别?当您使用复杂的工种,需要包装的方法:类,列表等(这不是 BsonValue )。他们只是正确地转换对象BsonValue类型。



小例子:



通过设置你不能做到以下几点:

  VAR项目=新MyDemo(); 
Update.Set(项目,项目)

您必须使用包装的方法或ToBsonDocument()延伸:

  VAR项目=新MyDemo(); 
Update.SetWrapped(项目,项目)

这就是它!



一个说明,司机和周围的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?

解决方案

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

这篇关于使用MongoDB的C#驱动程序:包裹或未包裹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆