2

Upon trying the following code:

db.getCollection('col').find({}).forEach(function(doc){
            var id = new UUID().toString(); // this doesn't work, neither does 'var id = new UUID();'
            db.getCollection('pubCol')
                .update({
                    "uuid" : id,
                    "deleted" : false
                    }
            , {upsert: true});
         });

I end up getting the following results respectively

"uuid" : "UUID(\"4554a991-2b7f-4464-b871-5f4e91332630\")"
"uuid" : UUID("4554a991-2b7f-4464-b871-5f4e91332630")

But I am looking for

"uuid" : "4554a991-2b7f-4464-b871-5f4e91332630"
Naman
  • 27,789
  • 26
  • 218
  • 353

1 Answers1

4

UUID().hex() returns the string you are after but without hyphens.

You can split it manually, e.g. with regexp:

UUID().hex().match(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/).slice(1,6).join('-')
Alex Blex
  • 34,704
  • 7
  • 48
  • 75
  • This would work to what I can test out. But the regex part makes it complicated though. – Naman Jan 16 '20 at 06:11
  • How about `.toString().substring(...)`? – Valijon Jan 16 '20 at 09:16
  • 2
    It will do the trick but it's not the intended use of toString(). UUID has 3 methods: base64, hex, and toString. The later returns JS representation. You want the hex one. The regexp is a workaround for the lack of hyphens. I would rather raise a feature request to add an optional "separator" parameter to the hex method. – Alex Blex Jan 16 '20 at 09:50