将 SQL 数据库(比如 1 个表)转换为 mongoDB 文档的最佳方法是什么?
我想我可以使用 C# 驱动程序并实现一个循环,它选择表中的每一行并将其保存在 Mongo 中。但是,我正在寻找一种更好的方法来转换大量数据。
最佳答案
这是我用于将数据从 SQL 服务器导入到位于我的盒子上的 Mongodb 的导入脚本。 这段代码只会在 MongoDB 中创建一个类似的表(存在于 SQL DB 中)。 您可以提供要以逗号分隔的表格列表导入,所有这些都可以毫无问题地导入。
static void Main(string[] args)
{
List<string> tablelist = new List<string>();
if (!args[0].Contains(','))
tablelist.Add(args[0]);
else
tablelist.AddRange(args[0].Split(','));
string sqlconnectionstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
var connectionString = "mongodb://localhost/?safe=true;w=1;wtimeout=30s";
var safemode = SafeMode.True;
MongoServer server = MongoServer.Create(connectionString);
MongoDatabase db = server.GetDatabase("testdb");
MongoCollection<MongoDB.Bson.BsonDocument> coll = db.GetCollection<BsonDocument>("test");
//coll.Find().Count();
int i = 0;
foreach (string table in tablelist)
{
using (SqlConnection conn = new SqlConnection(sqlconnectionstring))
{
string query = "select * from " + table;
using (SqlCommand cmd = new SqlCommand(query, conn))
{
/// Delete the MongoDb Collection first to proceed with data insertion
if (db.CollectionExists(table))
{
MongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>(table);
collection.Drop();
}
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
List<BsonDocument> bsonlist = new List<BsonDocument>(1000);
while (reader.Read())
{
if (i == 1000)
{
using (server.RequestStart(db))
{
//MongoCollection<MongoDB.Bson.BsonDocument>
coll = db.GetCollection<BsonDocument>(table);
coll.InsertBatch(bsonlist);
bsonlist.RemoveRange(0, bsonlist.Count);
}
i = 0;
}
++i;
BsonDocument bson = new BsonDocument();
for (int j = 0; j < reader.FieldCount; j++)
{
if (reader[j].GetType() == typeof(String))
bson.Add(new BsonElement(reader.GetName(j), reader[j].ToString()));
else if ((reader[j].GetType() == typeof(Int32)))
{
bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetInt32(j))));
}
else if (reader[j].GetType() == typeof(Int16))
{
bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetInt16(j))));
}
else if (reader[j].GetType() == typeof(Int64))
{
bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetInt64(j))));
}
else if (reader[j].GetType() == typeof(float))
{
bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetFloat(j))));
}
else if (reader[j].GetType() == typeof(Double))
{
bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetDouble(j))));
}
else if (reader[j].GetType() == typeof(DateTime))
{
bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetDateTime(j))));
}
else if (reader[j].GetType() == typeof(Guid))
bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetGuid(j))));
else if (reader[j].GetType() == typeof(Boolean))
{
bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetBoolean(j))));
}
else if (reader[j].GetType() == typeof(DBNull))
{
bson.Add(new BsonElement(reader.GetName(j), BsonNull.Value));
}
else if (reader[j].GetType() == typeof(Byte))
{
bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader.GetByte(j))));
}
else if (reader[j].GetType() == typeof(Byte[]))
{
bson.Add(new BsonElement(reader.GetName(j), BsonValue.Create(reader[j] as Byte[])));
}
else
throw new Exception();
}
bsonlist.Add(bson);
}
if (i > 0)
{
using (server.RequestStart(db))
{
//MongoCollection<MongoDB.Bson.BsonDocument>
coll = db.GetCollection<BsonDocument>(table);
coll.InsertBatch(bsonlist);
bsonlist.RemoveRange(0, bsonlist.Count);
}
i = 0;
}
}
}
}
}
关于c# - 将 SQL 表转换为 mongoDB 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4372630/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。
matlab打开matlab,用最简单的imread方法读取一个图像clcclearimg_h=imread('hua.jpg');返回一个数组(矩阵),往往是a*b*cunit8类型解释一下这个三维数组的意思,行数、数和层数,unit8:指数据类型,无符号八位整形,可理解为0~2^8的数三个层数分别代表RGB三个通道图像rgb最常用的是24-位实现方法,即RGB每个通道有256色阶(2^8)。基于这样的24-位RGB模型的色彩空间可以表现256×256×256≈1670万色当imshow传入了一个二维数组,它将以灰度方式绘制;可以把图像拆分为rgb三层,可以以灰度的方式观察它figure(1
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.