0% found this document useful (0 votes)
57 views

Crear Base de Datos Mongodb

MongoDB is a cross-platform document-oriented database that provides high performance, high availability, and easy scalability. It works on the concepts of databases, collections, and documents. A database stores collections, collections store documents, and documents are analogous to rows in a table in a relational database. MongoDB does not enforce a schema so documents in a collection can have different fields. The document shows how MongoDB terminology maps to relational database terminology.

Uploaded by

Florentino Games
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Crear Base de Datos Mongodb

MongoDB is a cross-platform document-oriented database that provides high performance, high availability, and easy scalability. It works on the concepts of databases, collections, and documents. A database stores collections, collections store documents, and documents are analogous to rows in a table in a relational database. MongoDB does not enforce a schema so documents in a collection can have different fields. The document shows how MongoDB terminology maps to relational database terminology.

Uploaded by

Florentino Games
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Administración de Base de Datos

Unidad Tema Catedrático


Ricardo Muro

Cuando más Fuertes sean tus pruebas, Más Grandes serán tus Victorias

MongoDB - Overview

MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and
easy scalability. MongoDB works on concept of collection and document.

Database
Database is a physical container for collections. Each database gets its own set of files on the file system. A single
MongoDB server typically has multiple databases.

Collection
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a
single database. Collections do not enforce a schema. Documents within a collection can have different fields.
Typically, all documents in a collection are of similar or related purpose.

Document
A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents
in the same collection do not need to have the same set of fields or structure, and common fields in a collection's
documents may hold different types of data.

The following table shows the relationship of RDBMS terminology with MongoDB.
0) Crear la Base de datos BD_Escuela1 en MongoDB
use master
go

Create DataBase BD_Escuela1


go

use BD_Escuela1
go

Create Table Materia


(
cveMat char(4) not null,
nomMat varchar(100) not null,

constraint pk_cveMat Primary Key(cveMat)


)
go

Insert Into Materia(cveMat,nomMat)values('1000','Ingles')


Insert Into Materia(cveMat,nomMat)values('2000','BD')
go

A) To check your currently selected database, use the command db


>db
current database

1) check your currently selected database


> db
test

B) Create Database
The use Command

MongoDB use DATABASE_NAME is used to create database. The command will create a new database if it doesn't
exist, otherwise it will return the existing database.

Syntax

Basic syntax of use DATABASE statement is as follows −


use DATABASE_NAME

2) Crear la Base de datos BD_Escuela1


> use BD_Escuela1
switched to db BD_Escuela1

3) Verifica base de datos actual


> db
BD_Escuela1
4) Crea la colección (tabla) Materia
> db.createCollection("Materia")
{ "ok" : 1 }

C) Create Collection
The createCollection() Method

MongoDB db.createCollection(name, options) is used to create collection.

Syntax

Basic syntax of createCollection() command is as follows −


db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document and is used to specify
configuration of collection.

Parameter Type Description

Name String Name of the collection to be created

Options Document (Optional) Specify options about memory size and indexing

5) Inserta un documento (registro) en la colección Materia


> db.Materia.insert({"cveMat":"1000","nomMat":"Ingles"})
WriteResult({ "nInserted" : 1 })

D) Insert Document
The insert() Method

To insert data into MongoDB collection, you need to use MongoDB's insert() or save() method.

Syntax

The basic syntax of insert() command is as follows −


>db.COLLECTION_NAME.insert(document)

6) Mostrar las colecciones de la base de datos en uso


> show collections
Materia

7) Crea la colección Grupo


> db.createCollection("Grupo")
{ "ok" : 1 }
8) Mostrar las colecciones de la base de datos en uso
> show collections
Grupo
Materia

F) drop() Method
The drop() Method

MongoDB's db.collection.drop() is used to drop a collection from the database.

Syntax

Basic syntax of drop() command is as follows −


db.COLLECTION_NAME.drop()

drop() method will return true, if the selected collection is dropped successfully, otherwise it will return false.

9) Elimina la colección Grupo


> db.Grupo.drop()
true

10) Mostrar las colecciones de la base de datos en uso


> show collections
Materia

G) Query Document The find() Method


The find() Method

To query data from MongoDB collection, you need to use MongoDB's find() method.

Syntax

The basic syntax of find() method is as follows −


>db.COLLECTION_NAME.find()
find() method will display all the documents in a non-structured way.

H) The pretty() Method


To display the results in a formatted way, you can use pretty() method.

Syntax

>db.COLLECTION_NAME.find().pretty()

11) Muestra los documentos de la colección Materia


