What are drawbacks of storing Guid as String in MongoDB?

后端 未结 2 2178
孤街浪徒
孤街浪徒 2021-02-19 05:48

An application persists Guid field in Mongo and it ends up being stored as BinData:

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

The

相关标签:
2条回答
  • 2021-02-19 06:23

    The drawbacks are that mongodb is optimised to use BSON ObjectID's so it will be slightly less efficient to use strings as ObjectID's. Also if you want to use range based queries on string ObjectIDs then a lexicographic compare will be used which may give different results than you expect. Other than that you can use strings as ObjectIDs. See Optimizing ObjectIDs http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs

    0 讨论(0)
  • 2021-02-19 06:30

    In addition to gregor's answer, using Guids will currently prevent the use of the new Aggregation Framework as it is represented as a binary type. Regardless, you can do what you are wanting in an easier way. This will let the mongodb bson library handle doing the conversions for you.

    public class MyClass
    {
      [BsonRepresentation(BsonType.String)]
      public Guid Id { get; set;}
    }
    
    0 讨论(0)
提交回复
热议问题