尝试以下代码后:
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"
UUID().hex()
返回您要跟随但不带连字符的字符串。
您可以手动拆分,例如使用正则表达式:
new UUID().hex().match(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/).slice(1,6).join('-')