SlideShare une entreprise Scribd logo
1  sur  45
Télécharger pour lire hors ligne
MongoDB for C# Developers
Simon Elliston Ball
Head of Big Data
!

@sireb
!
!
!
!
http://www.mongodb.org/
Document Database
id

full_name

address

1

John Smith

3a Test Street

2

Jane Doe

1b Fake Street

id

customer_id
...

1

3

1

1

2
customer_i
d3
1

2

1

...

3

...

...

id

1
......

... order_date
2013-10-10

order_date ...
2013-10-1 ...
0
...
...

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"
}
]
Key -> JSON
Document Database - Why?
Flexible Data Model - No Schema
Rapid Development
Scalability
Document Database - When?
Blogs
Document Database - When?
Content management
When read patterns are fixed
Scaling requirements are unknown
When write speed matters
•

Transactions per document

•

ACID, multi-document

•

Master-slave replication

•

But… Background Indexing

•

Many many languages: C#,

•

Master-master replication

JavaScript, Java, PHP, Python, Ruby,

•

.NET Only

Scala, Erlang, Go, C, C++, Perl (and those are just
the official ones)
•

Main interface: CLI

•

GUI Client

•

Transactions per document

•

Native JSON

•

Master-slave replication

•

Geo-aware replication

•

Many many languages: C#,

•

REST Interface, so anything

JavaScript, Java, PHP, Python, Ruby,

•

View based queries

Scala, Erlang, Go, C, C++, Perl (and those are just
the official ones)
Getting started with MongoDB
Download from http://www.mongodb.org/
Getting started with the C# client
PM> Install-Package mongocsharpdriver
Wooah there.
I thought you said JSON...
BSON Binary JSON
Typed
Serialisation library
Annotate POCOs to control mapping
or write config code if you must
Connecting...
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();

That’s it.
The driver will disconnect,
release objects and pool for you.
!

But...
Collections
Database
Collection
Document
_id
field…
CRUD creating new documents
With mapped entities:
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)

The BsonDocument way:
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);
CRUD creating new documents
The BsonDocument way:
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);

Beware of mixing your BSONs
List<Developer> allDevelopers = developerResults.ToList<Developer>();
CRUD creating new documents
Beware of mixing your BSONs
List<Developer> allDevelopers = developerResults.ToList<Developer>();
CRUD basic document reads
MongoCursor<BsonDocument> documentResults = documentCollection.FindAll();
MongoCursor<Developer> developerResults = developerCollection.FindAll();
CRUD basic document reads
MongoCursor<BsonDocument> documentResults = documentCollection.FindAll();
MongoCursor<Developer> developerResults = developerCollection.FindAll();
var cursor = collection.FindAll();
cursor.Skip = 100;
cursor.Limit = 10;
!

foreach (var developer in cursor) {
...
CRUD basic document reads
MongoCursor<BsonDocument> documentResults = documentCollection.FindAll();
MongoCursor<Developer> developerResults = developerCollection.FindAll();
var cursor = collection.FindAll();
cursor.Skip = 100;
cursor.Limit = 10;
!

foreach (var developer in cursor) {
...
}

var readQuery = Query<Developer>.EQ(n => n.PersonId, 2);
Developer developerRead = developerCollection.FindOne(readQuery);
CRUD basic document reads
MongoCursor<BsonDocument> documentResults = documentCollection.FindAll();
MongoCursor<Developer> developerResults = developerCollection.FindAll();
var cursor = collection.FindAll();
cursor.Skip = 100;
cursor.Limit = 10;
!

foreach (var developer in cursor) {
...
}

var readQuery = Query<Developer>.EQ(n => n.PersonId, 2);
Developer developerRead = developerCollection.FindOne(readQuery);
BsonDocument documentRead = documentCollection.FindOne(new QueryDocument {
{ "_id", documentId }
});
CRUD update
developerRead.LastName = "Something-Else";
developerCollection.Save(developerRead);
CRUD update
Write Concerns
Only relevant with Replication
The number of replicas which need to report successful writes
collection.Save(developerRead, new MongoInsertOptions
{
WriteConcern = WriteConcern.WMajority
}
);
Write concerns - J = 0, W = 0
Master

Log

Database

Replica 1

Replica 2
Write concerns - J = 1, W = 0
Master

Log

Database

Replica 1

Replica 2
Write concerns - J = 1, W = 1
Master

Log

Database

Replica 1

Replica 2
Write concerns - J = 1, W = Majority
Master

Log

Database

Replica 1

Replica 2
CRUD update
var 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
CRUD update
var 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
CRUD upsert
var 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
});
CRUD deleting
var query = new QueryDocument {
{ "LastName", "Person" }
};
collection.Remove(query);

