How can I convert a mongdb UUID() to a string?

Solution 1:

According to your own comment, UUID().toString() includes the method name. Your solution includes the quotation marks. To get rid of those, you could use the following:

UUID().toString().split('"')[1]

Explanation:

  • UUID().toString() will give a string containing UUID("00000000-0000-0000-0000-00000000000").
  • The split('"') will split the string, dividing by the quotation marks, into an array, which is ['UUID(', '00000000-0000-0000-0000-000000000000', ')'].
  • [1] selects the second element from that array, which is your UUID as a string.