fs项目---->migrate-mongo的使用(一)
tw项目中用的是mongo数据库,数据的迁移也是需求的一部分。这时我们可以使用migrate-mongo在nodejs中方便的进行数据的迁移,以下记录一下使用的过程。
一、migrate-mongo的使用
全局安装:npm install -g migrate-mongo,在项目中也可以局部安装:npm install migrate-mongo。
运行migrate-mongo,可以看到migrate-mongo支持以下的命令:
- init initialize a new migration project
- create [options] [description] create a new database migration with the provided description
- up [options] run all unapplied database migrations
- down [options] undo the last applied database migration
- status [options] print the changelog of the database
现在我们就来实践一下,来体验migrate-mongo的使用。首先我们创建一个目录,存放migration的脚本。
~/Program/javascript/node/--/learn-node/node1> mkdir migrations
~/Program/javascript/node/--/learn-node/node1> cd migration
我们的mongodb里面customer表的数据如下:
/* 1 */
{
"_id" : ObjectId("5b6d4ea91eb211b1c51600f8"),
"firstName" : "Alice",
"lastName" : "Smith",
"_class" : "com.thoughtworks.springcloud.entity.Customer"
} /* 2 */
{
"_id" : ObjectId("5b6d4ea91eb211b1c51600f9"),
"firstName" : "Bob",
"lastName" : "Smith",
"_class" : "com.thoughtworks.springcloud.entity.Customer"
}
创建migration的配置文件:migrate-mongo init,生成的config.js内容配置,其中mogodb的地址是本地,数据库名为huhx。
'use strict';
module.exports = { mongodb: {
url: 'mongodb://localhost:27017',
databaseName: "huhx", options: {
useNewUrlParser: true, // removes a deprecation warning when connecting
// connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
// socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
}
}, // The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
migrationsDir: 'migrations', // The mongodb collection where the applied changes are stored. Only edit this when really necessary.
changelogCollectionName: 'changelog',
};
然后我们创建我们的迁移脚本,在表里面加上一个更新lastname=Smith的值为hu。
~/Program/javascript/node/--/learn-node/node1/migrations> migrate-mongo create addfullnamefield
Created: migrations/-addfullnamefield.js
我们编写20180915084157-addfullnamefield.js的内容如下:
'use strict';
module.exports = {
up(db) {
// TODO write your migration here
const customerList = db.collection('customer').find({firstName: 'Alice'});
return customerList.forEach(item => {
const fullName = `${item.firstName} ${item.lastName}`;
db.collection('customer').updateOne({_id: item._id}, {$set: {fullName: fullName}}, {multi: });
});
}, down(db) {
// TODO write the statements to rollback your migration (if possible)
return db.collection('customer').updateMany({firstName: 'Alice'}, {$unset: {fullName: ''}});
}
};
此时我们可以使用migrate-mongo status命令来查看migrate的状态:

运行migrate-mongo up正式执行migration。如下所示
~/Program/javascript/node/--/learn-node/node1/migrations> migrate-mongo up
MIGRATED UP: -addfullnamefield.js
此时我们可以看到在huhx的数据库,可以看到changelog的collection。里面的内容如下:
{
"_id" : ObjectId("5b9ccd21adbd49b60990f0b8"),
"fileName" : "20180915084157-addfullnamefield.js",
"appliedAt" : ISODate("2018-09-15T09:13:05.272Z")
}
而且我们的customer中的数据,也相应的得到了迁移处理。
/* 1 */
{
"_id" : ObjectId("5b6d4ea91eb211b1c51600f8"),
"firstName" : "Alice",
"lastName" : "Smith",
"_class" : "com.thoughtworks.springcloud.entity.Customer",
"fullName" : "Alice Smith"
} /* 2 */
{
"_id" : ObjectId("5b6d4ea91eb211b1c51600f9"),
"firstName" : "Bob",
"lastName" : "Smith",
"_class" : "com.thoughtworks.springcloud.entity.Customer"
}
需要注意的是关于up的函数:
function up(db) { /* */ }should returnPromisefunction up(db, next) { /* */ }should callbacknext
如果我们要撤销修改,我们可以执行migrate-mongo downw命令,也就是执行我们定义的down()函数的内容,产生的效果是customer表中fullName字段删除了,而且查看changelog数据也是为空。再次运行migrate-mongo status查看状态:

最后我们也可以指定其它的配置文件,使用-f 参数。

