using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;
using MongoDB.Bson;
using System.IO;
using System.Text.RegularExpressions;
namespace csharpMongo
{
class Program
{
static void Main(string[] args)
{
string sourceFile=@"F:\FILE.csv";
StreamReader sr = new StreamReader(sourceFile);
string line = sr.ReadLine();
var columnNames = Regex.Split(line, ",");
string connectionString = "mongodb://xxx.xxx.x.xxx"; //TARGET IP ADDRESS
MongoServer mongo = MongoServer.Create(connectionString);
mongo.Connect();
var db = mongo.GetDatabase("database_name"); // TARGET DATABASE NAME
using (mongo.RequestStart(db))
{
var collection = db.GetCollection<BsonDocument >("Collection_name");
while ((line = sr.ReadLine()) != null)
{
string[] cols=Regex .Split (line ,",");
BsonDocument book = new BsonDocument();
for (int i = 0; i < columnNames.Length; i++)
{
book.Add(columnNames[i], cols[i]);
}
collection.Insert(book);
}
}
mongo.Disconnect();
}
}
}
Good job ragu!
ReplyDeleteClose the stream :D
ReplyDelete