collection.RemoveAll();
collection.Drop();
LINQ integration
Just make the collection queryable
using System.Linq;
using MongoDB.Driver.Linq;
!

var query =
from e in collection.AsQueryable()
where e.LastName == "Person"
select e;
!

foreach (var developer in query){
...
GridFS in C#
16MB document limit
GridFS used to break documents into chunks
using (var fs = new FileStream("largeVideo.m4v", FileMode.Open))
{
database.GridFS.Upload(fs, "largeVideo.m4v");
}
database.GridFS.Download("test.m4v", "largeVideo.m4v");
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.
Special Queries GeoNear
var query = Query.EQ("properties.amenity", new BsonString("pub"));
// here
double lon = 54.9117468;
double lat = -1.3737675;
!

var earthRadius = 6378.0; // km
var 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)
...
Aggregation Framework
Pipeline based
Chained operators
$project
$match
$limit
$skip
$unwind
$group
$sort
… http://docs.mongodb.org/manual/reference/operator/aggregation/
Aggregation Framework
Demo
Summary
Document databases are simple
BSON annotation makes POCO mapping is easy
CRUD is straight forward
You can use LINQ
Hard stuff is possible
Acknowledgements
MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.
Resources
The 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-tutorial

Sample code from this talk:

https://github.com/simonellistonball/MongoForCsharpSamples

A good walkthrough on MongoDB with ASP.NET MVC:

http://www.drdobbs.com/database/mongodb-with-c-deep-dive/240152181


Bonus extras
A Glimpse plugin to view mongo query details:

https://github.com/simonellistonball/Glimpse.MongoDB
Questions?
Simon Elliston Ball
simon.ellistonball@red-gate.com
!

@sireb
!
!
!

http://bit.ly/MongoForCsharp

Contenu connexe

Tendances

Mythbusting: Understanding How We Measure the Performance of MongoDB
Mythbusting: Understanding How We Measure the Performance of MongoDBMythbusting: Understanding How We Measure the Performance of MongoDB
Mythbusting: Understanding How We Measure the Performance of MongoDBMongoDB
 
Paris js extensions
Paris js extensionsParis js extensions
Paris js extensionserwanl
 
はじめてのMongoDB
はじめてのMongoDBはじめてのMongoDB
はじめてのMongoDBTakahiro Inoue
 
The Ring programming language version 1.7 book - Part 47 of 196
The Ring programming language version 1.7 book - Part 47 of 196The Ring programming language version 1.7 book - Part 47 of 196
The Ring programming language version 1.7 book - Part 47 of 196Mahmoud Samir Fayed
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...MongoDB
 
20110514 mongo dbチューニング
20110514 mongo dbチューニング20110514 mongo dbチューニング
20110514 mongo dbチューニングYuichi Matsuo
 
BDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
BDD - Behavior Driven Development Webapps mit Groovy Spock und GebBDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
BDD - Behavior Driven Development Webapps mit Groovy Spock und GebChristian Baranowski
 
MongoDB Performance Debugging
MongoDB Performance DebuggingMongoDB Performance Debugging
MongoDB Performance DebuggingMongoDB
 
Nodejs mongoose
Nodejs mongooseNodejs mongoose
Nodejs mongooseFin Chen
 
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB ApplicationBack to Basics: My First MongoDB Application
Back to Basics: My First MongoDB ApplicationMongoDB
 
MySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of ThingsMySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of ThingsAlexander Rubin
 
Getting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJSGetting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJSMongoDB
 
File System Operations
File System OperationsFile System Operations
File System OperationsG.C Reddy
 
Mongoose and MongoDB 101
Mongoose and MongoDB 101Mongoose and MongoDB 101
Mongoose and MongoDB 101Will Button
 
HeadCouch - CouchDB PHP Client
HeadCouch - CouchDB PHP ClientHeadCouch - CouchDB PHP Client
HeadCouch - CouchDB PHP ClientDimitar Ivanov
 
