2017-10-19 53 views
0

我有同样的问题,在这个问题:从蒙戈3.2附加键迁移索引MONGO 3.4

MongoDB dump from 3.2, restore with 3.4, error index save = null

在我的情况,由专人重新创建索引是不是一种选择,我需要一个脚本将其自动化,以便稍后迁移我的生产环境。

我迄今为止尝试:

1 /新数据库在蒙戈外壳运行以下命令:

for (var collection in ["_tempstore", "contracts", "images", "thumbs", "covers", "invoices"]) { 
    db.getCollection("cfs_gridfs." + collection + ".files").createIndex({filename: 1}); 
    db.getCollection("cfs_gridfs." + collection + ".chunks").createIndex({files_id: 1, n: 1}); 
} 

其失败。

2 /快速运行摆脱外来w关键这是我在我的旧数据库索引问题的根源:

db.system.indexes.update({w: {$exists: true}}, {$unset: {w: ""}}) 

这也将失败。

什么是正确的方法?

回答

1

我已经写了一个脚本,我运行反对我倾销的文件来消毒它们。

首先创建这两个文件:

sanitize.sh

#!/usr/bin/env bash 

DUMP_PATH=$1 
for file in $(ls $DUMP_PATH | grep .*\.metadata\.json); do 
    node remove-extraneous-keys-from-indexes.js $DUMP_PATH/$file 
done 

remove-extraneous-keys-from-indexes.js

const fs = require("fs"); 
const {promisify} = require("util"); 

const fileName = process.argv[2]; 

(async() => { 
    const text = await promisify(fs.readFile)(fileName, 'utf8') 
    const json = JSON.parse(text) 
    json.indexes = json.indexes.map(index => ({ 
    v: index.v, 
    key: index.key, 
    name: index.name, 
    ns: index.ns 
    })) 
    await promisify(fs.writeFile)(fileName, JSON.stringify(json)) 
})() 

然后运行

$ chmod u+x sanitize.sh 
$ ./sanitize.sh path/to/dump/folder 

则W母鸡我跑mongorestore,一切都很好。

警告:此脚本假定您具有最新版本的节点运行。通过运行node -v进行检查。它应该是8.6或更多。