mongodb and using mongodb with .net

32
MongoDB and using MongoDB with .NET Welcome to the JSON-stores world Telerik Software Academy http://academy.telerik.com Databases

Upload: matthew-randall

Post on 03-Jan-2016

131 views

Category:

Documents


2 download

DESCRIPTION

MongoDB and using MongoDB with .NET. Welcome to the JSON-stores world. Telerik Software Academy. http://academy.telerik.com. Databases. Table of Contents. MongoDB overview Installation and drivers Structure and documents Hosting locally MongoDB DB Viewers Console CLI, UMongo, Mongo VUE - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: MongoDB and using MongoDB with .NET

MongoDB and using MongoDB with .NET

Welcome to the JSON-stores world

Telerik Software Academyhttp://academy.telerik.com

Databases

Page 2: MongoDB and using MongoDB with .NET

Table of Contents MongoDB overview

Installation and drivers Structure and documents

Hosting locally MongoDB DB Viewers

Console CLI, UMongo, Mongo VUE Queries

Page 3: MongoDB and using MongoDB with .NET

MongoDB

Page 4: MongoDB and using MongoDB with .NET

MongoDB

MongoDB is an open-source document store database Save JSON-style objects with

dynamic schemas

Support for indices

Has document-based queries CRUD operations

Page 5: MongoDB and using MongoDB with .NET

Using MongoDB Download MongoDB from the official web site: https://

www.mongodb.org/downloads

Installers for all major platforms

When installed, MongoDB needs a driver to be usable with a specific platform One for .NET, another for Node.JS,

etc…

To install the MongoDB driver for .NET run in Visual Studio's Package Manager Console

Install-Package mongocsharpdriver

Page 6: MongoDB and using MongoDB with .NET

Installing MongoDBLive Demo

Page 7: MongoDB and using MongoDB with .NET

Starting MongoDB Instance

Once installed, the MongoDB must be started Go to installation folder and run mongod

$ cd path/to/mondodb/installation/folder$ mondgod

Or add the path to mongod.exe to the %PATH% And simply run

When run, the MongoDB can be used from .NET, Node.js, etc…

$ mondgod

Page 8: MongoDB and using MongoDB with .NET

Host the MongoDB instance

Connecting to MongoDB

with .NET and C# The C# driver for MongoDB provides a .NET SDK for working with MongoDB Init a MongoClient with a connection

string

