我正在使用Mongify将MySQL数据转换为MongoDB。但是,我在MySQL表中的id
字段为primary_key
,但是在mongify之后它会覆盖默认的MongoDB _id
值,即"_id" : ObjectId("58369f006ee5c61b9400000e"),
我不想覆盖它我需要完全MySQL中primary_key
列id
的相同值为MongoDB _id
,如下所示:
MySQL id: 123
应转换为 MongoDB 为"_id" : ObjectId("123"),
这是用户表的翻译文件
table "users" do
column "id", :key, :as => :integer
column "username", :string
column "password", :string
column "email", :string
column "first_name", :string
column "last_name", :string
column "dob", :date
column "gender", :boolean
column "status", :integer
column "created", :datetime
column "modified", :datetime
end
提前致谢。
答案 0 :(得分:0)
我刚刚找到了Mongify docs
中已有的解决方法添加此
users
因此table "users" do
before_save do |row|
row._id = row.delete('pre_mongified_id')
end
column "id", :key, :as => :integer
column "username", :string
column "password", :string
column "email", :string
column "first_name", :string
column "last_name", :string
column "dob", :date
column "gender", :boolean
column "status", :integer
column "created", :datetime
column "modified", :datetime
end
表的完整转换脚本看起来像
<?php
$ts= [1.31, 1.32, 1.33, 1.34, 1.35, 1.36, 1.37, 1.38, 1.39, 1.40];
array_walk($ts, function($t){echo number_format($t, 2)."\t".number_format(0.05*round($t*20), 2)."\n";});
非常感谢。