如果我们的addfullnamefield.js文件改变了,想要重新migrate up(已经up过了)。做法是在数据库changelog中删除记录或者是重新写一个脚本,再跑一次migrate-mongo up命令。
友情链接
fs项目---->migrate-mongo的使用(一)的更多相关文章
- fs项目---->async/await的学习(一)
2018-07-11号,我来到了fs项目组担任后端开发的角色.这是我来thoughtworks以来首个的正式项目,不管是在技术还是在敏捷的实践中都是受益匪浅.来感受tw特殊的文化的同时,我希望自己能够 ...
- fs项目---->cron框架的学习(一)
Cron是一种允许您按计划执行某些内容的工具.这通常使用cron语法来完成.我们允许您在计划作业触发时执行函数.我们还允许您使用子进程执行javascript进程外部的作业.此外,这个库超出了基本的c ...
- mongo语句优化分析
参考原文:http://www.mongoing.com/eshu_explain3 理想的查询状态由以下两种 普通查询: nReturned=totalKeysExamined & tota ...
- 【MongoDB】MongoDB与项目搭配启动进程
项目启动/数据连接命令 (20180701成功且不用再找正确关闭mongoDB的方式) 如上图在mongoDB的bin目录的同级新建mongo.config.mongostart.bat.mongo ...
- Mongo学习记录
引子 最近做项目利用mongo记录的日志做数据统计.着了非关系型数据库的迷,于是乎买了本<MongoDB实战>学习了一番.记录一下学习笔记,共享之. 准备 我在自己的Linux服务器上装了 ...
- Ubuntu14.04下Mongodb(离线安装方式|非apt-get)安装部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 说在前面的话 首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu LTS \n \l r ...
- Ubuntu16.04下Mongodb(离线安装方式|非apt-get)安装部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 说在前面的话 首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu LTS \n \l r ...
- rails命令行命令
x.指定端口启动rails项目 ruby script/server webrick -p 3000 ------------------------------------------------- ...
- HEXO与Github.io搭建个人博客
HEXO与Github.io搭建个人博客 HEXO搭建 HEXO是基于Node.JS的一款简单快速的博客框架,能够支持多线程,支持markdown,可以将生成的静态网页发布到github.io以 ...
随机推荐
- linux tomcat 绑定域名
1.解析域名到对应的服务器ip 2.找到tomcat安装路径进入/conf 3.vi server.xml 4.修改<Connector port="8080" protoc ...
- ThinkPhp 更改 BIT 类型的问题
在使用ThinkPhp更改字段为BIT 类型的注意了,您将会遇到以下错误信息: 这是因为ThinkPhp在使用setField或Save方法时,将你的值更改成了字符串类型 而TINYINT 类型是不 ...
- Tkinter(2.x 与3.X的区别)
1.包的引入 2.X下是 from Tkinter import * 而3.x是 from tkinter import * 否则,会报找不到tkinter的错误 Traceback (most re ...
- 用户人品预测大赛--getmax队--竞赛分享
用户人品预测大赛--getmax队--竞赛分享 DataCastle运营 发表于 2016-3-24 14:49:32 533 0 0 答辩PPT
- WPF 实现阴影效果
一.WPF最常见的一个阴影效果的类是DropShadowEffect.它有几种比较有用的属性比如:Color设置颜色Direction设置投影的方向ShadowDepth设置投影距纹理下方的距离Opa ...
- Wifiner for Mac(WiFi 状况分析工具)破解版安装
1.软件简介 Wifiner 是 macOS 系统上一款 Wifi 分析工具,仅需几次点击即可对您的 Wi-Fi 网络连接进行分析和故障排除.扫描您的 Wi-Fi 网络,获取包含交互式彩色编码热 ...
- Git 安装(分布式版本控制系统)
1.在 Windows 上安装 在 Windows 上安装 Git 也有几种安装方法. 官方版本可以在 Git 官方网站下载,打开下载会自动开始.要注意这是一个名为 Git for Windows 的 ...
- 设置tomcat 编译文件位置【转】
问题: 将项目发布到tomcat时,发现tomcat的cclasses目录下无任何编译后的文件. 解决方法:设置MyEclipse的文件编译目录即可: http://my.oschina.net/u/ ...
- Atitit web remote远程调试的原理attilax总结
Atitit web remote远程调试的原理attilax总结 Jvm是vm打开一个debug port,然后ide先连接..然后执行url,就会vm会与ide沟通.. Php的xdebug po ...
- install ceph by ceph-deploy
使用阿里云源安装ceph Luminous https://liuxu.co/2017/09/19/install-ceph-Luminous-on-centos7-with-ceph-deploy/ ...