Using .NET Guid as mongodb document id

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Major - P3
    • None
    • Affects Version/s: 1.6
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      I'm trying to use .NET Guid as document id for my mongodb documents. The problem that I face now is that I can't fetch new document by id after insertion:

      MongoCollection.AsQueryable().Where(user => user.Id == id)

      I saw that Guid bytes are reversed in MongoDb, so I tried something like this:

      public TData GetById(Guid id)

      { var reverseId = Reverse(id); return MongoCollection.AsQueryable() .Where(user => user.Id == reverseId) as TData; }

      private static Guid Reverse(Guid initial)

      { byte[] guidBytes = initial.ToByteArray(); Array.Reverse(guidBytes, 0, 4); Array.Reverse(guidBytes, 4, 2); Array.Reverse(guidBytes, 6, 2); return new Guid(guidBytes); }

      Still, it does not work... Please help.

            Assignee:
            Unassigned
            Reporter:
            Nikita Sushkov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: