using mongodb with the .net framework

24
+ Using MongoDB on Windows with the .Net Framework and C#

Upload: stefano-paluello

Post on 14-May-2015

5.913 views

Category:

Technology


3 download

DESCRIPTION

Connect to a MongoDB database using the official C# driver from 10gen

TRANSCRIPT

Page 1: Using MongoDB with the .Net Framework

+

Using MongoDB on Windows with the .Net Framework and C#

Page 2: Using MongoDB with the .Net Framework

Let me introduce myself…

Stefano Paluello All around geek @

Barcrest Group & Pastesoft

[email protected] stefanopaluello.wordpress.co

m Twitter: @palutz http://www.pastesoft.com

Page 3: Using MongoDB with the .Net Framework

What is MongoDB?

It’s an open source document oriented database: Full index support Replication & High Availability Scalable (auto-sharding) High performance (written in C++, atomic

update,…) Rich query support Map/Reduce GridFS (store files without problem) Commercial support

Page 4: Using MongoDB with the .Net Framework

Setting up MongoDB on Windows

Download your version (better the 64bit Production release) from the official website: http://www.mongodb.org/downloads

Page 5: Using MongoDB with the .Net Framework

Running MongoDB server…

Best place where to look at (Windows quickstart): http://

www.mongodb.org/display/DOCS/Quickstart+Windows

Unzip, create log and data dir, run it

Page 6: Using MongoDB with the .Net Framework

Ok. Are we running?

Let’s connect to the shell and check it $mongodir\bin\mongo.exe

Page 7: Using MongoDB with the .Net Framework

MongoDB can run as service

Official guide: http://

www.mongodb.org/display/DOCS/Windows+Service

Easy way: $mongodir\bin\mongod –install

Let’s add some “flavour”: $mongodir\bin\mongod --logpath .\log\logs --

logappend --dbpath .\data –directoryperdb –install To manage it, NET START mongos, NET STOP

mongos

Page 8: Using MongoDB with the .Net Framework

MongoDB C#/.Net driver Official, high performance, fully featured

C# driver supported by 10gen Now at version 1.2 Built and tested against MongoDB 1.8.3 e

2.0.0 on : Visual Studio 2008, Visual Studio 2010 MonoDevelop 2.6 (Mono 2.10)

It can be downloaded from github.com: http://github.com/mongodb/mongo-csharp-driver

NuGet: PM> Install-Package mongocsharpdriver http://www.nuget.org/List/Packages/mongocsharpdriver

Page 9: Using MongoDB with the .Net Framework

MongoDB C#/.Net driver on NuGet

Page 10: Using MongoDB with the .Net Framework

MongoDB C#/.Net driver

Two main (separated) assemblies: MongoDB.Bson.dll MongoDB.Driver.dll (you can use other drivers)

Namespaces (required) MongoDB.Bson; MongoDB.Driver;

Namespace (additional) MongoDB.Bson.IO,

MongoDB.Bson.Serialization; MongoDB.Bson.Serialization.*, MongoDB.Driver.GridFs

Page 11: Using MongoDB with the .Net Framework

BSON Namespace Represent the BSON Object Model A set of classes that handle all the specification of

BSON documents (I/O, serialization, in-memory object model)

Main classes: BsonDocument BsonElement BsonValue (abstract class), with BsonType enum property

BsonArray BsonDateTime BsonDocument BsonInt32 BsonObjectId BsonString

Page 12: Using MongoDB with the .Net Framework

BSON Object Model

BsonDocument is the main class BsonDocument is a collection of elements

(BsonElements ) BsonElement is a name/value pair where

the value is a BsonValue (with its own BsonType)

The BsonElement class is seldom used directly cause it’s created as needed implicitly: document.Add(new BsonElement(“Name”, “Stefano”)); document.Add(“Name”, “Stefano”));

Page 13: Using MongoDB with the .Net Framework

MongoDB.Driver

Main classes (all thread-safe): MongoServer MongoDatabase MongoCollection<TDocument> MongoCursor<Tdocument> (only when

frozen) MongoServer srv = MongoServer.Create();

Every URL has a MongoServer instance (different Create calls with the same URL return with the same MongoServer instance

NOT put in the session state (NOT serializable)

Page 14: Using MongoDB with the .Net Framework

MongoDB.Driver

MongoServer srv = MongoServer.Create(); Every URL has a MongoServer instance different Create() calls with the same URL

return with the same MongoServer instance Do NOT put in the session state (NOT

serializable) The driver manage a Connection Pools (one

Connection Pools for every server) The connections are shared with all threads

Page 15: Using MongoDB with the .Net Framework

MongoDB Driver

MongoCollection : The class manages a collection of

BsonDocument in a MongoDB database

MongoCollection<TDefaultDocument>: Manage a collection of document, with the

default type specified by the TDefaultDocument parameter

GetCollection

Page 16: Using MongoDB with the .Net Framework

MongoCollection class

CRUD with Mongo – Query a collection FindOne and FindOneAs<T> IMongoQuery objects (QueryComplete) Find(query)

Page 17: Using MongoDB with the .Net Framework

MongoCollection class

CRUD with MongoDB - Insert There are many ways to insert data into a

MongoDB collection (using the BsonDocument or any object that can be serialized

Page 18: Using MongoDB with the .Net Framework

MongoCollection class

CRUD with MongoDB – Update and Save There are many ways to update a document

Page 19: Using MongoDB with the .Net Framework

MongoCollection class

CRUD with MongoDB – Delete There are many ways to delete a Document

Page 20: Using MongoDB with the .Net Framework

Let’s play a bit with the code…

Page 21: Using MongoDB with the .Net Framework

Useful (IMHO) tools

Page 22: Using MongoDB with the .Net Framework

MongoVue (www.mongovue.com)

Page 23: Using MongoDB with the .Net Framework

Chrome Webstore – Mongo Live

Page 24: Using MongoDB with the .Net Framework

Another code demo

Get dynamics with C#