SlideShare uma empresa Scribd logo
1 de 45
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais 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
 

Mais 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
 

Último

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 

Último (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

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