> db.Materia.find().pretty()
{
"_id" : ObjectId("6206e56d2dca83f3e4519885"),
"cveMat" : "1000",
"nomMat" : "Ingles"
}
12) Inserta otro documento (registro) en la colección Materia
> db.Materia.insert({"cveMat":"2000","nomMat":"BD"})
WriteResult({ "nInserted" : 1 })

13) Muestra los documentos de la colección Materia


> db.Materia.find().pretty()
{
"_id" : ObjectId("6206e56d2dca83f3e4519885"),
"cveMat" : "1000",
"nomMat" : "Ingles"
}
{
"_id" : ObjectId("6206e72d2dca83f3e4519886"),
"cveMat" : "2000",
"nomMat" : "BD"
}

Bitácora de entendimiento

Entendí todo Entendí casi todo Entendí muy poco

Pregunta para el instructor (Opcional)


¿?

Ejercicios

0) Crear la Base de datos BD_GrupoAlumno en MongoDB


--/////////////////////
--SQL Ing. Ricardo Muro
--/////////////////////

--Establece en uso la base de datos master


use master
go

--Crea la base de datos


Create DataBase BD_GrupoAlumno
go

--Establece en uso la base de datos


use BD_GrupoAlumno
go

--Creación de la tabla Grupo


Create Table Grupo
(
cveGru char(7) not null, --pk
nomGru varchar(50) not null,
constraint pk_cveGru Primary Key(cveGru)
)
go

--Creación de la tabla Alumno


Create Table Alumno
(
cveAlu char(4) not null, --pk
nomAlu varchar(100) not null,
edaAlu tinyint not null,
cveGru char(7) not null, --fk

Constraint pk_cveAlu Primary Key(cveAlu),


Constraint fk_cveGru Foreign Key(cveGru)References Grupo(cveGru) On Delete Cascade
)
go

--Agregar 3 Grupos
Insert Into Grupo(cveGru,nomGru)values('GSI0511','Aguilas')
Insert Into Grupo(cveGru,nomGru)values('GSI0522','Chivas')
Insert Into Grupo(cveGru,nomGru)values('GSI0533','Pumas')
go

--Agregar 6 Alumnos
Insert Into Alumno(cveAlu,nomAlu,edaAlu,cveGru)values('1000','Francisco',20,'GSI0511')
Insert Into Alumno(cveAlu,nomAlu,edaAlu,cveGru)values('2000','Miguel',15,'GSI0511')
Insert Into Alumno(cveAlu,nomAlu,edaAlu,cveGru)values('3000','Maricela',19,'GSI0511')
Insert Into Alumno(cveAlu,nomAlu,edaAlu,cveGru)values('4000','Carmen',18,'GSI0522')
Insert Into Alumno(cveAlu,nomAlu,edaAlu,cveGru)values('5000','Alma',18,'GSI0522')
Insert Into Alumno(cveAlu,nomAlu,edaAlu,cveGru)values('6000','Guillermo',24,'GSI0522')
go

1) Crear la Base de datos BD_GrupoAlumno

Código

> use BD_GrupoAlumno


switched to db BD_GrupoAlumno

2) Verifica base de datos actual

Código

> db
BD_GrupoAlumno

3) Crea las colecciones e inserta los documentos

Código

> db.createCollection("Grupo")
{ "ok" : 1 }
> db.createCollection("Alumno")
{ "ok" : 1 }
4) Mostar las colecciones de la base de datos en uso

Código

> show collections


Alumno
Grupo

5) Muestra los documentos de la colección Grupo

Código

> db.Grupo.find().pretty()
{
"_id" : ObjectId("6206e99da45ce5016e4dd376"),
"cveGru" : "GSI0511",
"nomGru" : "Aguilas"
}
{
"_id" : ObjectId("6206e9c7a45ce5016e4dd377"),
"cveGru" : "GSI0522",
"nomGru" : "Chivas"
}
{
"_id" : ObjectId("6206e9d4a45ce5016e4dd378"),
"cveGru" : "GSI0533",
"nomGru" : "Pumas"
}

6) Muestra los documentos de la colección Alumno

Código

> db.Alumno.find().pretty()
{
"_id" : ObjectId("6206eb0aa45ce5016e4dd379"),
"cveAlu" : "1000",
"nomAlu" : "Francisco",
"edaAlu" : 20,
"cveGru" : "GSI0511"
}
{
"_id" : ObjectId("6206eb13a45ce5016e4dd37a"),
"cveAlu" : "2000",
"nomAlu" : "Miguel",
"edaAlu" : 15,
"cveGru" : "GSI0511"
}
{
"_id" : ObjectId("6206eb1ca45ce5016e4dd37b"),
"cveAlu" : "3000",
"nomAlu" : "Maricela",
"edaAlu" : 19,
"cveGru" : "GSI0511"
}
{
"_id" : ObjectId("6206eb24a45ce5016e4dd37c"),
"cveAlu" : "4000",
"nomAlu" : "Carmen",
"edaAlu" : 18,
"cveGru" : "GSI0522"
}
{
"_id" : ObjectId("6206eb2ca45ce5016e4dd37d"),
"cveAlu" : "5000",
"nomAlu" : "Alma",
"edaAlu" : 18,
"cveGru" : "GSI0522"
}
{
"_id" : ObjectId("6206eb35a45ce5016e4dd37e"),
"cveAlu" : "6000",
"nomAlu" : "Guillermo",
"edaAlu" : 24,
"cveGru" : "GSI0522"
}

