What are the disadvantages of storing Guid as String in MongoDB?

The application saves the Guid field in Mongo, and it ends up storing it as BinData:

"_id" : new BinData(3, "WBAc3FDBDU+Zh/cBQFPc3Q==") 

The advantage in this case is compactness, the disadvantage is manifested when you need to troubleshoot an application. Guides are passed through URLs and constantly convert them to BinData when switching to the Mongo console it is a little painful.

What are the disadvantages of storing guid as strings in addition to increasing size? One advantage is the ease of troubleshooting:

 "_id" : "3c901cac-5b90-4a09-896c-00e4779a9199" 

Here is a prototype of a permanent object in C #:

 class Thing { [BsonIgnore] public Guid Id { get; set; } [BsonId] public string DontUseInAppMongoId { get { return Id.ToString(); } set { Id = Guid.Parse(value); } } } 
+7
source share
2 answers

In addition to the gregor answer, using Guids will currently prevent the use of the new aggregation structure, as it is represented as a binary type. Despite this, you can do what you want in a simpler way. This will allow the monsodb bson library to handle conversions for you.

 public class MyClass { [BsonRepresentation(BsonType.String)] public Guid Id { get; set;} } 
+8
source

The disadvantages are that mongodb is optimized to use the ObjectIDID of BSON, so it will be slightly less efficient to use strings as ObjectID. Also, if you want to use range-based queries in string object identifiers, then lexicographic comparison will be used, which may give different results than you expect. Other than that, you can use strings as an ObjectID. See Optimizing Object Identifiers at http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs

+5
source

All Articles