开发者

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

开发者 https://www.devze.com 2023-02-12 16:03 出处:网络
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 thi

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 c开发者_开发百科ontents?.

thanks.


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.


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");
});


Consider a record

{ last_name : "Smith", best_score: 3.9 }

The strings "last_name" and "best_score" will be stored in each object's BSON. Using shorter strings would save space:

{ lname : "Smith", score : 3.9 }

Would save 9 bytes per document. This of course reduces expressiveness to the programmer and is not recommended unless you have a collection where this is of significant concern.

Field names are not stored in indexes as indexes have a predefined structure. Thus, shortening field names will not help the size of indexes. In general it is not necessary to use short field names.

check source for more details

but other side is described in famous topic "You saved 5 cents, and your code is not readable, congrats!"

And my own opinion that short name is bad way.

0

精彩评论

暂无评论...
验证码 换一张
取 消