SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
Introducción




               Jonathan Corona
               @soequelle
• Orientado a documentos
   ◦ Los documentos (objetos) se corresponden directamente con los distintos tipos de datos de
      los lenguajes de programación
   ◦ Los documentos embebidos y los arrays minimizan la necesidad de usar joins
   ◦ Ausencia de joins y de transacciones (de varios objetos) para lograr un alto rendimiento y
      una escalabilidad fácil
• Alto rendimiento
   ◦ Sin joins ni transacciones las lecturas y las escrituras son más rápidas
   ◦ Índices en documentos embebidos y arrays
• Alta disponibilidad
   ◦ Servidores replicados con mecanismos automáticos de sustitución de un servidor maestro en
      caso de fallo
• Fácil escalabilidad
   ◦ Lecturas consistentes eventualmente y distribuidas sobre los servidores replicados
   ◦ Sharding automátizado (auto-partición de los datos a lo largo de varios servidores)
        ■ Las lecturas y las escrituras se distribuyen sobre los distintos shards
             ■ La ausencia de joins y de transacciones hace que las consultas distribuidas sean
                más fáciles y sencillas
• Lenguaje de consulta muy completo y rico
http://try.mongodb.org/
http://www.mongodb.org/display/DOCSES/Descarga
Iniciando el daemon de la base de datos


bin/mongod --dbpath data


Iniciando el daemon de la base de datos


bin/mongo
Iniciando el daemon de la base de datos




Iniciando el daemon de la base de datos




       Listando bases de datos

> show dbs
admin
basket
local
Crear y Accesar una base de datos


> use blog
switched to db blog

         Listar colecciones


> show collections
>

        Crear una coleccion


> db.createCollection('posts')
{ "ok" : 1 }
>
         Listar colecciones


> show collections
posts
system.indexes
Agregando documentos


           > use blog
           switched to db blog

                 Agregando documentos


> db.posts.insert({'author_id':'1','uri':'/posts/','title':'El leon
hambriento','summary':'Un leon con mucha hambre','body':'Cronica de
sobre un cuento infantil...'})



> db.posts.insert({'author_id':'1','uri':'/posts/','title':'El leon
hambriento','summary':'Un leon con mucha hambre','body':'Cronica de
sobre un cuento infantil...','comments':
{'name':'Juan','email':'juan@juan.org','content':'Buen post'}})})
Consultando documentos


> db.posts.find()
{ "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "author_id" : "1", "uri" : "/
posts/", "title" : "El leon hambriento", "summary" : "Un leon con mucha hambre",
"body" : "Cronica de sobre un cuento infantil...", "comments" : { "name" :
"Juan", "email" : "juan@juan.org", "content" : "Buen post" } }
{ "_id" : ObjectId("4d2cca604f8e825e1172d59e"), "author_id" : "2", "uri" : "/
posts/2", "title" : "El leon hambriento2", "summary" : "Un leon con mucha
hambre2", "body" : "Cronica de sobre un cuento infantil...2", "comments" :
{ "name" : "Juan2", "email" : "juan2@juan.org", "content" : "Buen post2" } }
{ "_id" : ObjectId("4d2cca844f8e825e1172d59f"), "author_id" : "3", "uri" : "/
posts/3", "title" : "El leon hambriento3", "summary" : "Un leon con mucha
hambre3", "body" : "Cronica de sobre un cuento infantil...3", "comments" :
{ "name" : "Juan3", "email" : "juan3@juan.org", "content" : "Buen post3" } }
{ "_id" : ObjectId("4d2ccaa94f8e825e1172d5a0"), "author_id" : "4", "uri" : "/
posts/4", "title" : "El leon hambriento4", "summary" : "Un leon con mucha
hambre4", "body" : "Cronica de sobre un cuento infantil...4", "comments" :
{ "name" : "Juan4", "email" : "juan4@juan.org", "content" : "Buen post4" } }
{ "_id" : ObjectId("4d2ccade4f8e825e1172d5a1"), "author_id" : "5", "uri" : "/
posts/5", "title" : "El leon hambriento5", "summary" : "Un leon con mucha
hambre5", "body" : "Cronica de sobre un cuento infantil...5", "comments" :
{ "name" : "Juan5", "email" : "juan5@juan.org", "content" : "Buen post5" } }
>
Consultando documentos




