将带有额外键的索引从 Mongo 3.2 迁移到 Mongo 3.4 [英] Migrating indexes with extra keys from Mongo 3.2 to Mongo 3.4
问题描述
我和这个问题有同样的问题:
I have the same problem as in this question :
MongoDB 转储自3.2,用3.4恢复,错误索引save = null
就我而言,手动重新创建索引不是一种选择,我需要一个脚本来自动执行此操作,以便以后迁移我的生产环境.
In my case, recreating indexes by hand is not an option, I need a script that automates this for migrating my production environment later.
到目前为止我尝试过的:
What I have tried so far :
1/在新数据库上的 mongo shell 中运行:
1/ running this in mongo shell on the new database:
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
键,这是我旧数据库索引中问题的根源:
2/ Getting rid of the extraneous w
key which is the root of the issue in my indexes on my old database by running :
db.system.indexes.update({w: {$exists: true}}, {$unset: {w: ""}})
这也失败了.
正确的方法是什么?
推荐答案
我编写了一个脚本,我可以针对我的转储文件运行该脚本以清理它们.
I have written a script that i run against my dumped files to sanitize them.
首先创建这两个文件:
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
然后当我运行 mongorestore
时,一切都很好.
Then when I run mongorestore
, everything is fine.
警告:此脚本假定您运行的是最新版本的节点.通过运行 node -v
来检查这一点.应该是 8.6
或更高.
WARNING : this script assumes you have the latest version of node running. Check this by running node -v
. It should be 8.6
or more.
这篇关于将带有额外键的索引从 Mongo 3.2 迁移到 Mongo 3.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!