我有一个简单的mongo迁移框架,可以执行其中传递的一些脚本。
现在我想把我的LUUID迁移到UUID。我写了以下内容:
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外壳中运行这个脚本时,它可以正常工作。
这段代码有什么问题?为什么它只能在shell中工作?
发布于 2018-01-23 22:13:56
这家伙为我工作:
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版本的唯一方法。
https://stackoverflow.com/questions/48402816
复制相似问题