MongoDB全機能解説2
MongoDB全機能解説2MongoDB全機能解説2
MongoDB全機能解説2Takahiro Inoue
 

Tendances (20)

Mongo db presentation
Mongo db presentationMongo db presentation
Mongo db presentation
 
Couchdb w Ruby'm
Couchdb w Ruby'mCouchdb w Ruby'm
Couchdb w Ruby'm
 
Mythbusting: Understanding How We Measure the Performance of MongoDB
Mythbusting: Understanding How We Measure the Performance of MongoDBMythbusting: Understanding How We Measure the Performance of MongoDB
Mythbusting: Understanding How We Measure the Performance of MongoDB
 
Paris js extensions
Paris js extensionsParis js extensions
Paris js extensions
 
はじめてのMongoDB
はじめてのMongoDBはじめてのMongoDB
はじめてのMongoDB
 
The Ring programming language version 1.7 book - Part 47 of 196
The Ring programming language version 1.7 book - Part 47 of 196The Ring programming language version 1.7 book - Part 47 of 196
The Ring programming language version 1.7 book - Part 47 of 196
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
 
20110514 mongo dbチューニング
20110514 mongo dbチューニング20110514 mongo dbチューニング
20110514 mongo dbチューニング
 
BDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
BDD - Behavior Driven Development Webapps mit Groovy Spock und GebBDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
BDD - Behavior Driven Development Webapps mit Groovy Spock und Geb
 
MongoDB Performance Debugging
MongoDB Performance DebuggingMongoDB Performance Debugging
MongoDB Performance Debugging
 
Nodejs mongoose
Nodejs mongooseNodejs mongoose
Nodejs mongoose
 
MongoDB
MongoDBMongoDB
MongoDB
 
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB ApplicationBack to Basics: My First MongoDB Application
Back to Basics: My First MongoDB Application
 
Elastic search 검색
Elastic search 검색Elastic search 검색
Elastic search 검색
 
MySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of ThingsMySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of Things
 
Getting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJSGetting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJS
 
File System Operations
File System OperationsFile System Operations
File System Operations
 
Mongoose and MongoDB 101
Mongoose and MongoDB 101Mongoose and MongoDB 101
Mongoose and MongoDB 101
 
HeadCouch - CouchDB PHP Client
HeadCouch - CouchDB PHP ClientHeadCouch - CouchDB PHP Client
HeadCouch - CouchDB PHP Client
 
MongoDB全機能解説2
MongoDB全機能解説2MongoDB全機能解説2
MongoDB全機能解説2
 

Similaire à Mongo db for c# developers

Mongo db basic installation
Mongo db basic installationMongo db basic installation
Mongo db basic installationKishor Parkhe
 
Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.Andrii Lashchenko
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBMongoDB
 
Back to Basics Webinar 2 - Your First MongoDB Application
Back to  Basics Webinar 2 - Your First MongoDB ApplicationBack to  Basics Webinar 2 - Your First MongoDB Application
Back to Basics Webinar 2 - Your First MongoDB ApplicationJoe Drumgoole
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationMongoDB
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsMongoDB
 
Back to Basics, webinar 2: La tua prima applicazione MongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDBBack to Basics, webinar 2: La tua prima applicazione MongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDBMongoDB
 
Simplifying Persistence for Java and MongoDB with Morphia
Simplifying Persistence for Java and MongoDB with MorphiaSimplifying Persistence for Java and MongoDB with Morphia
Simplifying Persistence for Java and MongoDB with MorphiaMongoDB
 
Webinar: Simplifying Persistence for Java and MongoDB
Webinar: Simplifying Persistence for Java and MongoDBWebinar: Simplifying Persistence for Java and MongoDB
Webinar: Simplifying Persistence for Java and MongoDBMongoDB
 
Webinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and JavaWebinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and JavaMongoDB
 
Taking Web Apps Offline
Taking Web Apps OfflineTaking Web Apps Offline
Taking Web Apps OfflinePedro Morais
 
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...MongoDB
 
Building Your First MongoDB Application (Mongo Austin)
Building Your First MongoDB Application (Mongo Austin)Building Your First MongoDB Application (Mongo Austin)
Building Your First MongoDB Application (Mongo Austin)MongoDB
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksMongoDB
 
