Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# MongoDB Guid Problem

C# MongoDB Guid Problem

Scheduled Pinned Locked Moved C#
csharpmongodbhelptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm learning MongoDB. I'm working on a Repository implementation for both MongoDB and LinqToSQL. My MongoRepository Add method is

    //T is a StudentEntity

    public override void Add(T entity)
    {
    var collection = GetCollection(CollectionName);
    collection.InsertOne(entity);
    }

    This works fine. This writes out

    _id:Binary('aE/9nTBrM0mkDMf6/GpSCg==')
    FirstName:"John"
    LastName:"Smith"
    Class:"English 101"
    Age:32

    to the Students collection. Then, my Get method has:

    public override T Get(Guid id)
    {
    var collection = GetCollection(CollectionName);
    var result = collection.Find(r => r.Id == id).FirstOrDefault();
    return result;
    }

    and I call it like this

    private static void GetStudent()
    {
    var student = _repository.Get(new Guid("aE/9nTBrM0mkDMf6/GpSCg==")); //<=== THROWS
    Console.WriteLine(student);
    }

    The exception is "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'" It seems that Mongo stores the Guid differently than in .Net. What's the right way to fix this? Not sure how to handle this.

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    B Richard DeemingR 2 Replies Last reply
    0
    • K Kevin Marois

      I'm learning MongoDB. I'm working on a Repository implementation for both MongoDB and LinqToSQL. My MongoRepository Add method is

      //T is a StudentEntity

      public override void Add(T entity)
      {
      var collection = GetCollection(CollectionName);
      collection.InsertOne(entity);
      }

      This works fine. This writes out

      _id:Binary('aE/9nTBrM0mkDMf6/GpSCg==')
      FirstName:"John"
      LastName:"Smith"
      Class:"English 101"
      Age:32

      to the Students collection. Then, my Get method has:

      public override T Get(Guid id)
      {
      var collection = GetCollection(CollectionName);
      var result = collection.Find(r => r.Id == id).FirstOrDefault();
      return result;
      }

      and I call it like this

      private static void GetStudent()
      {
      var student = _repository.Get(new Guid("aE/9nTBrM0mkDMf6/GpSCg==")); //<=== THROWS
      Console.WriteLine(student);
      }

      The exception is "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'" It seems that Mongo stores the Guid differently than in .Net. What's the right way to fix this? Not sure how to handle this.

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      CodeProject is your friend: "3 Best Practices for GUID data in MongoDB" [^]

      «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

      1 Reply Last reply
      0
      • K Kevin Marois

        I'm learning MongoDB. I'm working on a Repository implementation for both MongoDB and LinqToSQL. My MongoRepository Add method is

        //T is a StudentEntity

        public override void Add(T entity)
        {
        var collection = GetCollection(CollectionName);
        collection.InsertOne(entity);
        }

        This works fine. This writes out

        _id:Binary('aE/9nTBrM0mkDMf6/GpSCg==')
        FirstName:"John"
        LastName:"Smith"
        Class:"English 101"
        Age:32

        to the Students collection. Then, my Get method has:

        public override T Get(Guid id)
        {
        var collection = GetCollection(CollectionName);
        var result = collection.Find(r => r.Id == id).FirstOrDefault();
        return result;
        }

        and I call it like this

        private static void GetStudent()
        {
        var student = _repository.Get(new Guid("aE/9nTBrM0mkDMf6/GpSCg==")); //<=== THROWS
        Console.WriteLine(student);
        }

        The exception is "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'" It seems that Mongo stores the Guid differently than in .Net. What's the right way to fix this? Not sure how to handle this.

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        Richard DeemingR Online
        Richard DeemingR Online
        Richard Deeming
        wrote on last edited by
        #3

        Kevin Marois wrote:

        "aE/9nTBrM0mkDMf6/GpSCg=="

        That's a Base64 encoded string containing the Guid bytes, so:

        new Guid(Convert.FromBase64String("aE/9nTBrM0mkDMf6/GpSCg=="))

        which returns: 9dfd4f68-6b30-4933-a40c-c7fafc6a520a


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups