Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-597

Using .NET Guid as mongodb document id

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 1.6
    • None
    • None

    Description

      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.

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: