MongoDB Hello world!

Costas

Administrator
Staff member
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 :

Servermongod.exe
Routermongos.exe
Clientmongo.exe
MonitoringToolsmongostat.exe, mongotop.exe
ImportExportToolsmongodump.exe, mongorestore.exe, mongoexport.exe, mongoimport.exe
MiscellaneousToolsbsondump.exe, mongofiles.exe, mongooplog.exe, mongoperf.exe

[TD]

Make a new folder c:\mongodb\dbase. Start the server with :
JavaScript:
//ref - https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-windows/
mongod.exe --dbpath C:\mongodb\dbase

kyhsHNN.jpg


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

1eKfwTP.png

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

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

[TD]

JZsb9VS.png


rOiezcC.png


4-Add the MongoDB Driver DLLs as reference to your project and paste the following :
JavaScript:
//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);
}


JavaScript:
//backup
mongoexport -d testdb -c movies -o movies.json

//restore
mongoimport -d testdb -c movies2 --file movies.json

newbies tutorial - https://www.guru99.com/installation-configuration-mongodb.html
 
Top