从旧版UUID迁移到标准UUID [英] Migrate from legacy UUID to standard UUID

查看:81
本文介绍了从旧版UUID迁移到标准UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于mongo的简单迁移框架,该框架正在执行其中传递的一些脚本.

I have a simple migration framework for mongo that is executing some scripts passed in it.

现在,我想将我的LUUID迁移到UUID.我写了以下内容:

Now I want to migrate my LUUID to UUID. I wrote following:

function fixIds(collectionName) {
    function uuidv4() {
        return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    }

    var collection = db.getCollection(collectionName);
    var items = collection.find({}).toArray().map(x => Object.assign(x, { _id: UUID(uuidv4()) })); // replace legace UUID with standard UUID
    collection.drop();
    collection.insertMany(items);
}

fixIds("specialoffers");

然后我运行它:

public static async Task<BsonValue> EvalAsync(this IMongoDatabase database, string javascript)
{
    var client = database.Client as MongoClient;

    if (client == null)
        throw new InvalidOperationException("Client is not a MongoClient");

    var function = new BsonJavaScript(javascript);
    var op = new EvalOperation(database.DatabaseNamespace, function, null);

    using (var writeBinding = new WritableServerBinding(client.Cluster, new CoreSessionHandle(new NoCoreSession())))
    {
        return await op.ExecuteAsync(writeBinding, CancellationToken.None).ConfigureAwait(false);
    }
}

它执行,但是将LUUID值替换为其他LUUID值.但是,当我在 Robo 3T 外壳中运行此脚本时,它可以按预期工作.

It executes, but it replaces LUUID values to other LUUID values. However, when I run very this script in my Robo 3T shell it works as expected.

此代码有什么问题?为什么它只能在外壳上工作?

What is wrong with this code? Why it work from the shell only?

推荐答案

这个人为我工作:

function fixIds(collectionName) {
    var collection = db.getCollection(collectionName);
    var items = collection.find({}).toArray().map(x => Object.assign(x, { _id: new BinData(4, x._id.base64()) })); // replace legacy UUID with standard UUID
    collection.drop();
    collection.insertMany(items);
}

fixIds("specialoffers");

我用 new BinData(4,x._id.base64())})替换了 UUID(uuidv4()),这是正确更新的唯一方法UUID版本.

I replaced UUID(uuidv4()) with new BinData(4, x._id.base64()) }) and this is the only way to correcly update UUID version.

这篇关于从旧版UUID迁移到标准UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