BsonElement 到 c# 数据类型

发布于 2025-01-08 12:23:28 字数 843 浏览 1 评论 0原文

我的数据库中有一个“BSonElement”,我已使用标准查询重试它。

问题是我无法将 BsonDocument 转换为类型。

示例:

更新 1:

 public partial class item_Stat
{

    [BsonExtraElements]
    public BsonDocument all_stat;
}

基本上,我已经进入了我可以使用“BsonExtraElements”读取的 DB 10-15 属性(字段)。这样,我可以重试属性而不定义它 在 C# 中。

all_stat,可以具有动态变化的 10-15-20 属性。 C# 是类型化语言,因此我无法在 C# 中定义此属性,并且我使用了 ExtraElements。

问题是当我从数据库查询对象时。

var item_db = myMongoCollection.find(theQuery); // find the OBJECT

item_db.all_stat // all the property hare HERE

// find the property "category_01"
var i =  item_db.all_stat.Where(p => p.Name == "category_01").Single();

// ok, i have found the Category, so i can cast it to C# Data Type    
var typed_value = (ItemStatSingle) i.Value // BsonElement to ItemStatSingle

I have an "BSonElement" in my DB and i have retry it with an standard Query.

The problem was that i can't Cast BsonDocument to Type.

Example:

UPDATE 1:

 public partial class item_Stat
{

    [BsonExtraElements]
    public BsonDocument all_stat;
}

Basically, i have into my DB 10-15 property (field) that I can read with "BsonExtraElements". In this way, i can retry property without define it
in C#.

all_stat, can have 10-15-20 property that dinamically change.
C# is typed language, so I can't define this property in C# and i have used ExtraElements.

The problem is when i QUERY the Object from DB.

var item_db = myMongoCollection.find(theQuery); // find the OBJECT

item_db.all_stat // all the property hare HERE

// find the property "category_01"
var i =  item_db.all_stat.Where(p => p.Name == "category_01").Single();

// ok, i have found the Category, so i can cast it to C# Data Type    
var typed_value = (ItemStatSingle) i.Value // BsonElement to ItemStatSingle

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋风の叶未落 2025-01-15 12:23:28

BsonElement.Value 是 BsonValue 类型。使用 As* 方法之一进行适当转换。这里的值是什么类型?由于您有用户定义的类型,因此您最好的选择是像 Barrie 上面所说的那样进行检索。如果您想自定义“映射”,请参阅序列化教程 http:// /www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial

BsonElement.Value is of type BsonValue. Use one of the As* method to convert appropriately. What is the type of the value here? Since you have a user defined type your best option is to retrieve as Barrie says above. If you want to customize the "mapping" refer to the Serialization tutorial http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial

冰雪之触 2025-01-15 12:23:28

以下是您可以执行的操作示例,给定域模型中的类,如下所示:

public class Employee
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
}

您可以像这样使用您的类:

var collection = database.GetCollection<employee>("employees");

var employee = new Employee { Name = "John Smith" };
collection.Insert(employee);

employee = collection.FindOne();</employee>

Here's an example of what you can do, given a class from your domain model like:

public class Employee
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
}

You can use your class like this:

var collection = database.GetCollection<employee>("employees");

var employee = new Employee { Name = "John Smith" };
collection.Insert(employee);

employee = collection.FindOne();</employee>
浪荡不羁 2025-01-15 12:23:28

您不能只从额外的元素中转换 BsonDocument。你必须反序列化它。

假设您有一个类 C

public class C
{
    public int X;
}

和一个 extraDocuments 变量(类似于您的 item_db.all_stat 属性),初始化如下:

var extraElements = new BsonDocument();
var c = new C { X = 1 };
extraElements["c"] = c.ToBsonDocument();

然后您可以提取“c”值并像这样反序列化它:

var r = BsonSerializer.Deserialize<C>(extraElements["c"].AsBsonDocument);
Console.WriteLine(r.ToJson());

You can't just cast a BsonDocument from your extra elements. You have to Deserialize it.

Suppose you have a class C

public class C
{
    public int X;
}

and an extraDocuments variable (analogous to your item_db.all_stat property) initialized like this:

var extraElements = new BsonDocument();
var c = new C { X = 1 };
extraElements["c"] = c.ToBsonDocument();

Then you could extract the "c" value and deserialize it like this:

var r = BsonSerializer.Deserialize<C>(extraElements["c"].AsBsonDocument);
Console.WriteLine(r.ToJson());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文