当前位置:网站首页>如何将一个mongodb中集合的索引 添加到另一个mongodb中集合中
如何将一个mongodb中集合的索引 添加到另一个mongodb中集合中
2022-07-28 21:48:00 【12程序猿】
一、前言
项目中遇到迁移mongodb数据库的需求,迁移中集合数据迁移成功,但是索引没有迁移过来(除了默认索引),然后对索引做迁移工作,把对mongodb数据库索引的迁移步骤记录下来,以便以后可以用到。
二、mongodb索引 迁移
步骤:
①在原mongodb中查询出所有要迁移集合的索引
②把①中索引添加到目标mongodb数据库中
步骤一:查看要迁移集合的索引
方法1:可以单个集合查看,然后使用索引创建方法在 目标mongodb数据库中 一个一个添加,此方法比较繁琐,不推荐。
方法2:查看原mongodb数据库中的所有索引,编写脚本打印出对应索引的添加语句,复制粘贴到 目标mongodb数据库中去执行即可(推荐)
下面介绍方法2的执行过程:
1.编写脚本,导出索引执行记录
//导出索引的脚本 兼容了唯一索引,和超时配置
var collectionList = db.getCollectionNames();
for(var index in collectionList){
var collection = collectionList[index];
var cur = db.getCollection(collection).getIndexes();
if(cur.length == 1){
continue;
}
for(var index1 in cur){
var next = cur[index1];
if(next["key"]["_id"] == '1'){
continue;
}
print(
"try{ db.getCollection(\""+collection+"\").ensureIndex("+JSON.stringify(next.key)+",{background:1, unique:" + (next.unique || false) + "" + (next.expireAfterSeconds > 0 ? ", expireAfterSeconds :" + next.expireAfterSeconds : "") + " })}catch(e){print(e)}")}}
把上面的导出索引脚本放到 原mongodb数据库中去运行,运行结果如下:
2.复制导出索引执行记录,去目标mongodb数据库中执行
复制上面的打印输出内容,粘贴到 目标mongodb数据库中去执行:
执行成功:
边栏推荐
- 零念科技完成Pre-A轮融资,推动智能驾驶平台软件国产替代
- 金仓数据库 KingbaseES 与 Oracle 的兼容性说明(4. SQL)
- What's special about this wireless router, which is popular in the whole network?
- Price for volume has encountered "six consecutive declines" in sales. Can Volvo, which is no longer safe, turn around?
- [mongodb] basic use of mongodb database, special cases, and the installation and creation process of mongoose (including the installation of mongoose fixed version)
- The safety dog has been selected into many details of cloud security panorama 2.0
- Subscript in swift
- 这个胶水有多强呢?
- All aspect visual monitoring of istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
- Why did "you" become a test / development programmer? The value of your existence
猜你喜欢

Runloop principle (I)

英特尔数据中心GPU正式发货,以开放灵活提供强劲算力

JSP tag case

Wechat applet development ④

Subscript in swift

Achieve high throughput through Wi Fi 7 - insight into the next generation of Wi Fi physical layer

Price for volume has encountered "six consecutive declines" in sales. Can Volvo, which is no longer safe, turn around?

Rhce第一天

Rhce第二天

KingbaseES客户端编程接口指南-ODBC(4. 创建数据源)
随机推荐
Use of typescript class
集火全屋智能“后装市场”,真正玩得转的没几个
The safety dog has been selected into many details of cloud security panorama 2.0
Cnpm installation steps
[image segmentation] vein segmentation based on directional valley detection with matlab code
IOS interview
金仓数据库 KingbaseES V8.3至V8.6迁移最佳实践(2. KingbaseES V8.3和 V8.6 兼容性)
Apk signature.Apk version information
22牛客多校day1 I - Chiitoitsu 概论dp
trivy【2】工具漏洞扫描
22 Niuke multi school Day1 I - Introduction to chiitoitsu DP
智能电视与小程序的结合
Date time functions commonly used in MySQL
22牛客多校day1 J - Serval and Essay 启发式合并
WebView whitelist
Input element label
With the "integration of driving and parking", freytek's high-performance domain controller leads the new track
Objc4-841.13 debuggable / compiled source code update
The Gerrit local code is associated to the remote warehouse
Win11快捷复制粘贴不能用怎么办?Win11快捷复制粘贴不能用