Crear Base de Datos Mongodb
Crear Base de Datos Mongodb
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
use BD_Escuela1
go
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
C) Create Collection
The createCollection() Method
Syntax
Options Document (Optional) Specify options about memory size and indexing
D) Insert Document
The insert() Method
To insert data into MongoDB collection, you need to use MongoDB's insert() or save() method.
Syntax
F) drop() Method
The drop() Method
Syntax
drop() method will return true, if the selected collection is dropped successfully, otherwise it will return false.
To query data from MongoDB collection, you need to use MongoDB's find() method.
Syntax
Syntax
>db.COLLECTION_NAME.find().pretty()
Bitácora de entendimiento
Ejercicios
--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
Código
Código
> db
BD_GrupoAlumno
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
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"
}
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"
}
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"
}
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.