将UUID转换为字符串以便在mongodb中插入

问题描述 投票:0回答:1

尝试以下代码后:

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});
         });

我最终分别得到以下结果

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

但是我正在寻找

"uuid" : "4554a991-2b7f-4464-b871-5f4e91332630"
string mongodb shell uuid robo3t
1个回答
1
投票

UUID().hex()返回您要跟随但不带连字符的字符串。

您可以手动拆分,例如使用正则表达式:

new UUID().hex().match(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/).slice(1,6).join('-')
© www.soinside.com 2019 - 2024. All rights reserved.