Mongoid in the real world
Mongoid in the real worldMongoid in the real world
Mongoid in the real worldKevin Faustino
 
MongoD Essentials
MongoD EssentialsMongoD Essentials
MongoD Essentialszahid-mian
 
Building Your First MongoDB App
Building Your First MongoDB AppBuilding Your First MongoDB App
Building Your First MongoDB AppHenrik Ingo
 

Similaire à Mongo db for c# developers (20)

Mongo db basic installation
Mongo db basic installationMongo db basic installation
Mongo db basic installation
 
MongoDB With Style
MongoDB With StyleMongoDB With Style
MongoDB With Style
 
Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
Back to Basics Webinar 2 - Your First MongoDB Application
Back to  Basics Webinar 2 - Your First MongoDB ApplicationBack to  Basics Webinar 2 - Your First MongoDB Application
Back to Basics Webinar 2 - Your First MongoDB Application
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
 
Back to Basics, webinar 2: La tua prima applicazione MongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDBBack to Basics, webinar 2: La tua prima applicazione MongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDB
 
Simplifying Persistence for Java and MongoDB with Morphia
Simplifying Persistence for Java and MongoDB with MorphiaSimplifying Persistence for Java and MongoDB with Morphia
Simplifying Persistence for Java and MongoDB with Morphia
 
Webinar: Simplifying Persistence for Java and MongoDB
Webinar: Simplifying Persistence for Java and MongoDBWebinar: Simplifying Persistence for Java and MongoDB
Webinar: Simplifying Persistence for Java and MongoDB
 
Webinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and JavaWebinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and Java
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Taking Web Apps Offline
Taking Web Apps OfflineTaking Web Apps Offline
Taking Web Apps Offline
 
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
 
Building Your First MongoDB Application (Mongo Austin)
Building Your First MongoDB Application (Mongo Austin)Building Your First MongoDB Application (Mongo Austin)
Building Your First MongoDB Application (Mongo Austin)
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New Tricks
 
Mongoid in the real world
Mongoid in the real worldMongoid in the real world
Mongoid in the real world
 
MongoD Essentials
MongoD EssentialsMongoD Essentials
MongoD Essentials
 
Building Your First MongoDB App
Building Your First MongoDB AppBuilding Your First MongoDB App
Building Your First MongoDB App
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 

Plus de Simon Elliston Ball

A streaming architecture for Cyber Security - Apache Metron
A streaming architecture for Cyber Security - Apache MetronA streaming architecture for Cyber Security - Apache Metron
A streaming architecture for Cyber Security - Apache MetronSimon Elliston Ball
 
mcubed london - data science at the edge
mcubed london - data science at the edgemcubed london - data science at the edge
mcubed london - data science at the edgeSimon Elliston Ball
 
When to no sql and when to know sql javaone
When to no sql and when to know sql   javaoneWhen to no sql and when to know sql   javaone
When to no sql and when to know sql javaoneSimon Elliston Ball
 
Machine learning without the PhD - azure ml
Machine learning without the PhD - azure mlMachine learning without the PhD - azure ml
Machine learning without the PhD - azure mlSimon Elliston Ball
 
Why Hadoop and SQL just want to be friends - lightning talk NoSQL Matters Dub...
Why Hadoop and SQL just want to be friends - lightning talk NoSQL Matters Dub...Why Hadoop and SQL just want to be friends - lightning talk NoSQL Matters Dub...
Why Hadoop and SQL just want to be friends - lightning talk NoSQL Matters Dub...Simon Elliston Ball
 
Getting your Big Data on with HDInsight
Getting your Big Data on with HDInsightGetting your Big Data on with HDInsight
Getting your Big Data on with HDInsightSimon Elliston Ball
 
Riding the Elephant - Hadoop 2.0
Riding the Elephant - Hadoop 2.0Riding the Elephant - Hadoop 2.0
Riding the Elephant - Hadoop 2.0Simon Elliston Ball
 
Riding the Elephant - Hadoop 2.0
Riding the Elephant - Hadoop 2.0Riding the Elephant - Hadoop 2.0
Riding the Elephant - Hadoop 2.0Simon Elliston Ball
 
Finding and Using Big Data in your business
Finding and Using Big Data in your businessFinding and Using Big Data in your business
Finding and Using Big Data in your businessSimon Elliston Ball
 