7) Busca documento para Alumno con nombre Alma

Código

> db.Alumno.find({"nomAlu":"Alma"}).pretty()
{
"_id" : ObjectId("6206eb2ca45ce5016e4dd37d"),
"cveAlu" : "5000",
"nomAlu" : "Alma",
"edaAlu" : 18,
"cveGru" : "GSI0522"
}
8) Consultar los primeros dos documentos de la colección Alumno

Código

> db.Alumno.find().limit(2).pretty()
{
"_id" : ObjectId("6206eb0aa45ce5016e4dd379"),
"cveAlu" : "1000",
"nomAlu" : "Francisco",
"edaAlu" : 20,
"cveGru" : "GSI0511"
}
{
"_id" : ObjectId("6206eb13a45ce5016e4dd37a"),
"cveAlu" : "2000",
"nomAlu" : "Miguel",
"edaAlu" : 15,
"cveGru" : "GSI0511"
}

9) Ordena los alumnos de forma ascendente con respecto a su edad

Código

> db.Alumno.find().sort({edaAlu:1});
{ "_id" : ObjectId("6206eb13a45ce5016e4dd37a"), "cveAlu" : "2000", "nomAlu" : "Miguel",
"edaAlu" : 15, "cveGru" : "GSI0511" }
{ "_id" : ObjectId("6206eb24a45ce5016e4dd37c"), "cveAlu" : "4000", "nomAlu" : "Carmen",
"edaAlu" : 18, "cveGru" : "GSI0522" }
{ "_id" : ObjectId("6206eb2ca45ce5016e4dd37d"), "cveAlu" : "5000", "nomAlu" : "Alma",
"edaAlu" : 18, "cveGru" : "GSI0522" }
{ "_id" : ObjectId("6206eb1ca45ce5016e4dd37b"), "cveAlu" : "3000", "nomAlu" : "Maricela",
"edaAlu" : 19, "cveGru" : "GSI0511" }
{ "_id" : ObjectId("6206eb0aa45ce5016e4dd379"), "cveAlu" : "1000", "nomAlu" : "Francisco",
"edaAlu" : 20, "cveGru" : "GSI0511" }
{ "_id" : ObjectId("6206eb35a45ce5016e4dd37e"), "cveAlu" : "6000", "nomAlu" : "Guillermo",
"edaAlu" : 24, "cveGru" : "GSI0522" }
10) Ordena los alumnos de forma descendente con respecto a su edad

Código

> db.Alumno.find().sort({edaAlu:-1});
{ "_id" : ObjectId("6206eb35a45ce5016e4dd37e"), "cveAlu" : "6000", "nomAlu" : "Guillermo",
"edaAlu" : 24, "cveGru" : "GSI0522" }
{ "_id" : ObjectId("6206eb0aa45ce5016e4dd379"), "cveAlu" : "1000", "nomAlu" : "Francisco",
"edaAlu" : 20, "cveGru" : "GSI0511" }
{ "_id" : ObjectId("6206eb1ca45ce5016e4dd37b"), "cveAlu" : "3000", "nomAlu" : "Maricela",
"edaAlu" : 19, "cveGru" : "GSI0511" }
{ "_id" : ObjectId("6206eb24a45ce5016e4dd37c"), "cveAlu" : "4000", "nomAlu" : "Carmen",
"edaAlu" : 18, "cveGru" : "GSI0522" }
{ "_id" : ObjectId("6206eb2ca45ce5016e4dd37d"), "cveAlu" : "5000", "nomAlu" : "Alma",
"edaAlu" : 18, "cveGru" : "GSI0522" }
{ "_id" : ObjectId("6206eb13a45ce5016e4dd37a"), "cveAlu" : "2000", "nomAlu" : "Miguel",
"edaAlu" : 15, "cveGru" : "GSI0511" }

Anexos
If you want to check your databases list, use the command show dbs.
>show dbs
local 0.78125GB
test 0.23012GB
Your created database (BD_Escuela1) is not present in list. To
display database, you need to insert at least one document
into it.

default database is test


In MongoDB default database is test. If you didn't create any database, then collections will be
stored in test database.

You might also like