Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB C# official driver : Mapping objects to short names to limit space

I searching a way to map the Bson objects defined using readable names ("category") to shorts names ("ct") and limit the space occuped by the items names in the main document base. I have seen this using others Drivers but what about using official Driver. How i can make, where is the best place to define. Can use longnames in queries and retrieve short contents?.

thanks.

like image 955
user325558 Avatar asked Feb 21 '11 17:02

user325558


People also ask

What is MongoDB C driver?

The MongoDB C Driver, also known as “libmongoc”, is a library for using MongoDB from C applications, and for writing MongoDB drivers in higher-level languages. It depends on libbson to generate and parse BSON documents, the native data format of MongoDB.

Can you use MongoDB with C#?

By developing with C# and MongoDB together one opens up a world of possibilities. Console, window, and web applications are all possible. As are cross-platform mobile applications using the Xamarin framework.

Is MongoDB paid?

MongoDB is a NoSQL database that is open source. MongoDB is available in two editions. One is MongoDB Open Source, which is free as part of the Open-Source Community, but for the other editions, you must pay a License fee. When compared to the free edition, this edition has some advanced features.


2 Answers

Since nobody has actually given the answer to the question, here it is.

With the official driver you can do this by decorating a property name with BsonElement. For example:

public class SomeClass
{
    public BsonObjectId Id { get; set; }

    [BsonElement("dt")]
    public DateTime SomeReallyLongDateTimePropertyName { get; set; }
}

Now the driver will use "dt" as the BSON property name.

However, at this time there is no way to query using the POCO property name. You would need to use "dt" in your queries. There is a separate project that is built on top of the C# driver that provides LINQ style querying capabilities, but I have not tested it to verify if it will do what you are asking.

like image 146
Bryan Migliorisi Avatar answered Sep 28 '22 04:09

Bryan Migliorisi


It's better to keep models clean. And MongoDB.Driver allows to do it outside.

BsonClassMap.RegisterClassMap<SomeClass>(x =>
{
     x.AutoMap();
     x.GetMemberMap(m => m.SomeReallyLongDateTimePropertyName).SetElementName("dt");
});
like image 34
rnofenko Avatar answered Sep 28 '22 05:09

rnofenko