Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用mongify将两个表合并为一个表_Mysql_Mongodb_Migration_Schema_Database Migration - Fatal编程技术网

Mysql 如何使用mongify将两个表合并为一个表

Mysql 如何使用mongify将两个表合并为一个表,mysql,mongodb,migration,schema,database-migration,Mysql,Mongodb,Migration,Schema,Database Migration,我计划将我的应用程序数据库从Mysql迁移到Mongo,模式更改很少。在我的新模式中,我将两个Mysql表合并到一个Mongo集合中。我想使用mongify()gem将现有的Mysql数据填充到具有更新模式的Mongo中 如何做到这一点?mongify中有没有办法将两个Mysql表合并为一个 Mysql表 user id name nickname type user_role id role alias user_id 我在M

我计划将我的应用程序数据库从Mysql迁移到Mongo,模式更改很少。在我的新模式中,我将两个Mysql表合并到一个Mongo集合中。我想使用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
尝试左联接:(将合并表)

将该数据导出为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;

我认为它可能会帮助您

使用JOINMySQL获取数据,并将数据加载到MongoDB:

试试这个:

SELECT U.id, U.name, U.type, UR.role
FROM USER U 
INNER JOIN user_role UR ON U.id = UR.user_id;

另一种方法是,如果很难合并两个临时表,则运行一个临时表;它们很容易编程(js的两行代码),执行起来也很简单。此外,还可以应用任何需要的转换,例如转换为mongo中允许的json/bson数组,但mysql中不允许

假设mongo中有用户和用户角色集合(或表)

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执行

mongo localhost:27017/test myjsfile.js


谢谢你宝贵的回复!!!用户表有“昵称”,第二个表有“别名”字段。如果需要将字段“昵称”重命名为“别名”,并且需要将user_info.alias值放入user表中。我该怎么做?请提供您的输入…是否要替换user_role.alias column中user.nickname的所有数据在这种情况下,我建议您在case中使用子查询,例如如果alias中有数据,则使用该别名更新订单昵称。
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});

}