When to NoSQL and when to know SQL
When to NoSQL and when to know SQLWhen to NoSQL and when to know SQL
When to NoSQL and when to know SQLSimon Elliston Ball
 

Plus de Simon Elliston Ball (10)

A streaming architecture for Cyber Security - Apache Metron
A streaming architecture for Cyber Security - Apache MetronA streaming architecture for Cyber Security - Apache Metron
A streaming architecture for Cyber Security - Apache Metron
 
mcubed london - data science at the edge
mcubed london - data science at the edgemcubed london - data science at the edge
mcubed london - data science at the edge
 
When to no sql and when to know sql javaone
When to no sql and when to know sql   javaoneWhen to no sql and when to know sql   javaone
When to no sql and when to know sql javaone
 
Machine learning without the PhD - azure ml
Machine learning without the PhD - azure mlMachine learning without the PhD - azure ml
Machine learning without the PhD - azure ml
 
Why Hadoop and SQL just want to be friends - lightning talk NoSQL Matters Dub...
Why Hadoop and SQL just want to be friends - lightning talk NoSQL Matters Dub...Why Hadoop and SQL just want to be friends - lightning talk NoSQL Matters Dub...
Why Hadoop and SQL just want to be friends - lightning talk NoSQL Matters Dub...
 
Getting your Big Data on with HDInsight
Getting your Big Data on with HDInsightGetting your Big Data on with HDInsight
Getting your Big Data on with HDInsight
 
Riding the Elephant - Hadoop 2.0
Riding the Elephant - Hadoop 2.0Riding the Elephant - Hadoop 2.0
Riding the Elephant - Hadoop 2.0
 
Riding the Elephant - Hadoop 2.0
Riding the Elephant - Hadoop 2.0Riding the Elephant - Hadoop 2.0
Riding the Elephant - Hadoop 2.0
 
Finding and Using Big Data in your business
Finding and Using Big Data in your businessFinding and Using Big Data in your business
Finding and Using Big Data in your business
 
When to NoSQL and when to know SQL
When to NoSQL and when to know SQLWhen to NoSQL and when to know SQL
When to NoSQL and when to know SQL
 

Dernier

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 

Dernier (20)

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 

