how to connect mongodb in linux using c# .net in windows

242 views
Skip to first unread message

manohar

unread,
May 31, 2011, 4:53:33 AM5/31/11
to mongodb-user
Dear my programmers

how to connect mongodb database in linux (means insert and delete with
documents)
through c#.net in windows OS (accessing dll(drivers))

i tried but it is not working
code written in c# it is windows OS



string connectionString = "mongodb://192.168.1.181:27017";//(linux ip)
var server = MongoServer.Create(connectionString);
if (server.State == MongoServerState.Disconnected)
server.Connect();
var OneConsoleDB = server.GetDatabase("test");
if (!OneConsoleDB.CollectionExists("test1"))
OneConsoleDB.CreateCollection("test1", null);
var NextMessages = OneConsoleDB.GetCollection("test1");
server.Disconnect();
Console.WriteLine("Server get connected in to this" +
connectionString + "Server");
server.GetDatabaseNames();
server.Disconnect();
Console.ReadLine();

Regards
Manohar


Robert Stam

unread,
May 31, 2011, 11:10:11 AM5/31/11
to mongodb-user
What do you mean by "not working"? Are you getting an exception? Is
so, which one?

You can simplify your code quite a bit:

- no need to call Connect, the driver connects automatically
- no need to call CreateCollection, collections are created
automatically when the first document is inserted
- no need to call Disconnect (in fact it's harmful, as it interferes
with connection pooling)

Once you have a collection object (as returned by GetCollection) you
probably want to call methods like Insert, Save, and Find.
Reply all
Reply to author
Forward
0 new messages