SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Downloaden Sie, um offline zu lesen
June 20-21, 2017
Chicago, IL
Chicago, IL
MongoDB World is where the
world’s fastest growing
database community comes
to connect, explore, and
learn.
Back to Basics 2017: Webinar 2
Mi primera aplicación MongoDB
Alejandro Mancilla
Senior Solutions Architect, EMEA
alejandro.mancilla@mongodb.com
@alxmancilla
Rubén Terceño
Senior Solutions Architect, LATAM
ruben@mongodb.com
@rubenTerceno
¡Bienvenidos!
Agenda del Curso
Date Time Webinar
25-Abril-2016 16:00 CEST Introducción a NoSQL
3-Mayo-2016 16:00 CEST Mi primera aplicación MongoDB
9-Mayo-2016 16:00 CEST Introducción a los ReplicaSets
16-Mayo-2016 16:00 CEST Introducción al Sharding
16:00 CEST = 14:00 UTC = 11:00am Argentina / Uruguay = 9:30am
Venezuela/Chile = 9:00am Colombia / Ecuador / México
Resumen del webinar 1
•  ¿Por qué existe NoSQL?
•  Tipos de bases de datos NoSQL
•  Características clave de MongoDB
•  Tolerancia a fallos y persistencia de datos en MongoDB
•  Escalabilidad en MongoDB
Agenda
• Vocabulario básico
• Instalación de MongoDB
• Construcción de una aplicación básica
• Creación de índices
• Optimización de queries con explain()
Vocabulario Básico
Relational MongoDB
Database Database / Base de datos
Table Collection / Colección
Row Document / Documento
Index Index / Índice
Join Lookup
Foreign Key Reference / Referencia
Multi-table transaction Single document transaction
Document
{
name : "Alejandro Mancilla",
title : "Senior Solutions Architect",
address : {
address1 : "Insurgentes 123",
address2 : "Col. Juárez",
zipcode : "06600",
}
expertise: ["MongoDB", "Java", "Javascript" ],
employee_number : 654,
location : [ 53.34, -6.26 ]
}
MongoDB Documents are Typed
{
name : “Alejandro Mancilla”,
title : “Senior Solutions Architect”,
Address : {
address1 : “Insurgentes 123”,
address2 : “Col. Juárez”,
zipcode : “06600”,
}
expertise: [ “MongoDB”, “Java”, “Javascript” ],
employee_number : 654,
location : [ 53.34, -6.26 ]
}
Strings
Nested Document
Array
Integer
Geo-spatial Coordinates
MongoDB Drivers
http://bsonspec.org/spec.html
Installing MongoDB
$ curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.4.4.tgz
$ tar xzvf mongodb-osx-x86_64-3.4.4.tgz
x mongodb-osx-x86_64-3.4.4/bin/mongodump
x mongodb-osx-x86_64-3.4.4/bin/mongorestore
x mongodb-osx-x86_64-3.4.4/bin/mongoexport
x mongodb-osx-x86_64-3.4.4/bin/mongoimport
x mongodb-osx-x86_64-3.4.4/bin/mongostat
x mongodb-osx-x86_64-3.4.4/bin/mongotop
x mongodb-osx-x86_64-3.4.4/bin/mongooplog
x mongodb-osx-x86_64-3.4.4/bin/mongoperf
x mongodb-osx-x86_64-3.4.4/bin/mongod
x mongodb-osx-x86_64-3.4.4/bin/mongos
x mongodb-osx-x86_64-3.4.4/bin/mongo
Running Mongod
$ mkdir ./data/
$ ./mongodb-osx-x86_64-3.4.4/bin/mongod --dbpath ./data/
2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] MongoDB starting : pid=18976 port=27017 dbpath=./data/ 64-bit
host=mancilla.local
2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] db version v3.4.4
…
2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] build environment:
2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] distarch: x86_64
2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] target_arch: x86_64
2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] options: { storage: { dbPath: "./data/" } }
2017-05-01T21:41:09.170-0500 I STORAGE [initandlisten] wiredtiger_open config:
create,cache_size=7680M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(
enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_si
ze=2GB),statistics_log=(wait=0),
…
2017-05-01T21:41:10.085-0500 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4
2017-05-01T21:41:10.086-0500 I NETWORK [thread1] waiting for connections on port 27017
2017-05-01T21:41:10.130-0500 I NETWORK [thread1] connection accepted from 127.0.0.1:57106 #1 (1 connection now open)
Connecting Via The Shell
$ ./mongodb-osx-x86_64-3.4.4/bin/mongo
MongoDB shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.4
Server has startup warnings:
2017-05-01T21:41:09.668-0500 I CONTROL [initandlisten]
2017-05-01T21:41:09.668-0500 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for
the database.
2017-05-01T21:41:09.668-0500 I CONTROL [initandlisten] ** Read and write access to data and
configuration is unrestricted.
2017-05-01T21:41:09.668-0500 I CONTROL [initandlisten]
> show databases
admin 0.000GB
local 0.000GB
Inserting your first record
> use cb
switched to db cb
> db.demo.insert({ name : "Alejandro Mancilla", title : "Senior Solutions Architect", address : { address1 : "Insurgentes 123",
address2 : "Col. Juárez", zipcode : "06600" }, expertise: ["MongoDB", "Java", "Javascript" ], employee_number : 654, location :
[ 53.34, -6.26 ] })
WriteResult({ "nInserted" : 1 })
> show databases
admin 0.000GB
cb 0.000GB
local 0.000GB
> show collections
demo
> db.demo.findOne()
{
"_id" : ObjectId("5909591701bfa6fc636edd17"),
"name" : "Alejandro Mancilla",
"title" : "Senior Solutions Architect",
"address" : { "address1" : "Insurgentes 123", "address2" : "Col. Juárez", "zipcode" : "06600” },
"expertise" : ["MongoDB","Java","Javascript” ],
"employee_number" : 654,
"location" : [53.34,-6.26]
}
Object ID
575420c87a75dbb02b4f45cb
TS------ID----PID-Count-
A Simple Blog Application
•  Lets create a blogging application with:
•  Articles
•  Users
•  Comments
18
Typical Entity Relation Diagram
User
·Name
·Email address
Category
·Name
·URL
Comment
·Comment
·Date
·Author
Article
·Name
·Slug
·Publish date
·Text
Tag
·Name
·URL
In MongoDB we build organically
> use blog
switched to db blog
> db.users.insert( { "name" : "amancilla", "password" :
"top secret", "lang" : "ES" } )
WriteResult({ "nInserted" : 1 })
> db.users.findOne()
{
"_id" : ObjectId("5907fb58bae58ed30ba2feed"),
"name" : "amancilla",
"password" : "top secret",
"lang" : "ES"
}
How do we do this in a program?
package com.mongodb.amancilla;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class Demo {
public static void main(String[] args) {
MongoClient client = new MongoClient();
MongoDatabase blog = client.getDatabase("blog");
MongoCollection<Document> users = blog.getCollection("users");
Document user = new Document(”name",”amancilla")
.append(”password", ”top secret")
.append("lang", "ES");
users.insertOne(user);
client.close();
}
}
Make a lot of users
public class Demo {
public static void main(String[] args) {
MongoClient client = new MongoClient("localhost", 27017);
MongoDatabase blog = client.getDatabase("blog");
MongoCollection<Document> users = blog.getCollection("users");
for (int i = 0; i < 1000; i++) {
int suffix = (int)Math.round(Math.random()*12345);
Document user = new Document()
.append("name", "USER_"+suffix)
.append("password", "pass"+ suffix)
.append("lang", "ES")
.append("karma", Integer.valueOf(suffix % 500));
users.insertOne(user);
}
client.close();
}
}
Next up Articles
import [+]
import static java.util.Arrays.asList;
public class Demo {
public static void main(String[] args) {
MongoClient client = new MongoClient();
MongoDatabase blog = client.getDatabase("blog");
MongoCollection<Document> articulos = blog.getCollection("articles");
String myName = ”amancilla";
Document articulo = new Document(”title","My article")
.append("author", myName)
.append("text", "Lorem ipsum dolor sit amet, […] ")
.append("tags", Arrays.asList("demo",”Java","MongoDB"));
articulos.insertOne(articulo);
client.close();
}
}
Create a new type of article
public class Demo {
static DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'” , Locale.ENGLISH);
public static void main(String[] args) {
MongoClient client = new MongoClient();
MongoDatabase blog = client.getDatabase("blog");
MongoCollection<Document> articulos = blog.getCollection("articles");
String myName = "amancilla";
Document articulo = new Document("title","My article")
.append("author", myName)
.append("text", "Lorem ipsum dolor sit amet […]")
.append("tags", Arrays.asList("demo", "Java", "MongoDB"))
.append("date", new Date());
articulos.insertOne(articulo);
client.close();
}
}
Make a lot of articles
public class Demo {
static DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"
, Locale.ENGLISH);
public static void main(String[] args) {
MongoClient client = new MongoClient();
MongoDatabase blog = client.getDatabase("blog");
MongoCollection<Document> articulos = blog.getCollection("articles");
List<Document> list = new ArrayList<Document>();
for (int i=0; i<1000000; i++){
Document articulo = new Document(”title”, "Mi artículo " + i)
.append("author", "USER_" + Math.round(Math.random()*10000))
.append("text", "Lorem ipsum dolor sit amet, […] ex ea commodo consequat.")
.append("tags", asList("demo", "español", "MongoDB"))
.append(”date", new Date());
list.add(articulo);
if (i % 5000 == 4999){
articulos.insertMany(list);
list.clear();
}
}
client.close();
}
}
Find a User
> db.users.findOne()
{
"_id" : ObjectId("5742da5bb26a88bc00e941ac"),
"name" : "FLFZQLSRWZ_0",
"lang" : "EN",
"password" : "vTlILbGWLt",
"karma" : 448
}
> db.users.find( { "name" : "VHXDAUUFJW_45" } ).pretty()
{
"_id" : ObjectId("5742da5bb26a88bc00e94206"),
"name" : "VHXDAUUFJW_45",
"lang" : "EN",
"password" : "GmRLnCeKVp",
"karma" : 284
}
Find Users with high Karma
> db.users.find( { "karma" : { $gte : 450 }} ).pretty()
{
"_id" : ObjectId("5742da5bb26a88bc00e941ae"),
"name" : "JALLFRKBWD_1",
"lang" : "EN",
"password" : "bCSKSKvUeb",
"karma" : 487
}
{
"_id" : ObjectId("5742da5bb26a88bc00e941e4"),
"name" : "OTKWJJBNBU_28",
"lang" : "EN",
"password" : "HAWpiATCBN",
"karma" : 473
}
{
Using projection
> db.users.find( { "karma" : { $gte : 450 }}, { "_id" :
0, name : 1, karma : 1 } )
{ "name" : "USER_8973", "karma" : 473 }
{ "name" : "USER_11959", "karma" : 459 }
{ "name" : "USER_9494", "karma" : 494 }
{ "name" : "USER_482", "karma" : 482 }
{ "name" : "USER_11466", "karma" : 466 }
{ "name" : "USER_9476", "karma" : 476 }
{ "name" : "USER_11956", "karma" : 456 }
{ "name" : "USER_1954", "karma" : 454 }
Using sort
> b.users.find({"karma": {$gte: 450}},{"_id": 0, name:
1, karma: 1}).sort({"karma": 1})
{ "name" : "USER_10950", "karma" : 450 }
{ "name" : "USER_8450", "karma" : 450 }
{ "name" : "USER_10953", "karma" : 453 }
{ "name" : "USER_8953", "karma" : 453 }
{ "name" : "USER_1953", "karma" : 453 }
{ "name" : "USER_3453", "karma" : 453 }
{ "name" : "USER_1954", "karma" : 454 }
{ "name" : "USER_11454", "karma" : 454 }
Article update: adding comments 1
> db.articles.find( { "_id" : 19 } ).pretty()
{
"_id" : 19,
"body" :
"nTzOofOcnHKkJxpjKAyqTTnKZMFzzkWFeXtBRuEKsctuGBgWIrEBrYdvFI
VHJWaXLUTVUXblOZZgUqWu",
"postdate" : ISODate("2016-05-23T12:02:46.830Z"),
"author" : "ASWTOMMABN_19",
"title" : "CPMaqHtAdRwLXhlUvsej"
}
> db.articles.update( { _id : 19 }, { $set : { comments :
[] }} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0,
"nModified" : 1 })
Article update: adding comments 2
> db.articles.find( { _id :19 } ).pretty()
{
"_id" : 19,
"body" :
"KmwFSIMQGcIsRNTDBFPuclwcVJkoMcrIPwTiSZDYyatoKzeQiKvJkiVSrn
dXqrALVIYZxGpaMjucgXUV",
"postdate" : ISODate("2016-05-23T16:04:39.497Z"),
"author" : "USER_18",
"title" : "wTLreIEyPfovEkBhJZZe",
"comments" : [ ]
}
>
Article update: adding comments 3
> db.articles.update( { _id : 19 }, { $push : { comments : { name : "USER_123", comment :
"Primer!" }}} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.articles.find( { _id :19 } ).pretty()
{
"_id" : 19,
"body" : "KmwFSIMQGcIsRNTDBFPuclJkoMcrIPwTiSZDYyatoKzeQiKvJkiVSrndXqrALVIYZxGpaMjucgXUV",
"postdate" : ISODate("2016-05-23T16:04:39.497Z"),
"author" : "USER_18",
"title" : "wTLreIEyPfovEkBhJZZe",
"comments" : [
{
"username" : "USER_123",
"comment" : ”Primer!"
}
]
}
Article delete
> db.articles.remove( { "_id" : 25 } )
WriteResult({ "nRemoved" : 1 })
> db.articles.remove( { "_id" : 25 } )
WriteResult({ "nRemoved" : 0 })
> db.articles.remove( { "_id" : { $lte : 5 }} )
WriteResult({ "nRemoved" : 6 })
•  Deletion leaves holes
•  Dropping a collection is cheaper than deleting a large collection element
by element
Remember Users and Articles
> db.users.findOne()
{
"_id" : ObjectId("57431c07b26a88bf060e10cb"),
"username" : "USER_0",
"lang" : "EN",
"password" : "kGIxPxqKGJ",
"karma" : 266
}
> db.articles.findOne()
{
"_id" : 0,
"body" : "hvJLnrrfZQurmtjPfUWbMhaQLZjsxHXbUycmJVZTeOZesTnZtojThrebRcUoiYwivjpwG",
"postdate" : ISODate("2016-05-23T16:04:39.246Z"),
"author" : "USER_0",
"title" : "gpNIoPxpfTAxWjzAVoTJ"
}
Find a User
> db.users.find( { "username" : "USER_123" } ).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "blog.users",
"indexFilterSet" : false,
"parsedQuery" : {
"username" : {
"$eq" : ”USER_123"
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"username" : {
"$eq" : ”USER_123"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
} "ok" : 1
}
Find a User – Execution Stats
> db.users.find( {"name" : "USER_999" } ).explain( "executionStats" ).executionStats
{
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 433,
"totalKeysExamined" : 0,
"totalDocsExamined" : 1000000,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"name" : {
"$eq" : "USER_999”}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 330,
"works" : 1000002,
"advanced" : 1,
"needTime" : 1000000,
"needYield" : 0,
"saveState" : 7812,
"restoreState" : 7812,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 1000000
We need an index
> db.users.createIndex( { name : 1 } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
Indexes Overview
•  Parameters
•  Background : Create an index in the background as opposed to locking the database
•  Unique : All keys in the collection must be unique. Duplicate key insertions will be
rejected with an error.
•  Name : explicitly name an index. Otherwise the index name is selfgenerated from the
index fields.
•  Deleting an Index
•  db.users.dropIndex({ “name” : 1 })
•  Get All the Indexes on a collection
•  db.users.getIndexes()
Query Plan Execution Stages
• COLLSCAN : for a collection scan
• IXSCAN : for scanning index keys
• FETCH : for retrieving documents
• SHARD_MERGE : for merging results from shards
Add an Index
> db.users.find( {"name" : "USER_999"} ).explain("executionStats").executionStats
{
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 1,
"totalDocsExamined" : 1,
…
Execution Stage
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"docsExamined" : 1,,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"keyPattern" : {
"username" : 1},
"indexName" : "username_1",
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"username" : [
"["USER_999", "USER_999"]”]},
"keysExamined" : 1,
"seenInvalidated" : 0 } } }
Drivers and Frameworks
¿Qué hemos aprendido?
• Cómo crear una base de datos y una colección
• Cómo insertar documentos
• Cómo realizar búsquedas
• Cómo hacer modificaciones de los documentos existentes
• Cómo borrar documentos
• Cómo comprobar la eficiencia de una operación
• Cómo crear índices
• Cómo revisar si los índices se utilizan en una operación
Próximo Webinar
Introducción a los ReplicaSets
•  9 de Mayo 2017 – 16:00 CEST, 11:00 ART, 9:00 CDT
•  Cómo garantizar que sus datos son durables
•  Cómo recuperarse de los fallos automáticamente
•  Cómo escribir código de cliente seguro
•  Regístrese en : https://www.mongodb.com/webinars
•  Denos su opinión, por favor: back-to-basics@mongodb.com
¿Preguntas?
Back to Basics 2017: Mí primera aplicación MongoDB

Weitere ähnliche Inhalte

Was ist angesagt?

mongoDB Performance
mongoDB PerformancemongoDB Performance
mongoDB Performance
Moshe Kaplan
 
MongoDB : The Definitive Guide
MongoDB : The Definitive GuideMongoDB : The Definitive Guide
MongoDB : The Definitive Guide
Wildan Maulana
 
Intro To Mongo Db
Intro To Mongo DbIntro To Mongo Db
Intro To Mongo Db
chriskite
 

Was ist angesagt? (20)

Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
 
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
 
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
 
Conceptos básicos. Seminario web 5: Introducción a Aggregation Framework
Conceptos básicos. Seminario web 5: Introducción a Aggregation FrameworkConceptos básicos. Seminario web 5: Introducción a Aggregation Framework
Conceptos básicos. Seminario web 5: Introducción a Aggregation Framework
 
MongoDB Days Silicon Valley: Winning the Dreamforce Hackathon with MongoDB
MongoDB Days Silicon Valley: Winning the Dreamforce Hackathon with MongoDBMongoDB Days Silicon Valley: Winning the Dreamforce Hackathon with MongoDB
MongoDB Days Silicon Valley: Winning the Dreamforce Hackathon with MongoDB
 
mongoDB Performance
mongoDB PerformancemongoDB Performance
mongoDB Performance
 
Webinar: Getting Started with MongoDB - Back to Basics
Webinar: Getting Started with MongoDB - Back to BasicsWebinar: Getting Started with MongoDB - Back to Basics
Webinar: Getting Started with MongoDB - Back to Basics
 
Back to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQLBack to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQL
 
Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011
 
High Performance Applications with MongoDB
High Performance Applications with MongoDBHigh Performance Applications with MongoDB
High Performance Applications with MongoDB
 
MongoDB : The Definitive Guide
MongoDB : The Definitive GuideMongoDB : The Definitive Guide
MongoDB : The Definitive Guide
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in Documents
 
MongoDB
MongoDBMongoDB
MongoDB
 
MongoDB Days Silicon Valley: Jumpstart: Ops/Admin 101
MongoDB Days Silicon Valley: Jumpstart: Ops/Admin 101MongoDB Days Silicon Valley: Jumpstart: Ops/Admin 101
MongoDB Days Silicon Valley: Jumpstart: Ops/Admin 101
 
Webinar: Developing with the modern App Stack: MEAN and MERN (with Angular2 a...
Webinar: Developing with the modern App Stack: MEAN and MERN (with Angular2 a...Webinar: Developing with the modern App Stack: MEAN and MERN (with Angular2 a...
Webinar: Developing with the modern App Stack: MEAN and MERN (with Angular2 a...
 
MongoDB 101
MongoDB 101MongoDB 101
MongoDB 101
 
10 Key MongoDB Performance Indicators
10 Key MongoDB Performance Indicators  10 Key MongoDB Performance Indicators
10 Key MongoDB Performance Indicators
 
Back to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQLBack to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQL
 
Mongodb
MongodbMongodb
Mongodb
 
Intro To Mongo Db
Intro To Mongo DbIntro To Mongo Db
Intro To Mongo Db
 

Ähnlich wie Back to Basics 2017: Mí primera aplicación MongoDB

Mongo db eveningschemadesign
Mongo db eveningschemadesignMongo db eveningschemadesign
Mongo db eveningschemadesign
MongoDB APAC
 
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
MongoDB
 
Webinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedWebinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting Started
MongoDB
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Rails
rfischer20
 

Ähnlich wie Back to Basics 2017: Mí primera aplicación MongoDB (20)

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
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDB
 
Getting started with MongoDB and Scala - Open Source Bridge 2012
Getting started with MongoDB and Scala - Open Source Bridge 2012Getting started with MongoDB and Scala - Open Source Bridge 2012
Getting started with MongoDB and Scala - Open Source Bridge 2012
 
Mongo db eveningschemadesign
Mongo db eveningschemadesignMongo db eveningschemadesign
Mongo db eveningschemadesign
 
MongoDB
MongoDBMongoDB
MongoDB
 
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
 
How to use MongoDB with CakePHP
How to use MongoDB with CakePHPHow to use MongoDB with CakePHP
How to use MongoDB with CakePHP
 
Webinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedWebinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting Started
 
Mongoose and MongoDB 101
Mongoose and MongoDB 101Mongoose and MongoDB 101
Mongoose and MongoDB 101
 
The emerging world of mongo db csp
The emerging world of mongo db   cspThe emerging world of mongo db   csp
The emerging world of mongo db csp
 
MongoDB - Back to Basics - La tua prima Applicazione
MongoDB - Back to Basics - La tua prima ApplicazioneMongoDB - Back to Basics - La tua prima Applicazione
MongoDB - Back to Basics - La tua prima Applicazione
 
Back to basics Italian webinar 2 Mia prima applicazione MongoDB
Back to basics Italian webinar 2  Mia prima applicazione MongoDBBack to basics Italian webinar 2  Mia prima applicazione MongoDB
Back to basics Italian webinar 2 Mia prima applicazione MongoDB
 
MongoDB Days Silicon Valley: Building Applications with the MEAN Stack
MongoDB Days Silicon Valley: Building Applications with the MEAN StackMongoDB Days Silicon Valley: Building Applications with the MEAN Stack
MongoDB Days Silicon Valley: Building Applications with the MEAN Stack
 
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDBMongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
 
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDBMongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
 
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
 
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
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Rails
 
moma-django overview --> Django + MongoDB: building a custom ORM layer
moma-django overview --> Django + MongoDB: building a custom ORM layermoma-django overview --> Django + MongoDB: building a custom ORM layer
moma-django overview --> Django + MongoDB: building a custom ORM layer
 
오픈 소스 프로그래밍 - NoSQL with Python
오픈 소스 프로그래밍 - NoSQL with Python오픈 소스 프로그래밍 - NoSQL with Python
오픈 소스 프로그래밍 - NoSQL with Python
 

Mehr von MongoDB

Mehr von MongoDB (20)

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
 

Kürzlich hochgeladen

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Kürzlich hochgeladen (20)

Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 

Back to Basics 2017: Mí primera aplicación MongoDB

  • 1.
  • 2. June 20-21, 2017 Chicago, IL Chicago, IL MongoDB World is where the world’s fastest growing database community comes to connect, explore, and learn.
  • 3. Back to Basics 2017: Webinar 2 Mi primera aplicación MongoDB Alejandro Mancilla Senior Solutions Architect, EMEA alejandro.mancilla@mongodb.com @alxmancilla Rubén Terceño Senior Solutions Architect, LATAM ruben@mongodb.com @rubenTerceno
  • 5. Agenda del Curso Date Time Webinar 25-Abril-2016 16:00 CEST Introducción a NoSQL 3-Mayo-2016 16:00 CEST Mi primera aplicación MongoDB 9-Mayo-2016 16:00 CEST Introducción a los ReplicaSets 16-Mayo-2016 16:00 CEST Introducción al Sharding 16:00 CEST = 14:00 UTC = 11:00am Argentina / Uruguay = 9:30am Venezuela/Chile = 9:00am Colombia / Ecuador / México
  • 6. Resumen del webinar 1 •  ¿Por qué existe NoSQL? •  Tipos de bases de datos NoSQL •  Características clave de MongoDB •  Tolerancia a fallos y persistencia de datos en MongoDB •  Escalabilidad en MongoDB
  • 7. Agenda • Vocabulario básico • Instalación de MongoDB • Construcción de una aplicación básica • Creación de índices • Optimización de queries con explain()
  • 8. Vocabulario Básico Relational MongoDB Database Database / Base de datos Table Collection / Colección Row Document / Documento Index Index / Índice Join Lookup Foreign Key Reference / Referencia Multi-table transaction Single document transaction
  • 9. Document { name : "Alejandro Mancilla", title : "Senior Solutions Architect", address : { address1 : "Insurgentes 123", address2 : "Col. Juárez", zipcode : "06600", } expertise: ["MongoDB", "Java", "Javascript" ], employee_number : 654, location : [ 53.34, -6.26 ] }
  • 10. MongoDB Documents are Typed { name : “Alejandro Mancilla”, title : “Senior Solutions Architect”, Address : { address1 : “Insurgentes 123”, address2 : “Col. Juárez”, zipcode : “06600”, } expertise: [ “MongoDB”, “Java”, “Javascript” ], employee_number : 654, location : [ 53.34, -6.26 ] } Strings Nested Document Array Integer Geo-spatial Coordinates
  • 12. Installing MongoDB $ curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.4.4.tgz $ tar xzvf mongodb-osx-x86_64-3.4.4.tgz x mongodb-osx-x86_64-3.4.4/bin/mongodump x mongodb-osx-x86_64-3.4.4/bin/mongorestore x mongodb-osx-x86_64-3.4.4/bin/mongoexport x mongodb-osx-x86_64-3.4.4/bin/mongoimport x mongodb-osx-x86_64-3.4.4/bin/mongostat x mongodb-osx-x86_64-3.4.4/bin/mongotop x mongodb-osx-x86_64-3.4.4/bin/mongooplog x mongodb-osx-x86_64-3.4.4/bin/mongoperf x mongodb-osx-x86_64-3.4.4/bin/mongod x mongodb-osx-x86_64-3.4.4/bin/mongos x mongodb-osx-x86_64-3.4.4/bin/mongo
  • 13. Running Mongod $ mkdir ./data/ $ ./mongodb-osx-x86_64-3.4.4/bin/mongod --dbpath ./data/ 2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] MongoDB starting : pid=18976 port=27017 dbpath=./data/ 64-bit host=mancilla.local 2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] db version v3.4.4 … 2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] build environment: 2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] distarch: x86_64 2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] target_arch: x86_64 2017-05-01T21:41:09.169-0500 I CONTROL [initandlisten] options: { storage: { dbPath: "./data/" } } 2017-05-01T21:41:09.170-0500 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=7680M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=( enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_si ze=2GB),statistics_log=(wait=0), … 2017-05-01T21:41:10.085-0500 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4 2017-05-01T21:41:10.086-0500 I NETWORK [thread1] waiting for connections on port 27017 2017-05-01T21:41:10.130-0500 I NETWORK [thread1] connection accepted from 127.0.0.1:57106 #1 (1 connection now open)
  • 14. Connecting Via The Shell $ ./mongodb-osx-x86_64-3.4.4/bin/mongo MongoDB shell version v3.4.4 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.4 Server has startup warnings: 2017-05-01T21:41:09.668-0500 I CONTROL [initandlisten] 2017-05-01T21:41:09.668-0500 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-05-01T21:41:09.668-0500 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-05-01T21:41:09.668-0500 I CONTROL [initandlisten] > show databases admin 0.000GB local 0.000GB
  • 15. Inserting your first record > use cb switched to db cb > db.demo.insert({ name : "Alejandro Mancilla", title : "Senior Solutions Architect", address : { address1 : "Insurgentes 123", address2 : "Col. Juárez", zipcode : "06600" }, expertise: ["MongoDB", "Java", "Javascript" ], employee_number : 654, location : [ 53.34, -6.26 ] }) WriteResult({ "nInserted" : 1 }) > show databases admin 0.000GB cb 0.000GB local 0.000GB > show collections demo > db.demo.findOne() { "_id" : ObjectId("5909591701bfa6fc636edd17"), "name" : "Alejandro Mancilla", "title" : "Senior Solutions Architect", "address" : { "address1" : "Insurgentes 123", "address2" : "Col. Juárez", "zipcode" : "06600” }, "expertise" : ["MongoDB","Java","Javascript” ], "employee_number" : 654, "location" : [53.34,-6.26] }
  • 17. A Simple Blog Application •  Lets create a blogging application with: •  Articles •  Users •  Comments
  • 18. 18 Typical Entity Relation Diagram User ·Name ·Email address Category ·Name ·URL Comment ·Comment ·Date ·Author Article ·Name ·Slug ·Publish date ·Text Tag ·Name ·URL
  • 19. In MongoDB we build organically > use blog switched to db blog > db.users.insert( { "name" : "amancilla", "password" : "top secret", "lang" : "ES" } ) WriteResult({ "nInserted" : 1 }) > db.users.findOne() { "_id" : ObjectId("5907fb58bae58ed30ba2feed"), "name" : "amancilla", "password" : "top secret", "lang" : "ES" }
  • 20. How do we do this in a program? package com.mongodb.amancilla; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; public class Demo { public static void main(String[] args) { MongoClient client = new MongoClient(); MongoDatabase blog = client.getDatabase("blog"); MongoCollection<Document> users = blog.getCollection("users"); Document user = new Document(”name",”amancilla") .append(”password", ”top secret") .append("lang", "ES"); users.insertOne(user); client.close(); } }
  • 21. Make a lot of users public class Demo { public static void main(String[] args) { MongoClient client = new MongoClient("localhost", 27017); MongoDatabase blog = client.getDatabase("blog"); MongoCollection<Document> users = blog.getCollection("users"); for (int i = 0; i < 1000; i++) { int suffix = (int)Math.round(Math.random()*12345); Document user = new Document() .append("name", "USER_"+suffix) .append("password", "pass"+ suffix) .append("lang", "ES") .append("karma", Integer.valueOf(suffix % 500)); users.insertOne(user); } client.close(); } }
  • 22. Next up Articles import [+] import static java.util.Arrays.asList; public class Demo { public static void main(String[] args) { MongoClient client = new MongoClient(); MongoDatabase blog = client.getDatabase("blog"); MongoCollection<Document> articulos = blog.getCollection("articles"); String myName = ”amancilla"; Document articulo = new Document(”title","My article") .append("author", myName) .append("text", "Lorem ipsum dolor sit amet, […] ") .append("tags", Arrays.asList("demo",”Java","MongoDB")); articulos.insertOne(articulo); client.close(); } }
  • 23. Create a new type of article public class Demo { static DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'” , Locale.ENGLISH); public static void main(String[] args) { MongoClient client = new MongoClient(); MongoDatabase blog = client.getDatabase("blog"); MongoCollection<Document> articulos = blog.getCollection("articles"); String myName = "amancilla"; Document articulo = new Document("title","My article") .append("author", myName) .append("text", "Lorem ipsum dolor sit amet […]") .append("tags", Arrays.asList("demo", "Java", "MongoDB")) .append("date", new Date()); articulos.insertOne(articulo); client.close(); } }
  • 24. Make a lot of articles public class Demo { static DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'" , Locale.ENGLISH); public static void main(String[] args) { MongoClient client = new MongoClient(); MongoDatabase blog = client.getDatabase("blog"); MongoCollection<Document> articulos = blog.getCollection("articles"); List<Document> list = new ArrayList<Document>(); for (int i=0; i<1000000; i++){ Document articulo = new Document(”title”, "Mi artículo " + i) .append("author", "USER_" + Math.round(Math.random()*10000)) .append("text", "Lorem ipsum dolor sit amet, […] ex ea commodo consequat.") .append("tags", asList("demo", "español", "MongoDB")) .append(”date", new Date()); list.add(articulo); if (i % 5000 == 4999){ articulos.insertMany(list); list.clear(); } } client.close(); } }
  • 25. Find a User > db.users.findOne() { "_id" : ObjectId("5742da5bb26a88bc00e941ac"), "name" : "FLFZQLSRWZ_0", "lang" : "EN", "password" : "vTlILbGWLt", "karma" : 448 } > db.users.find( { "name" : "VHXDAUUFJW_45" } ).pretty() { "_id" : ObjectId("5742da5bb26a88bc00e94206"), "name" : "VHXDAUUFJW_45", "lang" : "EN", "password" : "GmRLnCeKVp", "karma" : 284 }
  • 26. Find Users with high Karma > db.users.find( { "karma" : { $gte : 450 }} ).pretty() { "_id" : ObjectId("5742da5bb26a88bc00e941ae"), "name" : "JALLFRKBWD_1", "lang" : "EN", "password" : "bCSKSKvUeb", "karma" : 487 } { "_id" : ObjectId("5742da5bb26a88bc00e941e4"), "name" : "OTKWJJBNBU_28", "lang" : "EN", "password" : "HAWpiATCBN", "karma" : 473 } {
  • 27. Using projection > db.users.find( { "karma" : { $gte : 450 }}, { "_id" : 0, name : 1, karma : 1 } ) { "name" : "USER_8973", "karma" : 473 } { "name" : "USER_11959", "karma" : 459 } { "name" : "USER_9494", "karma" : 494 } { "name" : "USER_482", "karma" : 482 } { "name" : "USER_11466", "karma" : 466 } { "name" : "USER_9476", "karma" : 476 } { "name" : "USER_11956", "karma" : 456 } { "name" : "USER_1954", "karma" : 454 }
  • 28. Using sort > b.users.find({"karma": {$gte: 450}},{"_id": 0, name: 1, karma: 1}).sort({"karma": 1}) { "name" : "USER_10950", "karma" : 450 } { "name" : "USER_8450", "karma" : 450 } { "name" : "USER_10953", "karma" : 453 } { "name" : "USER_8953", "karma" : 453 } { "name" : "USER_1953", "karma" : 453 } { "name" : "USER_3453", "karma" : 453 } { "name" : "USER_1954", "karma" : 454 } { "name" : "USER_11454", "karma" : 454 }
  • 29. Article update: adding comments 1 > db.articles.find( { "_id" : 19 } ).pretty() { "_id" : 19, "body" : "nTzOofOcnHKkJxpjKAyqTTnKZMFzzkWFeXtBRuEKsctuGBgWIrEBrYdvFI VHJWaXLUTVUXblOZZgUqWu", "postdate" : ISODate("2016-05-23T12:02:46.830Z"), "author" : "ASWTOMMABN_19", "title" : "CPMaqHtAdRwLXhlUvsej" } > db.articles.update( { _id : 19 }, { $set : { comments : [] }} ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  • 30. Article update: adding comments 2 > db.articles.find( { _id :19 } ).pretty() { "_id" : 19, "body" : "KmwFSIMQGcIsRNTDBFPuclwcVJkoMcrIPwTiSZDYyatoKzeQiKvJkiVSrn dXqrALVIYZxGpaMjucgXUV", "postdate" : ISODate("2016-05-23T16:04:39.497Z"), "author" : "USER_18", "title" : "wTLreIEyPfovEkBhJZZe", "comments" : [ ] } >
  • 31. Article update: adding comments 3 > db.articles.update( { _id : 19 }, { $push : { comments : { name : "USER_123", comment : "Primer!" }}} ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.articles.find( { _id :19 } ).pretty() { "_id" : 19, "body" : "KmwFSIMQGcIsRNTDBFPuclJkoMcrIPwTiSZDYyatoKzeQiKvJkiVSrndXqrALVIYZxGpaMjucgXUV", "postdate" : ISODate("2016-05-23T16:04:39.497Z"), "author" : "USER_18", "title" : "wTLreIEyPfovEkBhJZZe", "comments" : [ { "username" : "USER_123", "comment" : ”Primer!" } ] }
  • 32. Article delete > db.articles.remove( { "_id" : 25 } ) WriteResult({ "nRemoved" : 1 }) > db.articles.remove( { "_id" : 25 } ) WriteResult({ "nRemoved" : 0 }) > db.articles.remove( { "_id" : { $lte : 5 }} ) WriteResult({ "nRemoved" : 6 }) •  Deletion leaves holes •  Dropping a collection is cheaper than deleting a large collection element by element
  • 33. Remember Users and Articles > db.users.findOne() { "_id" : ObjectId("57431c07b26a88bf060e10cb"), "username" : "USER_0", "lang" : "EN", "password" : "kGIxPxqKGJ", "karma" : 266 } > db.articles.findOne() { "_id" : 0, "body" : "hvJLnrrfZQurmtjPfUWbMhaQLZjsxHXbUycmJVZTeOZesTnZtojThrebRcUoiYwivjpwG", "postdate" : ISODate("2016-05-23T16:04:39.246Z"), "author" : "USER_0", "title" : "gpNIoPxpfTAxWjzAVoTJ" }
  • 34. Find a User > db.users.find( { "username" : "USER_123" } ).explain() { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "blog.users", "indexFilterSet" : false, "parsedQuery" : { "username" : { "$eq" : ”USER_123" } }, "winningPlan" : { "stage" : "COLLSCAN", "filter" : { "username" : { "$eq" : ”USER_123" } }, "direction" : "forward" }, "rejectedPlans" : [ ] } "ok" : 1 }
  • 35. Find a User – Execution Stats > db.users.find( {"name" : "USER_999" } ).explain( "executionStats" ).executionStats { "executionSuccess" : true, "nReturned" : 1, "executionTimeMillis" : 433, "totalKeysExamined" : 0, "totalDocsExamined" : 1000000, "executionStages" : { "stage" : "COLLSCAN", "filter" : { "name" : { "$eq" : "USER_999”} }, "nReturned" : 1, "executionTimeMillisEstimate" : 330, "works" : 1000002, "advanced" : 1, "needTime" : 1000000, "needYield" : 0, "saveState" : 7812, "restoreState" : 7812, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 1000000
  • 36. We need an index > db.users.createIndex( { name : 1 } ) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
  • 37. Indexes Overview •  Parameters •  Background : Create an index in the background as opposed to locking the database •  Unique : All keys in the collection must be unique. Duplicate key insertions will be rejected with an error. •  Name : explicitly name an index. Otherwise the index name is selfgenerated from the index fields. •  Deleting an Index •  db.users.dropIndex({ “name” : 1 }) •  Get All the Indexes on a collection •  db.users.getIndexes()
  • 38. Query Plan Execution Stages • COLLSCAN : for a collection scan • IXSCAN : for scanning index keys • FETCH : for retrieving documents • SHARD_MERGE : for merging results from shards
  • 39. Add an Index > db.users.find( {"name" : "USER_999"} ).explain("executionStats").executionStats { "executionSuccess" : true, "nReturned" : 1, "executionTimeMillis" : 0, "totalKeysExamined" : 1, "totalDocsExamined" : 1, …
  • 40. Execution Stage "executionStages" : { "stage" : "FETCH", "nReturned" : 1, "executionTimeMillisEstimate" : 0, "docsExamined" : 1,, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 1, "executionTimeMillisEstimate" : 0, "keyPattern" : { "username" : 1}, "indexName" : "username_1", "indexVersion" : 1, "direction" : "forward", "indexBounds" : { "username" : [ "["USER_999", "USER_999"]”]}, "keysExamined" : 1, "seenInvalidated" : 0 } } }
  • 42. ¿Qué hemos aprendido? • Cómo crear una base de datos y una colección • Cómo insertar documentos • Cómo realizar búsquedas • Cómo hacer modificaciones de los documentos existentes • Cómo borrar documentos • Cómo comprobar la eficiencia de una operación • Cómo crear índices • Cómo revisar si los índices se utilizan en una operación
  • 43. Próximo Webinar Introducción a los ReplicaSets •  9 de Mayo 2017 – 16:00 CEST, 11:00 ART, 9:00 CDT •  Cómo garantizar que sus datos son durables •  Cómo recuperarse de los fallos automáticamente •  Cómo escribir código de cliente seguro •  Regístrese en : https://www.mongodb.com/webinars •  Denos su opinión, por favor: back-to-basics@mongodb.com