> db.posts.findOne()
{
! "_id" : ObjectId("4d2cca114f8e825e1172d59d"),
! "author_id" : "1",
! "uri" : "/posts/",
! "title" : "El leon hambriento",
! "summary" : "Un leon con mucha hambre",
! "body" : "Cronica de sobre un cuento infantil...",
! "comments" : {
! ! "name" : "Juan",
! ! "email" : "juan@juan.org",
! ! "content" : "Buen post"
! }
}
>
Consultando documentos


> db.posts.find({'author_id':'1'})
{ "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "author_id" : "1", "uri" : "/
posts/", "title" : "El leon hambriento", "summary" : "Un leon con mucha hambre",
"body" : "Cronica de sobre un cuento infantil...", "comments" : { "name" :
"Juan", "email" : "juan@juan.org", "content" : "Buen post" } }
>



> db.posts.find({'comments.name':'Juan'})
{ "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "author_id" : "1", "uri" : "/
posts/", "title" : "El leon hambriento", "summary" : "Un leon con mucha hambre",
"body" : "Cronica de sobre un cuento infantil...", "comments" : { "name" :
"Juan", "email" : "juan@juan.org", "content" : "Buen post" } }
>

                      Número de documentos

> db.posts.find().count()
5
>
Limitando consulta de documentos
> db.posts.find().limit(2)
{ "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "author_id" : "1", "uri" : "/
posts/", "title" : "El leon hambriento", "summary" : "Un leon con mucha hambre",
"body" : "Cronica de sobre un cuento infantil...", "comments" : { "name" :
"Juan", "email" : "juan@juan.org", "content" : "Buen post" } }
{ "_id" : ObjectId("4d2cca604f8e825e1172d59e"), "author_id" : "2", "uri" : "/
posts/2", "title" : "El leon hambriento2", "summary" : "Un leon con mucha
hambre2", "body" : "Cronica de sobre un cuento infantil...2", "comments" :
{ "name" : "Juan2", "email" : "juan2@juan.org", "content" : "Buen post2" } }
>


                      Omitiendo documentos
 > db.posts.find().skip(2)
 { "_id" : ObjectId("4d2cca844f8e825e1172d59f"), "author_id" : "3", "uri" :   "/
 posts/3", "title" : "El leon hambriento3", "summary" : "Un leon con mucha
 hambre3", "body" : "Cronica de sobre un cuento infantil...3", "comments" :
 { "name" : "Juan3", "email" : "juan3@juan.org", "content" : "Buen post3" }   }
 { "_id" : ObjectId("4d2ccaa94f8e825e1172d5a0"), "author_id" : "4", "uri" :   "/
 posts/4", "title" : "El leon hambriento4", "summary" : "Un leon con mucha
 hambre4", "body" : "Cronica de sobre un cuento infantil...4", "comments" :
 { "name" : "Juan4", "email" : "juan4@juan.org", "content" : "Buen post4" }   }
 { "_id" : ObjectId("4d2ccade4f8e825e1172d5a1"), "author_id" : "5", "uri" :   "/
 posts/5", "title" : "El leon hambriento5", "summary" : "Un leon con mucha
 hambre5", "body" : "Cronica de sobre un cuento infantil...5", "comments" :
 { "name" : "Juan5", "email" : "juan5@juan.org", "content" : "Buen post5" }   }
 >
Ordenando Documentos


