MongoDB Hello world!

2018 – Working with MongoDB in .NET
https://www.mongodb.com/blog/post/working-with-mongodb-transactions-with-c-and-the-net-framework

2016 – Working with MongoDB in .NET
https://www.codementor.io/pmbanugo/working-with-mongodb-in-net-1-basics-g4frivcvz

1-Download MongoDB – https://www.mongodb.com/download-center/community or https://github.com/mongodb/mongo. Choose the ZIP package, download & extract the files to c:\mongodb. Files aligned as :

Server mongod.exe
Router mongos.exe
Client mongo.exe
MonitoringTools mongostat.exe, mongotop.exe
ImportExportTools mongodump.exe, mongorestore.exe, mongoexport.exe, mongoimport.exe
MiscellaneousTools bsondump.exe, mongofiles.exe, mongooplog.exe, mongoperf.exe

Make a new folder c:\mongodb\dbase. Start the server with :

 
//ref - https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-windows/
mongod.exe --dbpath C:\mongodb\dbase

2-Download .NET driver https://mongodb.github.io/mongo-csharp-driver/ or https://github.com/mongodb/mongo-csharp-driver/releases/tag/v2.7.3 (v2.7.3 is the last version supports framework v4.5.2)
3-Download Robomongo v1.3.1 https://robomongo.org/ or https://github.com/Studio3T/robomongo/releases


ref – https://docs.mongodb.com/manual/reference/default-mongodb-port/

27017 The default port for mongod and mongos instances. You can change this port with port or –port.
27018 The default port for mongod when running with –shardsvrcommand-line option or the shardsvr value for the clusterRole setting in a configuration file.
27019 The default port for mongod when running with –configsvrcommand-line option or the configsvr value for the clusterRole setting in a configuration file.

4-Add the MongoDB Driver DLLs as reference to your project and paste the following :

//FYI - MongoClient object is thread safe, so you can put it in a static field – 
//https://www.codementor.io/pmbanugo/working-with-mongodb-in-net-1-basics-g4frivcvz
IMongoDatabase db;

private void button1_Click(object sender, EventArgs e)
{
	try
	{
		var connectionString = "mongodb://localhost:27017";

		MongoClient client = new MongoClient(connectionString);
						
		db = client.GetDatabase("SignatureErrors");
	}
	catch (Exception x)
	{
		MessageBox.Show(x.Message);
	}
}

private void button2_Click(object sender, EventArgs e)
{
	var document = new BsonDocument();
	document.Add("name", "Steven Johnson");
	document.Add("age", 23);
	document.Add("subjects", new BsonArray() { "English", "Mathematics", "Physics" });

	IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("SignatureErrorsRECS");
	collection.InsertOneAsync(document);
}

By: |10/05/2019|categories: /