Posts MongoDB Hello world!
Post
Cancel

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 :

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

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

1
2
//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/

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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//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);
}

```js //backup mongoexport -d testdb -c movies -o movies.json

//restore mongoimport -d testdb -c movies2 –file movies.json ```</bsondocument></bsondocument>

origin - https://www.pipiscrew.com/?p=14907 mongodb-hello-world

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags