2017-10-19 49 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或更多。