我计划将我的应用程序数据库从Mysql迁移到模式更改较少的Mongo。在我的新模式中,我将两个Mysql表合并到一个Mongo集合中。我想使用mongify (https://github.com/anlek/mongify) gem将现有的Mysql数据填充到具有较新模式的Mongo中。
该怎么做呢?在mongify中有没有办法将两个Mysql表合并成一个表?
Mysql表
user
id
name
nickname
type
user_role
id
role
alias
user_id在Mongo中,我将上述两个表合并为一个集合
user
id
name
type
role
alias发布于 2015-01-05 19:47:20
尝试左连接:(表将被合并)
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;将数据导出为sql格式并将其上载到mongodb
SELECT country INTO customerCountry
FROM customers
WHERE customerNumber = p_customerNumber;
CASE customerCountry -- Here you have to see if that alias data is set
WHEN 'USA' THEN
SET p_shiping = '2-day Shipping'; -- here you have to write Update Query
ELSE
SET p_shiping = '5-day Shipping';
END CASE;我想这可能会对你有帮助
发布于 2015-01-05 20:09:22
使用JOIN从MySQL获取数据,并将该数据加载到MongoDB:
试试这个:
SELECT U.id, U.name, U.type, UR.role
FROM USER U
INNER JOIN user_role UR ON U.id = UR.user_id;发布于 2015-01-11 10:01:51
如果很难临时合并两个表,那么可以选择运行mongo shell script post-hoc;它们非常容易编程(只有几行js代码),而且执行起来也很简单。另外,可以应用任何所需的转换,例如在mongo中允许转换为json/bson数组,但在mysql中不允许。
假设在mongo中有一个用户和user_role集合(或表)。
db.user_role.find({}).forEach(function(doc) {
var roles = doc.roles;
// convert mysql format here if needed.
//var roles_array = JSON.parse(roles) // or whatever?
//var alias = doc.alias;
// if alias is null empty or ??
// dont upsert to avoid orphaned user_role records
db.user.update({"_id": doc.user_id}, {"roles": roles, "alias", doc.alias }, {upsert: false, multi: false});
}然后使用mongo shell执行
测试myjsfile.js本地主机:27017/
mongo
https://stackoverflow.com/questions/27754320
复制相似问题