0% found this document useful (0 votes)
130 views19 pages

MongoDB Shell Commands and Errors

The document shows the steps taken to connect to a MongoDB database named "pujdb" using the mongo shell. It then proceeds to insert sample documents into a collection called "estudiantes" representing students. Various CRUD operations are demonstrated like find, insertOne, deleteOne, updateOne. The operations insert student documents, read the documents, update the GPA field of students with the last name "Perez", and delete students by last name.

Uploaded by

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

MongoDB Shell Commands and Errors

The document shows the steps taken to connect to a MongoDB database named "pujdb" using the mongo shell. It then proceeds to insert sample documents into a collection called "estudiantes" representing students. Various CRUD operations are demonstrated like find, insertOne, deleteOne, updateOne. The operations insert student documents, read the documents, update the GPA field of students with the last name "Perez", and delete students by last name.

Uploaded by

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

Microsoft Windows [Versión 10.0.19042.

928]
(c) Microsoft Corporation. Todos los derechos reservados.

C:\Users\Heyling>cd C:\Program Files\MongoDB\Server\4.4\bin

C:\Program Files\MongoDB\Server\4.4\bin>mongo
MongoDB shell version v4.4.5
connecting to: mongodb://127.0.0.1:27017/?
compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed:
NetworkTimeout: Error connecting to 127.0.0.1:27017 :: caused by :: Socket
operation timed out :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

C:\Program Files\MongoDB\Server\4.4\bin>mongo
MongoDB shell version v4.4.5
connecting to: mongodb://127.0.0.1:27017/?
compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8984068f-b61f-4144-ba6b-8d43ed2b77f6") }
MongoDB server version: 4.4.5
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2021-05-10T07:14:29.326-05:00: Access control is not enabled for the
database. Read and write access to data and configuration is unrestricted
---
---
Enable MongoDB's free cloud-based monitoring service, which will then
receive and display
metrics about your deployment (disk utilization, CPU, operation statistics,
etc).

The monitoring data will be available on a MongoDB website with a unique


URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make
product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command:


db.enableFreeMonitoring()
To permanently disable this reminder, run the following command:
db.disableFreeMonitoring()
---
> use pujdb
switched to db pujdb
> db.estudiantes.inertOne({nombre:"Pedro",apellido:"Perez",promedio:43})
uncaught exception: TypeError: db.estudiantes.inertOne is not a function :
@(shell):1:1
> db.estudiantes.insertOne({nombre:"Pedro",apellido:"Perez",promedio:43})
{
"acknowledged" : true,
"insertedId" : ObjectId("609929175bada2fb2206f0c0")
}
> db.estudiantes.find()
{ "_id" : ObjectId("609929175bada2fb2206f0c0"), "nombre" : "Pedro", "apellido" :
"Perez", "promedio" : 43 }
> db.estudiantes.find().pretty()
{
"_id" : ObjectId("609929175bada2fb2206f0c0"),
"nombre" : "Pedro",
"apellido" : "Perez",
"promedio" : 43
}
> db.estudiantes.insertOne({nombre:"Egan",apellido:"Perez",promedio:48})
{
"acknowledged" : true,
"insertedId" : ObjectId("60992b3d5bada2fb2206f0c1")
}
> db.estudiantes.insertOne({nombre:"Juan",apellido:"Perez",promedio:45})
{
"acknowledged" : true,
"insertedId" : ObjectId("60992b515bada2fb2206f0c2")
}
> db.estudiantes.insertOne({nombre:"Maria",apellido:"Perez",promedio:46})
{
"acknowledged" : true,
"insertedId" : ObjectId("60992b5e5bada2fb2206f0c3")
}
> db.estudiantes.insertOne({nombre:"Juan",apellido:"Cuadrado",promedio:47})
{
"acknowledged" : true,
"insertedId" : ObjectId("60992b735bada2fb2206f0c4")
}
>
db.estudiantes.insertOne({nombre:"Pedro",apellido:"Perez",promedio:43,cedula:123})
{
"acknowledged" : true,
"insertedId" : ObjectId("60992bab5bada2fb2206f0c5")
}
> db.estudiantes.find().pretty()
{
"_id" : ObjectId("609929175bada2fb2206f0c0"),
"nombre" : "Pedro",
"apellido" : "Perez",
"promedio" : 43
}
{
"_id" : ObjectId("60992b3d5bada2fb2206f0c1"),
"nombre" : "Egan",
"apellido" : "Perez",
"promedio" : 48
}
{
"_id" : ObjectId("60992b515bada2fb2206f0c2"),
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 45
}
{
"_id" : ObjectId("60992b5e5bada2fb2206f0c3"),
"nombre" : "Maria",
"apellido" : "Perez",
"promedio" : 46
}
{
"_id" : ObjectId("60992b735bada2fb2206f0c4"),
"nombre" : "Juan",
"apellido" : "Cuadrado",
"promedio" : 47
}
{
"_id" : ObjectId("60992bab5bada2fb2206f0c5"),
"nombre" : "Pedro",
"apellido" : "Perez",
"promedio" : 43,
"cedula" : 123
}
> db.estudiantes.insertOne({nombre:"Juan",apellido:"Perez",promedio:44,
cedula:123456, _id:1})
{ "acknowledged" : true, "insertedId" : 1 }
> db.estudiantes.insertOne({nombre:"Juan",apellido:"Perez",promedio:44,
cedula:123456, _id:1})
WriteError({
"index" : 0,
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: pujdb.estudiantes index:
_id_ dup key: { _id: 1.0 }",
"op" : {
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 44,
"cedula" : 123456,
"_id" : 1
}
}) :
WriteError({
"index" : 0,
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: pujdb.estudiantes index:
_id_ dup key: { _id: 1.0 }",
"op" : {
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 44,
"cedula" : 123456,
"_id" : 1
}
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:264:9
@(shell):1:1
> db.estudiantes.find().pretty()
{
"_id" : ObjectId("609929175bada2fb2206f0c0"),
"nombre" : "Pedro",
"apellido" : "Perez",
"promedio" : 43
}
{
"_id" : ObjectId("60992b3d5bada2fb2206f0c1"),
"nombre" : "Egan",
"apellido" : "Perez",
"promedio" : 48
}
{
"_id" : ObjectId("60992b515bada2fb2206f0c2"),
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 45
}
{
"_id" : ObjectId("60992b5e5bada2fb2206f0c3"),
"nombre" : "Maria",
"apellido" : "Perez",
"promedio" : 46
}
{
"_id" : ObjectId("60992b735bada2fb2206f0c4"),
"nombre" : "Juan",
"apellido" : "Cuadrado",
"promedio" : 47
}
{
"_id" : ObjectId("60992bab5bada2fb2206f0c5"),
"nombre" : "Pedro",
"apellido" : "Perez",
"promedio" : 43,
"cedula" : 123
}
{
"_id" : 1,
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 44,
"cedula" : 123456
}
> db.estudiantes.find().pretty()
{
"_id" : ObjectId("609929175bada2fb2206f0c0"),
"nombre" : "Pedro",
"apellido" : "Perez",
"promedio" : 43
}
{
"_id" : ObjectId("60992b3d5bada2fb2206f0c1"),
"nombre" : "Egan",
"apellido" : "Perez",
"promedio" : 48
}
{
"_id" : ObjectId("60992b515bada2fb2206f0c2"),
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 45
}
{
"_id" : ObjectId("60992b5e5bada2fb2206f0c3"),
"nombre" : "Maria",
"apellido" : "Perez",
"promedio" : 46
}
{
"_id" : ObjectId("60992b735bada2fb2206f0c4"),
"nombre" : "Juan",
"apellido" : "Cuadrado",
"promedio" : 47
}
{
"_id" : ObjectId("60992bab5bada2fb2206f0c5"),
"nombre" : "Pedro",
"apellido" : "Perez",
"promedio" : 43,
"cedula" : 123
}
{
"_id" : 1,
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 44,
"cedula" : 123456
}
> db.estudiantes.deleteOne({apellido:"Perez"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.estudiantes.find().pretty()
{
"_id" : ObjectId("60992b3d5bada2fb2206f0c1"),
"nombre" : "Egan",
"apellido" : "Perez",
"promedio" : 48
}
{
"_id" : ObjectId("60992b515bada2fb2206f0c2"),
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 45
}
{
"_id" : ObjectId("60992b5e5bada2fb2206f0c3"),
"nombre" : "Maria",
"apellido" : "Perez",
"promedio" : 46
}
{
"_id" : ObjectId("60992b735bada2fb2206f0c4"),
"nombre" : "Juan",
"apellido" : "Cuadrado",
"promedio" : 47
}
{
"_id" : ObjectId("60992bab5bada2fb2206f0c5"),
"nombre" : "Pedro",
"apellido" : "Perez",
"promedio" : 43,
"cedula" : 123
}
{
"_id" : 1,
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 44,
"cedula" : 123456
}
> db.estudiantes.find()
{ "_id" : ObjectId("60992b3d5bada2fb2206f0c1"), "nombre" : "Egan", "apellido" :
"Perez", "promedio" : 48 }
{ "_id" : ObjectId("60992b515bada2fb2206f0c2"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 45 }
{ "_id" : ObjectId("60992b5e5bada2fb2206f0c3"), "nombre" : "Maria", "apellido" :
"Perez", "promedio" : 46 }
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 47 }
{ "_id" : ObjectId("60992bab5bada2fb2206f0c5"), "nombre" : "Pedro", "apellido" :
"Perez", "promedio" : 43, "cedula" : 123 }
{ "_id" : 1, "nombre" : "Juan", "apellido" : "Perez", "promedio" : 44, "cedula" :
123456 }
> db.estudiantes.updateOne({apellido:"Perez"},{$set:{promedio:50}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.estudiantes.find()
{ "_id" : ObjectId("60992b3d5bada2fb2206f0c1"), "nombre" : "Egan", "apellido" :
"Perez", "promedio" : 50 }
{ "_id" : ObjectId("60992b515bada2fb2206f0c2"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 45 }
{ "_id" : ObjectId("60992b5e5bada2fb2206f0c3"), "nombre" : "Maria", "apellido" :
"Perez", "promedio" : 46 }
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 47 }
{ "_id" : ObjectId("60992bab5bada2fb2206f0c5"), "nombre" : "Pedro", "apellido" :
"Perez", "promedio" : 43, "cedula" : 123 }
{ "_id" : 1, "nombre" : "Juan", "apellido" : "Perez", "promedio" : 44, "cedula" :
123456 }
> db.estudiantes.updateOne({apellido:"Cuadrado"},{$set:{promedio:50}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.estudiantes.find()
{ "_id" : ObjectId("60992b3d5bada2fb2206f0c1"), "nombre" : "Egan", "apellido" :
"Perez", "promedio" : 50 }
{ "_id" : ObjectId("60992b515bada2fb2206f0c2"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 45 }
{ "_id" : ObjectId("60992b5e5bada2fb2206f0c3"), "nombre" : "Maria", "apellido" :
"Perez", "promedio" : 46 }
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 50 }
{ "_id" : ObjectId("60992bab5bada2fb2206f0c5"), "nombre" : "Pedro", "apellido" :
"Perez", "promedio" : 43, "cedula" : 123 }
{ "_id" : 1, "nombre" : "Juan", "apellido" : "Perez", "promedio" : 44, "cedula" :
123456 }
> db.estudiantes.updateOne({apellido:"Perez"},{$set:{estatura:170}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.estudiantes.find()
{ "_id" : ObjectId("60992b3d5bada2fb2206f0c1"), "nombre" : "Egan", "apellido" :
"Perez", "promedio" : 50, "estatura" : 170 }
{ "_id" : ObjectId("60992b515bada2fb2206f0c2"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 45 }
{ "_id" : ObjectId("60992b5e5bada2fb2206f0c3"), "nombre" : "Maria", "apellido" :
"Perez", "promedio" : 46 }
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 50 }
{ "_id" : ObjectId("60992bab5bada2fb2206f0c5"), "nombre" : "Pedro", "apellido" :
"Perez", "promedio" : 43, "cedula" : 123 }
{ "_id" : 1, "nombre" : "Juan", "apellido" : "Perez", "promedio" : 44, "cedula" :
123456 }
> db.estudiantes.find().pretty()
{
"_id" : ObjectId("60992b3d5bada2fb2206f0c1"),
"nombre" : "Egan",
"apellido" : "Perez",
"promedio" : 50,
"estatura" : 170
}
{
"_id" : ObjectId("60992b515bada2fb2206f0c2"),
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 45
}
{
"_id" : ObjectId("60992b5e5bada2fb2206f0c3"),
"nombre" : "Maria",
"apellido" : "Perez",
"promedio" : 46
}
{
"_id" : ObjectId("60992b735bada2fb2206f0c4"),
"nombre" : "Juan",
"apellido" : "Cuadrado",
"promedio" : 50
}
{
"_id" : ObjectId("60992bab5bada2fb2206f0c5"),
"nombre" : "Pedro",
"apellido" : "Perez",
"promedio" : 43,
"cedula" : 123
}
{
"_id" : 1,
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 44,
"cedula" : 123456
}
> db.estudiantes.updateMany({},{$set:{promedio:23}})
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
> db.estudiantes.updateMany({},{$set:{estado:"activo"}})
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
> db.estudiantes.find()
{ "_id" : ObjectId("60992b3d5bada2fb2206f0c1"), "nombre" : "Egan", "apellido" :
"Perez", "promedio" : 23, "estatura" : 170, "estado" : "activo" }
{ "_id" : ObjectId("60992b515bada2fb2206f0c2"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 23, "estado" : "activo" }
{ "_id" : ObjectId("60992b5e5bada2fb2206f0c3"), "nombre" : "Maria", "apellido" :
"Perez", "promedio" : 23, "estado" : "activo" }
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 23, "estado" : "activo" }
{ "_id" : ObjectId("60992bab5bada2fb2206f0c5"), "nombre" : "Pedro", "apellido" :
"Perez", "promedio" : 23, "cedula" : 123, "estado" : "activo" }
{ "_id" : 1, "nombre" : "Juan", "apellido" : "Perez", "promedio" : 23, "cedula" :
123456, "estado" : "activo" }
> db.estudiantes.find().pretty()
{
"_id" : ObjectId("60992b3d5bada2fb2206f0c1"),
"nombre" : "Egan",
"apellido" : "Perez",
"promedio" : 23,
"estatura" : 170,
"estado" : "activo"
}
{
"_id" : ObjectId("60992b515bada2fb2206f0c2"),
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 23,
"estado" : "activo"
}
{
"_id" : ObjectId("60992b5e5bada2fb2206f0c3"),
"nombre" : "Maria",
"apellido" : "Perez",
"promedio" : 23,
"estado" : "activo"
}
{
"_id" : ObjectId("60992b735bada2fb2206f0c4"),
"nombre" : "Juan",
"apellido" : "Cuadrado",
"promedio" : 23,
"estado" : "activo"
}
{
"_id" : ObjectId("60992bab5bada2fb2206f0c5"),
"nombre" : "Pedro",
"apellido" : "Perez",
"promedio" : 23,
"cedula" : 123,
"estado" : "activo"
}
{
"_id" : 1,
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 23,
"cedula" : 123456,
"estado" : "activo"
}
> db.estudiantes.deleteMany({apellido:"Perez"})
{ "acknowledged" : true, "deletedCount" : 5 }
> db.estudiantes.find()
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 23, "estado" : "activo" }
> db.estudiantes.insertOne({nombre:"Maria",apellido:"Perez",promedio:46})
{
"acknowledged" : true,
"insertedId" : ObjectId("609931935bada2fb2206f0c6")
}
> db.estudiantes.insertOne({nombre:"Juan",apellido:"Perez",promedio:45})
{
"acknowledged" : true,
"insertedId" : ObjectId("6099319f5bada2fb2206f0c7")
}
> db.estudiantes.deleteMany({apellido:"perez"})
{ "acknowledged" : true, "deletedCount" : 0 }
>
db.estudiantes.insertMany([{nombre:"Juanita",apellido:"Peredb.estudiantes.insertMan
y([{nombre:"Juanita",apellido:"Peredb.estudiantes.insertMany([{nombre:"Juanita",ape
llido:"Peredb.estudiantes.insertMany([{nombre:"Juanita",apellido:"Peredb.estudiante
s.insertMany([{nombre:"Juanita",apellido:"Peredb.estudiantes.insertMany([{nombre:"J
uanita",apellido:"Peredb.estudiantes.insertMany([{nombre:"Juanita",apellido:"Peredb
.estudiantes.insertMany([{nombre:"Juanita",apellido:"Peredb.estudiantes.insertMany(
[{nombre:"Juanita",apellido:"Peredb.estudiantes.insertMany([{nombre:"Juanita",apell
ido:"Peredb.estudiantes.insertMany([{nombre:"Juanita",apellido:"Peredb.estudiantes.
insertMany([{nombre:"Juanita",apellido:"Peredb.estudiantes.insertMany([{nombre:"Jua
nita",apellido:"Peredb.estudiantes.insertMany([{nombre:"Juanita",apellido:"Peredb.e
studiantes.insertMany([{nombre:"Juanita",apellido:"Peredb.estudiantes.insertMany([{
nombre:"Juanita",apellido:"Peredb.estudiantes.insertMany([{nombre:"Juanita",apellid
o:"Peredb.estudiantes.insertMany([{nombre:"Juanita",apellido:"Peredb.estudiantes.in
sertMany([{nombre:"Juanita",apellido:"Peredb.estudiantes.insertMany([{nombre:"Juani
ta",apellido:"Peredb.estudiantes.insertMany([{nombre:"Juanita",apellido:"Peredb.est
udiantes.insertMany([{nombre:"Juanita",apellido:"Perodb.estudiantes.insertMany([{no
mbre:"Juanita",apellido:"Peo4db.estudiantes.insertMany([{nombre:"Juanita",apellido:
"Po44db.estudiantes.insertMany([{nombre:"Juanita",apellido:"o44,db.estudiantes.inse
rtMany([{nombre:"Juanita",apellido:o44,cdb.estudiantes.insertMany([{nombre:"Juanita
",apellidoo44,cedb.estudiantes.insertMany([{nombre:"Juanita",apellido44,ceddb.estud
iantes.insertMany([{ omb
re:"Juanita",apello44,cedula
> db.estudiantes.find()
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 23, "estado" : "activo" }
{ "_id" : ObjectId("609931935bada2fb2206f0c6"), "nombre" : "Maria", "apellido" :
"Perez", "promedio" : 46 }
{ "_id" : ObjectId("6099319f5bada2fb2206f0c7"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 45 }
>
db.estudiantes.insertMany([{nombre:"Juanita",apellido:"Perez",promedio:44,cedula:12
3456},{nombre:"Juan",apellido:"Perez",promedio:44,cedula:123456789}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("609932ac5bada2fb2206f0c8"),
ObjectId("609932ac5bada2fb2206f0c9")
]
}
> db.estudiantes.find()
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 23, "estado" : "activo" }
{ "_id" : ObjectId("609931935bada2fb2206f0c6"), "nombre" : "Maria", "apellido" :
"Perez", "promedio" : 46 }
{ "_id" : ObjectId("6099319f5bada2fb2206f0c7"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 45 }
{ "_id" : ObjectId("609932ac5bada2fb2206f0c8"), "nombre" : "Juanita", "apellido" :
"Perez", "promedio" : 44, "cedula" : 123456 }
{ "_id" : ObjectId("609932ac5bada2fb2206f0c9"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 44, "cedula" : 123456789 }
> db.estudiantes.find({apellido:"Perez"})
{ "_id" : ObjectId("609931935bada2fb2206f0c6"), "nombre" : "Maria", "apellido" :
"Perez", "promedio" : 46 }
{ "_id" : ObjectId("6099319f5bada2fb2206f0c7"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 45 }
{ "_id" : ObjectId("609932ac5bada2fb2206f0c8"), "nombre" : "Juanita", "apellido" :
"Perez", "promedio" : 44, "cedula" : 123456 }
{ "_id" : ObjectId("609932ac5bada2fb2206f0c9"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 44, "cedula" : 123456789 }
> db.estudiantes.find({apellido:"Perez"}).pretty()
{
"_id" : ObjectId("609931935bada2fb2206f0c6"),
"nombre" : "Maria",
"apellido" : "Perez",
"promedio" : 46
}
{
"_id" : ObjectId("6099319f5bada2fb2206f0c7"),
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 45
}
{
"_id" : ObjectId("609932ac5bada2fb2206f0c8"),
"nombre" : "Juanita",
"apellido" : "Perez",
"promedio" : 44,
"cedula" : 123456
}
{
"_id" : ObjectId("609932ac5bada2fb2206f0c9"),
"nombre" : "Juan",
"apellido" : "Perez",
"promedio" : 44,
"cedula" : 123456789
}
> db.estudiantes.find({promedio:{$gt:1}})
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 23, "estado" : "activo" }
{ "_id" : ObjectId("609931935bada2fb2206f0c6"), "nombre" : "Maria", "apellido" :
"Perez", "promedio" : 46 }
{ "_id" : ObjectId("6099319f5bada2fb2206f0c7"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 45 }
{ "_id" : ObjectId("609932ac5bada2fb2206f0c8"), "nombre" : "Juanita", "apellido" :
"Perez", "promedio" : 44, "cedula" : 123456 }
{ "_id" : ObjectId("609932ac5bada2fb2206f0c9"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 44, "cedula" : 123456789 }
> db.estudiantes.find({promedio:{$gt:48}})
> db.estudiantes.find({promedio:{$gt:45}})
{ "_id" : ObjectId("609931935bada2fb2206f0c6"), "nombre" : "Maria", "apellido" :
"Perez", "promedio" : 46 }
> db.estudiantes.findOne({promedio:{$gt:1}})
{
"_id" : ObjectId("60992b735bada2fb2206f0c4"),
"nombre" : "Juan",
"apellido" : "Cuadrado",
"promedio" : 23,
"estado" : "activo"
}
> db.estudiantes.update({"_id":ObjectId("60993135bada2fb2206f0c6")},{promedio:49})
Error: invalid object id: length :
@(shell):1:30
> db.estudiantes.update({"_id":ObjectId("609931935bada2fb2206f0c6")},{promedio:49})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.estudiantes.replaceOne({_id:ObjectId("609932ac5bada2fb2206f0c8")},
{nombre:"Juanita",apellido:"perez",promedio:4.4,cedula:"123456"})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.estudiantes.find()
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 23, "estado" : "activo" }
{ "_id" : ObjectId("609931935bada2fb2206f0c6"), "promedio" : 49 }
{ "_id" : ObjectId("6099319f5bada2fb2206f0c7"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 45 }
{ "_id" : ObjectId("609932ac5bada2fb2206f0c8"), "nombre" : "Juanita", "apellido" :
"perez", "promedio" : 4.4, "cedula" : "123456" }
{ "_id" : ObjectId("609932ac5bada2fb2206f0c9"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 44, "cedula" : 123456789 }
> db.estudiantes.replaceOne({_id:ObjectId("609932ac5bada2fb2206f0c8")},
{nombre:"Juanita",apellido:"perez",promedio:4.4,cedula:"123456"})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.estudiantes.replaceOne({_id:ObjectId("609932ac5bada2fb2206f0c8")},
{estadoCivil:"Casado"})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.estudiantes.find()
{ "_id" : ObjectId("60992b735bada2fb2206f0c4"), "nombre" : "Juan", "apellido" :
"Cuadrado", "promedio" : 23, "estado" : "activo" }
{ "_id" : ObjectId("609931935bada2fb2206f0c6"), "promedio" : 49 }
{ "_id" : ObjectId("6099319f5bada2fb2206f0c7"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 45 }
{ "_id" : ObjectId("609932ac5bada2fb2206f0c8"), "estadoCivil" : "Casado" }
{ "_id" : ObjectId("609932ac5bada2fb2206f0c9"), "nombre" : "Juan", "apellido" :
"Perez", "promedio" : 44, "cedula" : 123456789 }

> db.estudiantes.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 0 }
> db.estudiantes.insertOne({nombre:"pedro",promdeio:42})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a4ff6279e7cc844ce91320")
}
> db.estudiantes.insertOne({nombre:"Maria",promdeio:42})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a4ff6f79e7cc844ce91321")
}
> db.estudiantes.insertOne({nombre:"Juan",promdeio:43})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a4ff7e79e7cc844ce91322")
}
> db.estudiantes.insertOne({nombre:"Maria",promdeio:43})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a4ff9a79e7cc844ce91323")
}
> db.estudiantes.aggregate([{$match:{promedio:43}}])
> db.estudiantes.aggregate([{$match:{}},$group:{_id:"$promedio"},total{$sum:1}}}])
uncaught exception: SyntaxError: missing ] after element list :
@(shell):1:44
> db.estudiantes.aggregate([{$match:{}},$group:{_id:
{prom:"$promedio"},total{$sum:1}}}])
uncaught exception: SyntaxError: missing ] after element list :
@(shell):1:44
> db.estudiantes.aggregate([{$match:{}},$group:{_id:{prom:"$promedio"},total:
{$sum:1}}}])
uncaught exception: SyntaxError: missing ] after element list :
@(shell):1:44
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:{prom:"$promedio"},total:
{$sum:1}}}])
{ "_id" : { "prom" : null }, "total" : 4 }
> db.estudiantes.find()
{ "_id" : ObjectId("60a4ff6279e7cc844ce91320"), "nombre" : "pedro", "promdeio" : 42
}
{ "_id" : ObjectId("60a4ff6f79e7cc844ce91321"), "nombre" : "Maria", "promdeio" : 42
}
{ "_id" : ObjectId("60a4ff7e79e7cc844ce91322"), "nombre" : "Juan", "promdeio" :
43 }
{ "_id" : ObjectId("60a4ff9a79e7cc844ce91323"), "nombre" : "Maria", "promdeio" : 43
}
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:{prom:"$promdeio"},total:
{$sum:1}}}])
{ "_id" : { "prom" : 43 }, "total" : 2 }
{ "_id" : { "prom" : 42 }, "total" : 2 }
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:{prom:"$promdeio"},total:
{$sum:2}}}])
{ "_id" : { "prom" : 42 }, "total" : 4 }
{ "_id" : { "prom" : 43 }, "total" : 4 }
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:{prom:"$promdeio"},total:
{$sum:3}}}])
{ "_id" : { "prom" : 42 }, "total" : 6 }
{ "_id" : { "prom" : 43 }, "total" : 6 }
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:
{prom:"$promdeio",nombre:"$nombre"},total:{$sum:3}}}])
{ "_id" : { "prom" : 42, "nombre" : "pedro" }, "total" : 3 }
{ "_id" : { "prom" : 42, "nombre" : "Maria" }, "total" : 3 }
{ "_id" : { "prom" : 43, "nombre" : "Maria" }, "total" : 3 }
{ "_id" : { "prom" : 43, "nombre" : "Juan" }, "total" : 3 }
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:
{prom:"$promdeio",nombre:"$nombre"},total:{$sum:1}}}])
{ "_id" : { "prom" : 42, "nombre" : "pedro" }, "total" : 1 }
{ "_id" : { "prom" : 42, "nombre" : "Maria" }, "total" : 1 }
{ "_id" : { "prom" : 43, "nombre" : "Maria" }, "total" : 1 }
{ "_id" : { "prom" : 43, "nombre" : "Juan" }, "total" : 1 }
> db.estudiantes.insertOne({nombre:"Maria",promdeio:43})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a5024279e7cc844ce91324")
}
> db.estudiantes.find()
{ "_id" : ObjectId("60a4ff6279e7cc844ce91320"), "nombre" : "pedro", "promdeio" : 42
}
{ "_id" : ObjectId("60a4ff6f79e7cc844ce91321"), "nombre" : "Maria", "promdeio" : 42
}
{ "_id" : ObjectId("60a4ff7e79e7cc844ce91322"), "nombre" : "Juan", "promdeio" :
43 }
{ "_id" : ObjectId("60a4ff9a79e7cc844ce91323"), "nombre" : "Maria", "promdeio" : 43
}
{ "_id" : ObjectId("60a5024279e7cc844ce91324"), "nombre" : "Maria", "promdeio" : 43
}
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:
{prom:"$promdeio",nombre:"$nombre"},total:{$sum:1}}}])
{ "_id" : { "prom" : 42, "nombre" : "pedro" }, "total" : 1 }
{ "_id" : { "prom" : 42, "nombre" : "Maria" }, "total" : 1 }
{ "_id" : { "prom" : 43, "nombre" : "Juan" }, "total" : 1 }
{
> db.estudiantes.aggregate([{$match:{promdeio:43}},{$group:{_id:
{prom:"$promdeio",nombre:"$nombre"},total:{$sum:1}} ])
> db.estudiantes.aggregate([{$match:{promdeio:43}},{$group:{_id:
{prom:"$promdeio",nombre:"$nombre"},total:{$sum:1}}}
> db.estudiantes.aggregate([{$match:{promdeio:43}},{$group:{_id:
{prom:"$promdeio",nombre:"$nombre"},total:{$sum:1}}}]
> db.estudiantes.aggregate([{$match:{promdeio:43}},{$group:{_id:
{prom:"$promdeio",nombre:"$nombre"},total:{$sum:1}}}])
> db.estudiantes.aggregate([{$match:{promdeio:43}},{$group:{_id:
{nombre:"$nombre"},prom:{$avg:"$promdeio"},total:{$sum:1}}])
...
...
>
>
> db.estudiantes.aggregate([{$match:{promdeio:43}},{$group:{_id:
{prom:"$promdeio",nombre:"$nombre"},total:{$sum:1}}}])
{ "_id" : { "prom" : 43, "nombre" : "Juan" }, "total" : 1 }
{ "_id" : { "prom" : 43, "nombre" : "Maria" }, "total" : 2 }
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:
{prom:"$promdeio",nombre:"$nombre"},total:{$sum:1}}}])
{ "_id" : { "prom" : 42, "nombre" : "pedro" }, "total" : 1 }
{ "_id" : { "prom" : 42, "nombre" : "Maria" }, "total" : 1 }
{ "_id" : { "prom" : 43, "nombre" : "Maria" }, "total" : 2 }
{ "_id" : { "prom" : 43, "nombre" : "Juan" }, "total" : 1 }
> db.estudiantes.aggregate([{$match:{promdeio:43}},{$group:{_id:
{prom:"$promdeio",nombre:"$nombre"},total:{$sum:1}}}])
{ "_id" : { "prom" : 43, "nombre" : "Maria" }, "total" : 2 }
{ "_id" : { "prom" : 43, "nombre" : "Juan" }, "total" : 1 }
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:{nombre:"$nombre"},prom:
{$avg:"$promdeio"},total:{$sum:1}}])
...
...
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:{nombre:"$nombre"},prom:
{$avg:"$promdeio"},total:{$sum:1}}}])
{ "_id" : { "nombre" : "Juan" }, "prom" : 43, "total" : 1 }
{ "_id" : { "nombre" : "Maria" }, "prom" : 42.666666666666664, "total" : 3 }
{ db.estudiantes.aggregate([{$match:{}},{$group:{_id:{nombre:"$nombre"},prom:
{$avg:"$promdeio"},total:{$sum:1}}},{$sort:{total:-1,nombre:1}}])
{ "_id" : { "nombre" : "Maria" }, "prom" : 42.666666666666664, "total" : 3 }:
{$avg:"$promdeio"},total:{$sum:1}}},{$sort:{total:-1,nombre:1}}])
{ "_id" : { "nombre" : "pedro" }, "prom" : 42, "total" : 1 }
{ "_id" : { "nombre" : "Juan" }, "prom" : 43, "total" : 1 }
> db.estudiantes.aggregate([{$match:{}},{$group:{_id:{nombre:"$nombre"},prom:
{$avg:"$promdeio"},total:{$sum:1}}},{$sort:{total:-1,nombre:1}}])
{ "_id" : { "nombre" : "Maria" }, "prom" : 42.666666666666664, "total" : 3 }
{ "_id" : { "nombre" : "Juan" }, "prom" : 43, "total" : 1 }
{ "_id" : { "nombre" : "pedro" }, "prom" : 42, "total" : 1 }
> db.estudiantes.drop()
true
> db.createCollection('estudiantes', {
...
... validator: {
...
... $jsonSchema: {
...
... bsonType: 'object',
...
... required: ['nombre', 'direccion', 'ciudad', 'carreras'],
...
... properties: {
...
... nombre: {
...
... bsonType: 'string',
...
... description: 'must be a string and is required'
...
... },
...
... direccion: {
...
... bsonType: 'string',
...
... description: 'must be a string and is required'
...
... },
...
... ciudad: {
...
... bsonType: 'objectId',
...
... description: 'must be an objectid and is required'
...
... },
...
... carreras: {
...
... bsonType: 'array',
...
... description: 'must be an array and is required',
...
... items: {
...
... bsonType: 'object',
...
... required: ['promedio','carrera' ],
...
... properties: {
...
... promedio: {
...
... bsonType: 'string',
...
... description: 'must be a string and is required'
...
... },
...
... carrera: {
...
... bsonType: 'objectId',
...
... description: 'must be an objectid and is required'
...
... }
...
... }
...
... }
...
... }
...
... }
...
... }
...
... }
...
... });
{ "ok" : 1 }
> db.estudiantes.insertOne("Juan","calle 45A # 13","Bogota",carreras:
[{promedio:43,carrera:"ingenieria"}])
uncaught exception: SyntaxError: missing ) after argument list :
@(shell):1:66
> db.estudiantes.insertOne("Juan","calle 45A # 13","Bogota",{carreras:
[{promedio:43,carrera:"ingenieria"}]})
uncaught exception: Error: argument passed to addIdIfNeeded is not an object :
DBCollection.prototype.addIdIfNeeded@src/mongo/shell/crud_api.js:31:15
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:250:16
@(shell):1:1
> db.estudiantes.insertOne("Juan","calle 45A # 13","Bogota",
[{promedio:43,carrera:"ingenieria"}])
uncaught exception: Error: argument passed to addIdIfNeeded is not an object :
DBCollection.prototype.addIdIfNeeded@src/mongo/shell/crud_api.js:31:15
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:250:16
@(shell):1:1
> db.estudiantes.insertOne("Juan","calle 45A # 13","Bogota",
{promedio:43,carrera:"ingenieria"})
uncaught exception: Error: argument passed to addIdIfNeeded is not an object :
DBCollection.prototype.addIdIfNeeded@src/mongo/shell/crud_api.js:31:15
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:250:16
@(shell):1:1
> db.estudiantes.insertOne("Juan","calle 45A # 13","Bogota",carreras:
[{promedio:43,carrera:"ingenieria"}])
uncaught exception: SyntaxError: missing ) after argument list :
@(shell):1:66
> db.estudiantes.insertOne("Juan","calle 45A # 13","Bogota",carreras:
[{43,"ingenieria"}])
uncaught exception: SyntaxError: missing ) after argument list :
@(shell):1:66
> db.estudiantes.insertOne("Juan","calle 45A # 13","Bogota",[{43,"ingenieria"}])
uncaught exception: SyntaxError: missing : after property id :
@(shell):1:62
> db.estudiantes.insertOne("Juan","calle 45A # 13","Bogota",{carreras:
[{43,"ingenieria"}]})
uncaught exception: SyntaxError: missing : after property id :
@(shell):1:72
> db.estudiantes.insertOne({nombre:"Juan",direccion:"calle 45A #
13",ciudad:"Bogota",carreras:{43,"ingenieria"}})
uncaught exception: SyntaxError: missing : after property id :
@(shell):1:95
> db.estudiantes.insertOne({nombre:"Juan",direccion:"calle 45A #
13",ciudad:"Bogota",carreras:{promedio:43,carrera:"ingenieria"}})
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("60a50ef579e7cc844ce91325"),
"nombre" : "Juan",
"direccion" : "calle 45A # 13",
"ciudad" : "Bogota",
"carreras" : {
"promedio" : 43,
"carrera" : "ingenieria"
}
}
}) :
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("60a50ef579e7cc844ce91325"),
"nombre" : "Juan",
"direccion" : "calle 45A # 13",
"ciudad" : "Bogota",
"carreras" : {
"promedio" : 43,
"carrera" : "ingenieria"
}
}
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:264:9
@(shell):1:1
> db.estudiantes.find()
> db.estudiantes.insert({nombre:"Juan",direccion:"calle 45A #
13",ciudad:"Bogota",carreras:{promedio:43,carrera:"ingenieria"}})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})
> db.estudiantes.insertOne({nombre:"Juan",direccion:"calle 45A #
13",ciudad:"Bogota",carreras:{promedio:"4.3",carrera:"ingenieria"}})
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("60a510c679e7cc844ce91327"),
"nombre" : "Juan",
"direccion" : "calle 45A # 13",
"ciudad" : "Bogota",
"carreras" : {
"promedio" : "4.3",
"carrera" : "ingenieria"
}
}
}) :
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("60a510c679e7cc844ce91327"),
"nombre" : "Juan",
"direccion" : "calle 45A # 13",
"ciudad" : "Bogota",
"carreras" : {
"promedio" : "4.3",
"carrera" : "ingenieria"
}
}
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:264:9
@(shell):1:1
> db.productos.find()
> db.producto.find()
> db.carrera.insertOne(nombre:"ingenieria")
uncaught exception: SyntaxError: missing ) after argument list :
@(shell):1:27
> db.carrera.insertOne({nombre:"ingenieria"})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a513ce79e7cc844ce91328")
}
> db.direcciones.insertOne({direccion:"calle 163 a#36"})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a513fb79e7cc844ce91329")
}
> db.estudiantes.insertOne({nombre:"Juan",direccion:"calle 45A #
13",ciudad:"Bogota",carreras:{promedio:"4.3",carrera:""}})
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("60a5141579e7cc844ce9132a"),
"nombre" : "Juan",
"direccion" : "calle 45A # 13",
"ciudad" : "Bogota",
"carreras" : {
"promedio" : "4.3",
"carrera" : ""
}
}
}) :
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("60a5141579e7cc844ce9132a"),
"nombre" : "Juan",
"direccion" : "calle 45A # 13",
"ciudad" : "Bogota",
"carreras" : {
"promedio" : "4.3",
"carrera" : ""
}
}
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:264:9
@(shell):1:1
> db.estudiantes.insertOne({nombre:"Juan",direccion:"calle 45A #
13",ciudad:ObjectId("60a513fb79e7cc844ce91329"),carreras:
{promedio:"4.3",carrera:ObjectId("60a513ce79e7cc844ce91328")}})
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("60a5145c79e7cc844ce9132b"),
"nombre" : "Juan",
"direccion" : "calle 45A # 13",
"ciudad" : ObjectId("60a513fb79e7cc844ce91329"),
"carreras" : {
"promedio" : "4.3",
"carrera" : ObjectId("60a513ce79e7cc844ce91328")
}
}
}) :
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("60a5145c79e7cc844ce9132b"),
"nombre" : "Juan",
"direccion" : "calle 45A # 13",
"ciudad" : ObjectId("60a513fb79e7cc844ce91329"),
"carreras" : {
"promedio" : "4.3",
"carrera" : ObjectId("60a513ce79e7cc844ce91328")
}
}
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:264:9
@(shell):1:1
> db.estudiantes.insertOne({nombre:"Juan",direccion:"calle 45A #
13",ciudad:ObjectId("60a513fb79e7cc844ce91329"),carreras:
[{promedio:"4.3",carrera:ObjectId("60a513ce79e7cc844ce91328")}]})
{
"acknowledged" : true,
"insertedId" : ObjectId("60a5146b79e7cc844ce9132c")
}

You might also like