MongoClient client = new MongoClient("mongodb://localhost);

MongoServer server = client.GetServer();

Connect to a database If the db does not exists, it will be

createadMongoDatabase db = server.GetDatabase("logger");

Work with the database

Page 9: MongoDB and using MongoDB with .NET

Using the MongoDBwith .NET and C#

Live Demo

Page 10: MongoDB and using MongoDB with .NET

MongoDB Viewers

Page 11: MongoDB and using MongoDB with .NET

MongoDB Viewers

MongoDB is an open-source DB system So there are many available viewers

Some, but not all are: MongoDB CLI

Comes with installation of MongoDB

Execute queries inside the CMD/Terminal

MongoVUE & UMongo Provide UI to view, edit are remove DB

documents

Page 12: MongoDB and using MongoDB with .NET

MongoDB ViewersLive Demo

Page 13: MongoDB and using MongoDB with .NET

MongoDB StructureHow is the data structured in MongoDB

Page 14: MongoDB and using MongoDB with .NET

MongoDB Structure A MongoDB instance can have many

databases A database can have many collections

A collection can have many documentsMongoDB Instance

Database DatabaseCollectio

nDocument

Document

Database

Database

Collection

Collection

Collection

Collection

Collection

Collection

Document

Page 15: MongoDB and using MongoDB with .NET

MongoDB Documents Documents in MongoDB are JSON

objectsName: Databases

Students: 250

Titles:

- Intro to SQL

- NoSQL databases

- Entity Framework

Evaluation:

- Homework: 10

- Exam: 60

- Teamwork: 20

- Attendance: 10

{"name": "Databases", "students": 250, "titles": [ "Intro to SQL", "NoSQL databases", "Entity Framework" ], "evaluation": { "homework": 10, "exam": 60, "teamwork": 20, "attendance": 10}

Page 16: MongoDB and using MongoDB with .NET

MongoDB Documents (2)

MongoDB documents many types of values Numbers, Booleans

Strings, ObjectIds

Dates

Objects Also know as

nested documents

Arrays

17, 3.14, true/false

"Doncho Minkov"

2014-09-01T14:58:48.126Z{ "username": "Minkov", "accessLvl": 2}

['bananas', 'oranges']

Page 17: MongoDB and using MongoDB with .NET

.NET Driver

Page 18: MongoDB and using MongoDB with .NET

The .NET MongoDB SDK The .NET driver provides a SDK to work with MongoDB databases Supports CRUD operations

create, read, update and delete

Can create indexes Provides LINQ-like functionality

That is executed over the database using the native query commands

Does not have transactions Still has atomicity on single

document

Page 19: MongoDB and using MongoDB with .NET

CRUD Operations with MongoDB

and .NETInsert, Remove, Update, Get

Page 20: MongoDB and using MongoDB with .NET

CRUD Over MongoDB with .NET

A collection must be created/fetched Before performing any operationsvar logsCollection = db.GetCollection<Log>("logs");

All operations are done over this collection: Fetch documentslogsCollection.FindAll();

logs.FindOneById(id);

Insert RemovelogsCollection.Insert(log);

logs.Remove(removeQuery);

UpdatelogsCollection.Update( query, update);

Page 21: MongoDB and using MongoDB with .NET

CRUD Operations with MongoDB

and .NETLive Demo

Page 22: MongoDB and using MongoDB with .NET

MongoDB QueriesPerform Operations over the Database

Page 23: MongoDB and using MongoDB with .NET

MongoDB Queries

MongoDB supports document-based queries They are executed on the server

They are the equivalent of SQL for RDBMS

Queries can be made in two ways: Using native-like MongoDB queries

Using LINQ-like queries

Page 24: MongoDB and using MongoDB with .NET

Native-like MongoDB Queries

Find queries are created using similar to the native MongoDB document queries: Find recent logs:IMongoQuery findNewLogsQuery = Query.And(Query.GT("LogDate", date));var logs = logsCollection.Find(findNewLogsQuery);

Remove logs, older than a day:IMongoQuery findOldLogsQuery = Query.And( Query.LT("LogDate", Date.now.addDays(-1)));logsCollection.Remove(findOldLogsQuery);

Page 25: MongoDB and using MongoDB with .NET

Native-like MongoDB Queries

Live Demo

Page 26: MongoDB and using MongoDB with .NET

LINQ-like Queries

LINQ-like Queries:

var findRecentBugsQuery = Query<Log>.Where(log => log.LogType.Type == "bug" && log.LogDate > date);var logs = logsCollection.Find(findRecentBugsQuery) .Select(log => log.Text);logsCollection.Update

var findOldPendingBugsQuery = Query<Log>.Where( log => log.LogDate < DateTime.Now.AddDays(-10) && log.LogType.Type == "bug" && log.LogType.State == "pending");

Page 27: MongoDB and using MongoDB with .NET

LINQ-like MongoDB Queries

Live Demo

Page 28: MongoDB and using MongoDB with .NET

LINQ-to-MongoDB The MongoDB C# driver supports part

of LINQ over MongoDB collections Only LINQ queries that can be

translated to an equivalent MongoDB query are supported Others throw runtime exception and the

error message will indicate which part of the query wasn’t supportedvar logs =

from log in db.GetCollection<Log>("Logs").AsQueryable<Log>() where log.LogType.Type == "ticket" && log.LogType.State == "pending"select log;

Page 29: MongoDB and using MongoDB with .NET

LINQ-to-MongoDBLive Demo

Page 30: MongoDB and using MongoDB with .NET

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезания

ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGap

free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно

?

? ? ??

?? ?

?

?

?

??

?

?

? ?

Questions?

?

MongoDB and MongoDB with .NEt

http://academy.telerik.com

Page 31: MongoDB and using MongoDB with .NET

Homework

1.Research how to create a MongoDB database in Mongolab(http://mongolab.com )

2.Create a chat database in MongoLab The database should keep

messages

Each message has a text, date and an embedded field user

Users have username

Page 32: MongoDB and using MongoDB with .NET

Homework (2)

3. Create a Chat client, using the Chat MongoDB database

When the client starts, it asks for username

Without password

Logged-in users can see All posts, since they have logged in

History of all posts

Logged-in users can post message

Create a simple WPF application for the client