How to use .net GUID in JAVA UUID as primary key of mongodb collection

I have mongodb database where each document in each collection has _id field which is .net GUID. Now we want to work with the same data base from java application. How to convert GUID to UUID and map objects correctly?

1 answer

  • answered 2017-10-11 21:34 Jens

    A Java UUID and a Mongodb UUID both implement the UUID standard.

    The textual representation can be parsed with e.g. fromString:

    UUID myUUID = UUID.fromString("449772DE-2780-4412-B9F7-E49E48605875");
    

    To convert a UUID into the String representation use toString:

    String uuid = myUUID.toString(); 
    // gives "449772de-2780-4412-b9f7-e49e48605875"
    

    Upper-/lowercase does not matter (as can be seen in this example).

    See the quickstart on how to query for documents.