Menu

Obfuscating MongoDB POCOs without breaking BSON
πŸ“°

Obfuscating MongoDB POCOs without breaking BSON

DEV Community: mongodbΒ·Zero HeartbeatΒ·3 days ago
#PlHD7I9Y
#devtomongodb#devto#dev#element#name
Long article (1396 words) β€” no AI summary yet.
Reading 0:00
15s threshold

The MongoDB C# driver feels schemaless, but it is not nameless. Every document the driver writes is a BSON map whose keys come from your POCO, and the driver decides those keys by reflecting over your type at startup. Store a Device and the driver walks its public members, maps each to a BSON element, and β€” unless you say otherwise β€” uses the member name as the element name. The collection fills with documents that literally contain the strings "DeviceId" and "Status" . Those strings are now part of your data, not just your code.

That is the collision. An obfuscator renames metadata identifiers; Device.DeviceId is a metadata identifier. Rename it to a and the next InsertOne writes { "a": ... } , while the ten million documents already in the collection still say { "DeviceId": ... } β€” and when you read one of those back, the driver looks for an element named a , cannot find it, and leaves the member at its default. No exception. The query runs, the object materializes, and DeviceId is null .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.