> db.posts.find().sort({author_id:-1})
{ "_id" : ObjectId("4d2ccade4f8e825e1172d5a1"), "author_id" : "5", "uri" : "/
posts/5", "title" : "El leon hambriento5", "summary" : "Un leon con mucha
hambre5", "body" : "Cronica de sobre un cuento infantil...5", "comments" :
{ "name" : "Juan5", "email" : "juan5@juan.org", "content" : "Buen post5" } }
{ "_id" : ObjectId("4d2ccaa94f8e825e1172d5a0"), "author_id" : "4", "uri" : "/
posts/4", "title" : "El leon hambriento4", "summary" : "Un leon con mucha
hambre4", "body" : "Cronica de sobre un cuento infantil...4", "comments" :
{ "name" : "Juan4", "email" : "juan4@juan.org", "content" : "Buen post4" } }
{ "_id" : ObjectId("4d2cca844f8e825e1172d59f"), "author_id" : "3", "uri" : "/
posts/3", "title" : "El leon hambriento3", "summary" : "Un leon con mucha
hambre3", "body" : "Cronica de sobre un cuento infantil...3", "comments" :
{ "name" : "Juan3", "email" : "juan3@juan.org", "content" : "Buen post3" } }
{ "_id" : ObjectId("4d2cca604f8e825e1172d59e"), "author_id" : "2", "uri" : "/
posts/2", "title" : "El leon hambriento2", "summary" : "Un leon con mucha
hambre2", "body" : "Cronica de sobre un cuento infantil...2", "comments" :
{ "name" : "Juan2", "email" : "juan2@juan.org", "content" : "Buen post2" } }
{ "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "author_id" : "1", "uri" : "/
posts/", "title" : "El leon hambriento", "summary" : "Un leon con mucha hambre",
"body" : "Cronica de sobre un cuento infantil...", "comments" : { "name" :
"Juan", "email" : "juan@juan.org", "content" : "Buen post" } }
>
Consultando documentos

> db.posts.find().skip(2).limit(2).sort({author_id:-1})
{ "_id" : ObjectId("4d2cca844f8e825e1172d59f"), "author_id" : "3", "uri" :   "/
posts/3", "title" : "El leon hambriento3", "summary" : "Un leon con mucha
hambre3", "body" : "Cronica de sobre un cuento infantil...3", "comments" :
{ "name" : "Juan3", "email" : "juan3@juan.org", "content" : "Buen post3" }   }
{ "_id" : ObjectId("4d2cca604f8e825e1172d59e"), "author_id" : "2", "uri" :   "/
posts/2", "title" : "El leon hambriento2", "summary" : "Un leon con mucha
hambre2", "body" : "Cronica de sobre un cuento infantil...2", "comments" :
{ "name" : "Juan2", "email" : "juan2@juan.org", "content" : "Buen post2" }   }
>



                           Operadores

•   $gt: Valores mayores que el que le pasamos.
•   $lt: Valores menores que el que le pasamos.
•   $gte: Valores mayores o iguales que el que le pasamos.
•   $lte: Valores menores o iguales que el que le pasamos.
•   $ne: Distinto
•   $in: Si el valor esta entre los que le pasamos como array.
Actualización de documentos
>   var dato=db.posts.findOne({'author_id':'3'})
>   dato
{
!    "_id" : ObjectId("4d2cca844f8e825e1172d59f"),
!    "author_id" : "3",
!    "uri" : "/posts/3",
!    "title" : "El leon hambriento3",
!    "summary" : "Un leon con mucha hambre3",
!    "body" : "Cronica de sobre un cuento infantil...3",
!    "comments" : {
!    ! "name" : "Juan3",
!    ! "email" : "juan3@juan.org",
!    ! "content" : "Buen post3"
!    }
}
>   dato['create']=new Date(2011,01,11)
>   db.posts.save(dato)
>   db.posts.findOne({'author_id':'3'})
{
!    "_id" : ObjectId("4d2cca844f8e825e1172d59f"),
!    "author_id" : "3",
!    "uri" : "/posts/3",
!    "title" : "El leon hambriento3",
!    "summary" : "Un leon con mucha hambre3",
!    "body" : "Cronica de sobre un cuento infantil...3",
!    "comments" : {
!    ! "name" : "Juan3",
!    ! "email" : "juan3@juan.org",
!    ! "content" : "Buen post3"
!    },
!    "create" : "Fri Feb 11 2011 00:00:00 GMT-0600 (CST)"
}
Actualización de documentos

> db.posts.update({'author_id':'3'},{'$set':{'uri':'/post/30'}})




                        Eliminando documentos
