I want to migrate my tables from SQL Server to MongoDB. I use below code and it is working fine:
string strQueryInline = "select * from "+ddl_table.SelectedValue +"";
SqlDataAdapter adpt = new SqlDataAdapter(strQueryInline, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
var collection = db.GetCollection<BsonDocument>(txt_value.Text);
try
{
foreach (DataRow dr in dt.Rows)
{
BsonDocument bson = new BsonDocument();
for (int i = 0; i < dr.ItemArray.Count(); i++)
{
bson.Add(dr.Table.Columns[i].ColumnName, dr[i].ToString());
}
collection.Insert(bson);
}
}
But the problem is that it is taking same name and column name as in SQL Server. But I want to use a different name for the table and some different names for the fields in the mongo collection.. I searched a few links but I am not able to reach the exact solution...
plz suggest something..
GetCollection({name}). That controls the collection name. Map the column names to new field names in your loop andinsert.