如何在C#中为mongodb文档的条目使用不同的名称? [英] How to have a different name for entries of a mongodb document in C#?

查看:106
本文介绍了如何在C#中为mongodb文档的条目使用不同的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 MongoDB C#中的文档进行CRUD操作.我想在C#中使用长的有意义的属性名称来固定结构化的域实体,但是由于每个属性的名称将针对每个文档保存在MongoDB中,所以这不是一个好主意.这是因为属性名称将多余地保存在数据库中,并影响总存储量和性能.

I'm trying to do CRUD operations on documents in MongoDB and C#. I would like to have fixed structured domain entities in C# with long meaningful property names but since the name of each property will be saved in MongoDB for each document, that's not a good idea. That's because property names will be redundantly saved in database and affect the total storage and performance.

我能想到的解决此问题的解决方案是,在C#中使用与MongoDB中不同的属性名称,这意味着它们之间需要映射.做到这一点的一种优雅方法是合并C#的属性,如下所示:

The solution I can think of to overcome this problem, is to use different names for properties in C# than in MongoDB which means a mapping between them is needed. One elegant way of doing this is to incorporate C#'s attributes, something like this:

class Book
{
    [BsonProperty("n")]
    public string Name { get; set; }
    [BsonProperty("a")]
    public string Author { get; set; }
}

在这种情况下,文档将像这样保存在数据库中:

In this case documents would be saved in database like this:

{ n: "...", a: "..." }

我不确定MongoDB的C#驱动程序是否支持该功能!?我自己找不到它,但我希望我找错地方了!

I'm not sure if this is support in MongoDB's C# Driver or not!? I can not find it myself but I was hoping I'm looking in wrong places!

推荐答案

我终于找到了它.

class Book
{
    [BsonElement("n")]
    public string Name { get; set; }
    [BsonElement("a")]
    public string Author { get; set; }
}

这是页面并提供详细信息.

这篇关于如何在C#中为mongodb文档的条目使用不同的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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