当前位置: 首页 > 知识库问答 >
问题:

将带有额外键的索引从Mongo 3.2迁移到Mongo 3.4

程天佑
2023-03-14

MongoDB从3.2转储,用3.4还原,错误索引保存=null

在我的情况下,手工重新创建索引不是一个选项,我需要一个脚本来自动创建索引,以便以后迁移我的生产环境。

到目前为止我所尝试的:

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}); 
}
db.system.indexes.update({w: {$exists: true}}, {$unset: {w: ""}})

正确的进行方式是什么?

共有1个答案

别开诚
2023-03-14

我已经写了一个脚本,我运行对我的转储文件进行消毒。

首先创建这两个文件

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
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
 类似资料:
  • 我想用某种通配符来表示“现在你可以自由地忽略额外的输入”... 以前的基于SimpleDateFormat的版本很好地处理了这一点... 这里有一个指向拉请求的链接:https://github.com/apache/james-mime4j/pull/44

  • 我必须将昨天添加的一位同事的迁移应用到我的本地数据库: 问题是我的表中已经有数据并且迁移失败: 有什么方法可以避免这种冲突,而不必清除< code>Document表中的所有数据?

  • 我花了一整天的时间试图弄清楚下面的迁移发生了什么。 这是表的迁移。我的想法是使用“id_stays_abroad”和“user_id”作为外键。 这是添加外键的另一个迁移 当我运行php artisan迁移时,我遇到了以下错误: SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter table

  • 我必须定义code.Spatial类,如主键和codels.LScharacteristic codels.PlannedUsing,如外键。当我尝试migrate:ProgrammingError:ERROR:foreign key约束中指定的id列不存在时,我遇到了这个问题。 UPD完整日志:上述异常是以下异常的直接原因: 回溯(最后一次调用):文件“manage.py”,第23行,从命令行执

  • 我正在开发用于搜索的spring应用程序。我使用elasticsearch spring数据库创建索引和管理文档。为了查询(搜索),我使用了elasticsearch中的常规客户端,而不是spring数据。 我注意到,spring数据只有在elasticsearch中缺少索引时才会创建索引。每当将新字段添加到用@Document注释的类时,映射将不会更新。因此,在刚刚添加的字段中搜索会导致错误的请

  • Laravel5.6运行于:PHP7.2,MariaDB 10.3 当我想为我的表设置外键时,我只是不断地犯这个错误。 在其他表中,由Laravel本身定义的所有id变量和无符号自动增量 所以,我的迁移是这样的: 错误如下所示: SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MariaDB服务器版本对应的手册,以了解在LINE 1(SQL:alter