> db.posts.remove({'author_id':'2'})
> db.posts.find()
{ "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "uri" : "vacio", "title" : "vacio" }
{ "_id" : ObjectId("4d2ccaa94f8e825e1172d5a0"), "uri" : "vacio", "title" : "vacio" }
{ "_id" : ObjectId("4d2ccade4f8e825e1172d5a1"), "author_id" : "5", "uri" : "/posts/5",
"title" : "El leon hambriento5", "summary" : "Un leon con mucha hambre5", "body" :
"Cronica de sobre un cuento infantil...5", "comments" : { "name" : "Juan5", "email" :
"juan5@juan.org", "content" : "Buen post5" } }
{ "_id" : ObjectId("4d2cca844f8e825e1172d59f"), "uri" : "vacio", "title" : "vacio" }
>
http://rickosborne.org/download/SQL-to-MongoDB.pdf
Muchas gracias



                 Jonathan Corona
                 @soequelle

Más contenido relacionado

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Introduccion mongodb

  • 1. Introducción Jonathan Corona @soequelle
  • 2. • Orientado a documentos ◦ Los documentos (objetos) se corresponden directamente con los distintos tipos de datos de los lenguajes de programación ◦ Los documentos embebidos y los arrays minimizan la necesidad de usar joins ◦ Ausencia de joins y de transacciones (de varios objetos) para lograr un alto rendimiento y una escalabilidad fácil • Alto rendimiento ◦ Sin joins ni transacciones las lecturas y las escrituras son más rápidas ◦ Índices en documentos embebidos y arrays • Alta disponibilidad ◦ Servidores replicados con mecanismos automáticos de sustitución de un servidor maestro en caso de fallo • Fácil escalabilidad ◦ Lecturas consistentes eventualmente y distribuidas sobre los servidores replicados ◦ Sharding automátizado (auto-partición de los datos a lo largo de varios servidores) ■ Las lecturas y las escrituras se distribuyen sobre los distintos shards ■ La ausencia de joins y de transacciones hace que las consultas distribuidas sean más fáciles y sencillas • Lenguaje de consulta muy completo y rico
  • 5. Iniciando el daemon de la base de datos bin/mongod --dbpath data Iniciando el daemon de la base de datos bin/mongo
  • 6. Iniciando el daemon de la base de datos Iniciando el daemon de la base de datos Listando bases de datos > show dbs admin basket local
  • 7.
  • 8. Crear y Accesar una base de datos > use blog switched to db blog Listar colecciones > show collections > Crear una coleccion > db.createCollection('posts') { "ok" : 1 } > Listar colecciones > show collections posts system.indexes
  • 9. Agregando documentos > use blog switched to db blog Agregando documentos > db.posts.insert({'author_id':'1','uri':'/posts/','title':'El leon hambriento','summary':'Un leon con mucha hambre','body':'Cronica de sobre un cuento infantil...'}) > db.posts.insert({'author_id':'1','uri':'/posts/','title':'El leon hambriento','summary':'Un leon con mucha hambre','body':'Cronica de sobre un cuento infantil...','comments': {'name':'Juan','email':'juan@juan.org','content':'Buen post'}})})
  • 10. Consultando documentos > db.posts.find() { "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "author_id" : "1", "uri" : "/ posts/", "title" : "El leon hambriento", "summary" : "Un leon con mucha hambre", "body" : "Cronica de sobre un cuento infantil...", "comments" : { "name" : "Juan", "email" : "juan@juan.org", "content" : "Buen post" } } { "_id" : ObjectId("4d2cca604f8e825e1172d59e"), "author_id" : "2", "uri" : "/ posts/2", "title" : "El leon hambriento2", "summary" : "Un leon con mucha hambre2", "body" : "Cronica de sobre un cuento infantil...2", "comments" : { "name" : "Juan2", "email" : "juan2@juan.org", "content" : "Buen post2" } } { "_id" : ObjectId("4d2cca844f8e825e1172d59f"), "author_id" : "3", "uri" : "/ posts/3", "title" : "El leon hambriento3", "summary" : "Un leon con mucha hambre3", "body" : "Cronica de sobre un cuento infantil...3", "comments" : { "name" : "Juan3", "email" : "juan3@juan.org", "content" : "Buen post3" } } { "_id" : ObjectId("4d2ccaa94f8e825e1172d5a0"), "author_id" : "4", "uri" : "/ posts/4", "title" : "El leon hambriento4", "summary" : "Un leon con mucha hambre4", "body" : "Cronica de sobre un cuento infantil...4", "comments" : { "name" : "Juan4", "email" : "juan4@juan.org", "content" : "Buen post4" } } { "_id" : ObjectId("4d2ccade4f8e825e1172d5a1"), "author_id" : "5", "uri" : "/ posts/5", "title" : "El leon hambriento5", "summary" : "Un leon con mucha hambre5", "body" : "Cronica de sobre un cuento infantil...5", "comments" : { "name" : "Juan5", "email" : "juan5@juan.org", "content" : "Buen post5" } } >
  • 11. Consultando documentos > db.posts.findOne() { ! "_id" : ObjectId("4d2cca114f8e825e1172d59d"), ! "author_id" : "1", ! "uri" : "/posts/", ! "title" : "El leon hambriento", ! "summary" : "Un leon con mucha hambre", ! "body" : "Cronica de sobre un cuento infantil...", ! "comments" : { ! ! "name" : "Juan", ! ! "email" : "juan@juan.org", ! ! "content" : "Buen post" ! } } >
  • 12. Consultando documentos > db.posts.find({'author_id':'1'}) { "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "author_id" : "1", "uri" : "/ posts/", "title" : "El leon hambriento", "summary" : "Un leon con mucha hambre", "body" : "Cronica de sobre un cuento infantil...", "comments" : { "name" : "Juan", "email" : "juan@juan.org", "content" : "Buen post" } } > > db.posts.find({'comments.name':'Juan'}) { "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "author_id" : "1", "uri" : "/ posts/", "title" : "El leon hambriento", "summary" : "Un leon con mucha hambre", "body" : "Cronica de sobre un cuento infantil...", "comments" : { "name" : "Juan", "email" : "juan@juan.org", "content" : "Buen post" } } > Número de documentos > db.posts.find().count() 5 >
  • 13. Limitando consulta de documentos > db.posts.find().limit(2) { "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "author_id" : "1", "uri" : "/ posts/", "title" : "El leon hambriento", "summary" : "Un leon con mucha hambre", "body" : "Cronica de sobre un cuento infantil...", "comments" : { "name" : "Juan", "email" : "juan@juan.org", "content" : "Buen post" } } { "_id" : ObjectId("4d2cca604f8e825e1172d59e"), "author_id" : "2", "uri" : "/ posts/2", "title" : "El leon hambriento2", "summary" : "Un leon con mucha hambre2", "body" : "Cronica de sobre un cuento infantil...2", "comments" : { "name" : "Juan2", "email" : "juan2@juan.org", "content" : "Buen post2" } } > Omitiendo documentos > db.posts.find().skip(2) { "_id" : ObjectId("4d2cca844f8e825e1172d59f"), "author_id" : "3", "uri" : "/ posts/3", "title" : "El leon hambriento3", "summary" : "Un leon con mucha hambre3", "body" : "Cronica de sobre un cuento infantil...3", "comments" : { "name" : "Juan3", "email" : "juan3@juan.org", "content" : "Buen post3" } } { "_id" : ObjectId("4d2ccaa94f8e825e1172d5a0"), "author_id" : "4", "uri" : "/ posts/4", "title" : "El leon hambriento4", "summary" : "Un leon con mucha hambre4", "body" : "Cronica de sobre un cuento infantil...4", "comments" : { "name" : "Juan4", "email" : "juan4@juan.org", "content" : "Buen post4" } } { "_id" : ObjectId("4d2ccade4f8e825e1172d5a1"), "author_id" : "5", "uri" : "/ posts/5", "title" : "El leon hambriento5", "summary" : "Un leon con mucha hambre5", "body" : "Cronica de sobre un cuento infantil...5", "comments" : { "name" : "Juan5", "email" : "juan5@juan.org", "content" : "Buen post5" } } >
  • 14. Ordenando Documentos > db.posts.find().sort({author_id:-1}) { "_id" : ObjectId("4d2ccade4f8e825e1172d5a1"), "author_id" : "5", "uri" : "/ posts/5", "title" : "El leon hambriento5", "summary" : "Un leon con mucha hambre5", "body" : "Cronica de sobre un cuento infantil...5", "comments" : { "name" : "Juan5", "email" : "juan5@juan.org", "content" : "Buen post5" } } { "_id" : ObjectId("4d2ccaa94f8e825e1172d5a0"), "author_id" : "4", "uri" : "/ posts/4", "title" : "El leon hambriento4", "summary" : "Un leon con mucha hambre4", "body" : "Cronica de sobre un cuento infantil...4", "comments" : { "name" : "Juan4", "email" : "juan4@juan.org", "content" : "Buen post4" } } { "_id" : ObjectId("4d2cca844f8e825e1172d59f"), "author_id" : "3", "uri" : "/ posts/3", "title" : "El leon hambriento3", "summary" : "Un leon con mucha hambre3", "body" : "Cronica de sobre un cuento infantil...3", "comments" : { "name" : "Juan3", "email" : "juan3@juan.org", "content" : "Buen post3" } } { "_id" : ObjectId("4d2cca604f8e825e1172d59e"), "author_id" : "2", "uri" : "/ posts/2", "title" : "El leon hambriento2", "summary" : "Un leon con mucha hambre2", "body" : "Cronica de sobre un cuento infantil...2", "comments" : { "name" : "Juan2", "email" : "juan2@juan.org", "content" : "Buen post2" } } { "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "author_id" : "1", "uri" : "/ posts/", "title" : "El leon hambriento", "summary" : "Un leon con mucha hambre", "body" : "Cronica de sobre un cuento infantil...", "comments" : { "name" : "Juan", "email" : "juan@juan.org", "content" : "Buen post" } } >
  • 15. Consultando documentos > db.posts.find().skip(2).limit(2).sort({author_id:-1}) { "_id" : ObjectId("4d2cca844f8e825e1172d59f"), "author_id" : "3", "uri" : "/ posts/3", "title" : "El leon hambriento3", "summary" : "Un leon con mucha hambre3", "body" : "Cronica de sobre un cuento infantil...3", "comments" : { "name" : "Juan3", "email" : "juan3@juan.org", "content" : "Buen post3" } } { "_id" : ObjectId("4d2cca604f8e825e1172d59e"), "author_id" : "2", "uri" : "/ posts/2", "title" : "El leon hambriento2", "summary" : "Un leon con mucha hambre2", "body" : "Cronica de sobre un cuento infantil...2", "comments" : { "name" : "Juan2", "email" : "juan2@juan.org", "content" : "Buen post2" } } > Operadores • $gt: Valores mayores que el que le pasamos. • $lt: Valores menores que el que le pasamos. • $gte: Valores mayores o iguales que el que le pasamos. • $lte: Valores menores o iguales que el que le pasamos. • $ne: Distinto • $in: Si el valor esta entre los que le pasamos como array.
  • 16. Actualización de documentos > var dato=db.posts.findOne({'author_id':'3'}) > dato { ! "_id" : ObjectId("4d2cca844f8e825e1172d59f"), ! "author_id" : "3", ! "uri" : "/posts/3", ! "title" : "El leon hambriento3", ! "summary" : "Un leon con mucha hambre3", ! "body" : "Cronica de sobre un cuento infantil...3", ! "comments" : { ! ! "name" : "Juan3", ! ! "email" : "juan3@juan.org", ! ! "content" : "Buen post3" ! } } > dato['create']=new Date(2011,01,11) > db.posts.save(dato) > db.posts.findOne({'author_id':'3'}) { ! "_id" : ObjectId("4d2cca844f8e825e1172d59f"), ! "author_id" : "3", ! "uri" : "/posts/3", ! "title" : "El leon hambriento3", ! "summary" : "Un leon con mucha hambre3", ! "body" : "Cronica de sobre un cuento infantil...3", ! "comments" : { ! ! "name" : "Juan3", ! ! "email" : "juan3@juan.org", ! ! "content" : "Buen post3" ! }, ! "create" : "Fri Feb 11 2011 00:00:00 GMT-0600 (CST)" }
  • 17. Actualización de documentos > db.posts.update({'author_id':'3'},{'$set':{'uri':'/post/30'}}) Eliminando documentos > db.posts.remove({'author_id':'2'}) > db.posts.find() { "_id" : ObjectId("4d2cca114f8e825e1172d59d"), "uri" : "vacio", "title" : "vacio" } { "_id" : ObjectId("4d2ccaa94f8e825e1172d5a0"), "uri" : "vacio", "title" : "vacio" } { "_id" : ObjectId("4d2ccade4f8e825e1172d5a1"), "author_id" : "5", "uri" : "/posts/5", "title" : "El leon hambriento5", "summary" : "Un leon con mucha hambre5", "body" : "Cronica de sobre un cuento infantil...5", "comments" : { "name" : "Juan5", "email" : "juan5@juan.org", "content" : "Buen post5" } } { "_id" : ObjectId("4d2cca844f8e825e1172d59f"), "uri" : "vacio", "title" : "vacio" } >
  • 19. Muchas gracias Jonathan Corona @soequelle