Mongo db for c# developers

  • 1. MongoDB for C# Developers Simon Elliston Ball Head of Big Data ! @sireb ! ! ! !
  • 3. Document Database id full_name address 1 John Smith 3a Test Street 2 Jane Doe 1b Fake Street id customer_id ... 1 3 1 1 2 customer_i d3 1 2 1 ... 3 ... ... id 1 ...... ... order_date 2013-10-10 order_date ... 2013-10-1 ... 0 ... ... 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" } ]
  • 5. Document Database - Why? Flexible Data Model - No Schema Rapid Development Scalability
  • 6. Document Database - When? Blogs
  • 7. Document Database - When? Content management When read patterns are fixed Scaling requirements are unknown When write speed matters
  • 8.
  • 9. • Transactions per document • ACID, multi-document • Master-slave replication • But… Background Indexing • Many many languages: C#, • Master-master replication JavaScript, Java, PHP, Python, Ruby, • .NET Only Scala, Erlang, Go, C, C++, Perl (and those are just the official ones)
  • 10. • Main interface: CLI • GUI Client • Transactions per document • Native JSON • Master-slave replication • Geo-aware replication • Many many languages: C#, • REST Interface, so anything JavaScript, Java, PHP, Python, Ruby, • View based queries Scala, Erlang, Go, C, C++, Perl (and those are just the official ones)
  • 11. Getting started with MongoDB Download from http://www.mongodb.org/
  • 12.
  • 13. Getting started with the C# client PM> Install-Package mongocsharpdriver
  • 14.
  • 15. Wooah there. I thought you said JSON...
  • 16. BSON Binary JSON Typed Serialisation library Annotate POCOs to control mapping or write config code if you must
  • 17. Connecting... var connectionString = "mongodb://localhost"; var client = new MongoClient(connectionString); var server = client.GetServer(); That’s it. The driver will disconnect, release objects and pool for you. ! But...
  • 19. CRUD creating new documents With mapped entities: 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) The BsonDocument way: 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);
  • 20. CRUD creating new documents The BsonDocument way: 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); Beware of mixing your BSONs List<Developer> allDevelopers = developerResults.ToList<Developer>();
  • 21. CRUD creating new documents Beware of mixing your BSONs List<Developer> allDevelopers = developerResults.ToList<Developer>();
  • 22. CRUD basic document reads MongoCursor<BsonDocument> documentResults = documentCollection.FindAll(); MongoCursor<Developer> developerResults = developerCollection.FindAll();
  • 23. CRUD basic document reads MongoCursor<BsonDocument> documentResults = documentCollection.FindAll(); MongoCursor<Developer> developerResults = developerCollection.FindAll(); var cursor = collection.FindAll(); cursor.Skip = 100; cursor.Limit = 10; ! foreach (var developer in cursor) { ...
  • 24. CRUD basic document reads MongoCursor<BsonDocument> documentResults = documentCollection.FindAll(); MongoCursor<Developer> developerResults = developerCollection.FindAll(); var cursor = collection.FindAll(); cursor.Skip = 100; cursor.Limit = 10; ! foreach (var developer in cursor) { ... } var readQuery = Query<Developer>.EQ(n => n.PersonId, 2); Developer developerRead = developerCollection.FindOne(readQuery);
  • 25. CRUD basic document reads MongoCursor<BsonDocument> documentResults = documentCollection.FindAll(); MongoCursor<Developer> developerResults = developerCollection.FindAll(); var cursor = collection.FindAll(); cursor.Skip = 100; cursor.Limit = 10; ! foreach (var developer in cursor) { ... } var readQuery = Query<Developer>.EQ(n => n.PersonId, 2); Developer developerRead = developerCollection.FindOne(readQuery); BsonDocument documentRead = documentCollection.FindOne(new QueryDocument { { "_id", documentId } });
  • 26. CRUD update developerRead.LastName = "Something-Else"; developerCollection.Save(developerRead);
  • 27. CRUD update Write Concerns Only relevant with Replication The number of replicas which need to report successful writes collection.Save(developerRead, new MongoInsertOptions { WriteConcern = WriteConcern.WMajority } );
  • 28. Write concerns - J = 0, W = 0 Master Log Database Replica 1 Replica 2
  • 29. Write concerns - J = 1, W = 0 Master Log Database Replica 1 Replica 2
  • 30. Write concerns - J = 1, W = 1 Master Log Database Replica 1 Replica 2
  • 31. Write concerns - J = 1, W = Majority Master Log Database Replica 1 Replica 2
  • 32. CRUD update var 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
  • 33. CRUD update var 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
  • 34. CRUD upsert var 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 });
  • 35. CRUD deleting var query = new QueryDocument { { "LastName", "Person" } }; collection.Remove(query); collection.RemoveAll(); collection.Drop();
  • 36. LINQ integration Just make the collection queryable using System.Linq; using MongoDB.Driver.Linq; ! var query = from e in collection.AsQueryable() where e.LastName == "Person" select e; ! foreach (var developer in query){ ...
  • 37. GridFS in C# 16MB document limit GridFS used to break documents into chunks using (var fs = new FileStream("largeVideo.m4v", FileMode.Open)) { database.GridFS.Upload(fs, "largeVideo.m4v"); } database.GridFS.Download("test.m4v", "largeVideo.m4v");
  • 38. 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.
  • 39. Special Queries GeoNear var query = Query.EQ("properties.amenity", new BsonString("pub")); // here double lon = 54.9117468; double lat = -1.3737675; ! var earthRadius = 6378.0; // km var 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) ...
  • 40. Aggregation Framework Pipeline based Chained operators $project $match $limit $skip $unwind $group $sort … http://docs.mongodb.org/manual/reference/operator/aggregation/
  • 42. Summary Document databases are simple BSON annotation makes POCO mapping is easy CRUD is straight forward You can use LINQ Hard stuff is possible
  • 43. Acknowledgements MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.
  • 44. Resources The 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-tutorial Sample code from this talk: https://github.com/simonellistonball/MongoForCsharpSamples A good walkthrough on MongoDB with ASP.NET MVC: http://www.drdobbs.com/database/mongodb-with-c-deep-dive/240152181 Bonus extras A Glimpse plugin to view mongo query details: https://github.com/simonellistonball/Glimpse.MongoDB