mongodb for c# developers - simonellistonball.com for c# developers.… · resources the mongodb c...
TRANSCRIPT
![Page 1: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/1.jpg)
MongoDB for C# DevelopersSimon Elliston Ball @sireb
Saturday, 12 October 13
![Page 3: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/3.jpg)
Document Database
Saturday, 12 October 13
![Page 4: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/4.jpg)
Document Databaseid full_name address1 John
Smith3a Test Street
2 Jane Doe 1b Fake Street3 ... ...
Saturday, 12 October 13
![Page 5: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/5.jpg)
id customer_id
order_date1 1 ... 2013-10-102 1 ... ...
3 ... ... ...
Document Databaseid full_name address1 John
Smith3a Test Street
2 Jane Doe 1b Fake Street3 ... ...
Saturday, 12 October 13
![Page 6: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/6.jpg)
id customer_id
order_date1 1 ... 2013-10-102 1 ... ...
3 ... ... ...
Document Databaseid full_name address1 John
Smith3a Test Street
2 Jane Doe 1b Fake Street3 ... ...id customer_id order_date
1 1 2013-10-102 1 ...3 ... ...
Saturday, 12 October 13
![Page 7: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/7.jpg)
id customer_id
order_date1 1 ... 2013-10-102 1 ... ...
3 ... ... ...
Document Databaseid full_name address1 John
Smith3a Test Street
2 Jane Doe 1b Fake Street3 ... ...id customer_id order_date
1 1 2013-10-102 1 ...3 ... ...
Saturday, 12 October 13
![Page 8: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/8.jpg)
id customer_id
order_date1 1 ... 2013-10-102 1 ... ...
3 ... ... ...
Document Databaseid full_name address1 John
Smith3a Test Street
2 Jane Doe 1b Fake Street3 ... ...id customer_id order_date
1 1 2013-10-102 1 ...3 ... ...
customers = [ { "_id" : ObjectId("5256b399ac46b80084974d9a"), "name" : "John Smith", "address" : "3a Test Street", "orders" [ { "order_date": "2013-10-10", "order_item": [ { "product": "Widget"...} ... ] ... }] }, { "_id" : ObjectId("5256b3a8ac46b80084974d9b"), "name" : "Jane Doe", "address" : "1b Fake Street" }]
Saturday, 12 October 13
![Page 9: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/9.jpg)
Key -> JSON
Saturday, 12 October 13
![Page 10: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/10.jpg)
Saturday, 12 October 13
![Page 11: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/11.jpg)
Saturday, 12 October 13
![Page 12: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/12.jpg)
Saturday, 12 October 13
![Page 13: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/13.jpg)
• Transactions per document
• Master-slave replication
• Many many languages: C#, JavaScript, Java, PHP, Python, Ruby,Scala, Erlang, Go, C, C++, Perl (and those are just the official ones)
• ACID, multi-document
• Master-master replication
• .NET Only
Saturday, 12 October 13
![Page 14: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/14.jpg)
Getting started with MongoDB
Download from http://www.mongodb.org/
Saturday, 12 October 13
![Page 15: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/15.jpg)
Saturday, 12 October 13
![Page 16: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/16.jpg)
Getting started with the C# client
PM> Install-Package mongocsharpdriver
Saturday, 12 October 13
![Page 17: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/17.jpg)
Saturday, 12 October 13
![Page 18: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/18.jpg)
Wooah there. I thought you said JSON...
Saturday, 12 October 13
![Page 19: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/19.jpg)
BSON Binary JSON
Saturday, 12 October 13
![Page 20: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/20.jpg)
BSON Binary JSONTyped
Saturday, 12 October 13
![Page 21: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/21.jpg)
BSON Binary JSONTyped
Serialisation library
Saturday, 12 October 13
![Page 22: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/22.jpg)
BSON Binary JSONTyped
Serialisation library
Annotate POCOs to control mappingor write config code if you must
Saturday, 12 October 13
![Page 23: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/23.jpg)
Connecting...var connectionString = "mongodb://localhost";var client = new MongoClient(connectionString);var server = client.GetServer();
Saturday, 12 October 13
![Page 24: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/24.jpg)
Connecting...
That’s it.The driver will disconnect, release objects and pool for you.
But...
var connectionString = "mongodb://localhost";var client = new MongoClient(connectionString);var server = client.GetServer();
Saturday, 12 October 13
![Page 25: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/25.jpg)
CollectionsDatabase
Collection
Document
_id
field
Saturday, 12 October 13
![Page 26: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/26.jpg)
CRUD creating new documents
var developerCollection = database.GetCollection<Developer>("team");
var Developer = new Developer(1,"Test", "Person");developerCollection.Insert(Developer);var Developer2 = new Developer(2,"Another", "Developer");developerCollection.Insert(Developer2)
var documentCollection = database.GetCollection("team");
BsonDocument document = new BsonDocument();document.Add(new BsonElement("name", "Testing")) .Add(new BsonElement("number", new BsonInt32(42)));
documentCollection.Insert(document);
The BsonDocument way:
With mapped entities:
Saturday, 12 October 13
![Page 27: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/27.jpg)
CRUD creating new documents
var documentCollection = database.GetCollection("team");
BsonDocument document = new BsonDocument();document.Add(new BsonElement("name", "Testing")) .Add(new BsonElement("number", new BsonInt32(42)));
documentCollection.Insert(document);
The BsonDocument way:
Beware of mixing your BSONsList<Developer> allDevelopers = developerResults.ToList<Developer>();
Saturday, 12 October 13
![Page 28: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/28.jpg)
CRUD creating new documentsBeware of mixing your BSONsList<Developer> allDevelopers = developerResults.ToList<Developer>();
Saturday, 12 October 13
![Page 29: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/29.jpg)
CRUD basic document readsMongoCursor<BsonDocument> documentResults = documentCollection.FindAll();MongoCursor<Developer> developerResults = developerCollection.FindAll();
Saturday, 12 October 13
![Page 30: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/30.jpg)
CRUD basic document readsMongoCursor<BsonDocument> documentResults = documentCollection.FindAll();MongoCursor<Developer> developerResults = developerCollection.FindAll();
var cursor = collection.FindAll();cursor.Skip = 100;cursor.Limit = 10;
foreach (var developer in cursor) { ...
Saturday, 12 October 13
![Page 31: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/31.jpg)
CRUD basic document readsMongoCursor<BsonDocument> documentResults = documentCollection.FindAll();MongoCursor<Developer> developerResults = developerCollection.FindAll();
var readQuery = Query<Developer>.EQ(n => n.PersonId, 2);Developer developerRead = developerCollection.FindOne(readQuery);
var cursor = collection.FindAll();cursor.Skip = 100;cursor.Limit = 10;
foreach (var developer in cursor) { ...}
Saturday, 12 October 13
![Page 32: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/32.jpg)
CRUD basic document readsMongoCursor<BsonDocument> documentResults = documentCollection.FindAll();MongoCursor<Developer> developerResults = developerCollection.FindAll();
var readQuery = Query<Developer>.EQ(n => n.PersonId, 2);Developer developerRead = developerCollection.FindOne(readQuery);
BsonDocument documentRead = documentCollection.FindOne(new QueryDocument { { "_id", documentId }});
var cursor = collection.FindAll();cursor.Skip = 100;cursor.Limit = 10;
foreach (var developer in cursor) { ...}
Saturday, 12 October 13
![Page 33: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/33.jpg)
CRUD updatedeveloperRead.LastName = "Something-Else";developerCollection.Save(developerRead);
Saturday, 12 October 13
![Page 34: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/34.jpg)
CRUD updateWrite Concerns
Only relevant with ReplicationThe number replica which need to report successful writescollection.Save(developerRead, new MongoInsertOptions { WriteConcern = WriteConcern.WMajority });
Saturday, 12 October 13
![Page 35: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/35.jpg)
CRUD updatevar update = new UpdateDocument { { "$set", new BsonDocument("LastName", "A new name") }};
var query = new QueryDocument { { "LastName", "Developer" }};
collection.Update(query, update);
NB. Only updates one document
Saturday, 12 October 13
![Page 36: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/36.jpg)
CRUD updatevar update = new UpdateDocument { { "$set", new BsonDocument("LastName", "A new name") }};
var query = new QueryDocument { { "LastName", "Developer" }};
collection.Update(query, update);
NB. Only updates one document
collection.Update(query, update, new MongoUpdateOptions{ Flags = UpdateFlags.Multi});
Applies to all documents that match query
Saturday, 12 October 13
![Page 37: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/37.jpg)
CRUD upsertvar update = new UpdateDocument { { "$set", new BsonDocument("LastName", "A new name") }};var query = Query<Developer>.EQ(d => d.PersonId, 10);
collection.Update(query, update, new MongoUpdateOptions{ Flags = UpdateFlags.Upsert});
Saturday, 12 October 13
![Page 38: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/38.jpg)
CRUD deletingvar query = new QueryDocument { { "LastName", "Person" }};collection.Remove(query);
Saturday, 12 October 13
![Page 39: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/39.jpg)
CRUD deletingvar query = new QueryDocument { { "LastName", "Person" }};collection.Remove(query);
collection.RemoveAll();
collection.Drop();
Saturday, 12 October 13
![Page 40: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/40.jpg)
LINQ integrationJust make the collection queryableusing System.Linq;using MongoDB.Driver.Linq;
var query = from e in collection.AsQueryable() where e.LastName == "Person" select e;
foreach (var developer in query){ ...
Saturday, 12 October 13
![Page 41: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/41.jpg)
GridFS in C#16MB document limitGridFS used to break documents into chunksusing (var fs = new FileStream("largeVideo.m4v", FileMode.Open)){ database.GridFS.Upload(fs, "largeVideo.m4v");}
database.GridFS.Download("test.m4v", "largeVideo.m4v");
Saturday, 12 October 13
![Page 42: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/42.jpg)
MapReduce (JavaScript)var map = "function() {" + " for (var key in this) {" + " emit(key, { count : 1 });" + " }" + "}";
var reduce = "function(key, emits) {" + " total = 0;" + " for (var i in emits) {" + " total += emits[i].count;" + " }" + " return { count : total };" + "}";
var mr = collection.MapReduce(map, reduce);
Yes, it’s a word count. Yes, it’s JavaScript.Saturday, 12 October 13
![Page 43: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/43.jpg)
Special Queries GeoNearvar query = Query.EQ("properties.amenity", new BsonString("pub")); // heredouble lon = 54.9117468;double lat = -1.3737675;
var earthRadius = 6378.0; // kmvar rangeInKm = 3000.0; // km
var options = GeoNearOptions .SetMaxDistance(rangeInKm / earthRadius /* to radians */) .SetSpherical(true);
var results = collection.GeoNear(query, lat, lon, 10, options);
foreach (var result in results.Hits) ...
Saturday, 12 October 13
![Page 44: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/44.jpg)
Acknowledgements
MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.
Saturday, 12 October 13
![Page 45: MongoDB for C# Developers - simonellistonball.com for C# Developers.… · Resources The MongoDB C Sharp Language Center: A tutorial on the driver from MongoDB themselves:](https://reader031.vdocument.in/reader031/viewer/2022022510/5adb91867f8b9afc0f8e036b/html5/thumbnails/45.jpg)
ResourcesThe MongoDB C Sharp Language Center:http://docs.mongodb.org/ecosystem/drivers/csharp/A tutorial on the driver from MongoDB themselves:http://docs.mongodb.org/ecosystem/tutorial/use-csharp-driver/#csharp-driver-tutorialSample code from this talk:https://github.com/simonellistonball/MongoForCsharpSamplesA good walkthrough on MongoDB with ASP.NET MVC:http://www.drdobbs.com/database/mongodb-with-c-deep-dive/240152181
Bonus extrasA Glimpse plugin to view mongo query details:https://github.com/simonellistonball/Glimpse.MongoDB
Saturday, 12 October 13