Skip to content

Instantly share code, notes, and snippets.

@elyse0
Created November 24, 2021 19:19
Show Gist options
  • Save elyse0/29ce50726142cdc5df0dbbb31ea69644 to your computer and use it in GitHub Desktop.
Save elyse0/29ce50726142cdc5df0dbbb31ea69644 to your computer and use it in GitHub Desktop.
db.alumnos.find({
materias: {$all: ["ESPAÑOL", "MATEMATIACS", "ETICA"]}
}).count();
db.alumnos.find({
materias: {
$elemMatch: {
$in: ["ES", "MAT", "ETICA"]
}
}
}).count();
db.alumnos.distinct("clave_alu").sort()
db.alumnos.distinct("nombre",
{$and: [
{nombre: {$gte: "M"}},
{nombre: {$lte: "S"}}
]
})
.sort()
db.alumnos.find({materias: {$size: 5}})
db.alumnos.find({
$expr: {
$gt: [
"$edad.dias",
"$edad.anios"
]
}
}, {
_id: false,
nombre: true,
edad: true
}).count()
db.alumnos.find({
$expr: {
$gt: [
{$cond:{
if: {$gte: ["$edad.anios", 30]},
then: {$divide: ["$edad.anios", 2]},
else: {$divide: ["$edad.anios", 3]}
}
},
10
]
}
}, {
_id: false,
nombre: true,
edad: true
}).count()
db.alumnos.find({
sexo: "F"
}, {
_id: false, nombre: true
})
.count()
p = {sexo: "F"}
q = {_id: false, nombre: true}
db.alumnos.find(p, q)
let divide = {$cond:{
if: {$gte: ["$edad.anios", 30]},
then: {$divide: ["$edad.anios", 2]},
else: {$divide: ["$edad.anios", 3]}
}
}
db.alumnos.find({$expr: {$gt: [divide, 10]}})
db.alumnos.find({
$where: "this.nombre === 'ALDO'"
}, {
_id:false, nombre: true, email:true
})
db.alumnos.find({
$where: "this.nombre === 'MATEMATICAS'"
}, {
_id:false, nombre: true, email:true
})
db.alumnos.find({
$where: function(){return isObject(this.materias)}
},
{
_id: 0
}
)
// Regex
db.alumnos.find({
nombre: {$regex: ".*ale.*"}
}, {
_id: false, nombre: true, email: true
})
db.alumnos.find({
nombre: {$regex: ".*ale.*", $options: "i"}
}, {
_id: false, nombre: true, email: true
})
// Anything (.*)
db.alumnos.distinct("nombre", {
nombre: {$regex: ".*ale.*", $options: "i"}
})
// Starts with (^)
db.alumnos.distinct("nombre", {
nombre: {$regex: "^ale.*", $options: "i"}
})
// Ends with ($)
db.alumnos.distinct("nombre", {
nombre: {$regex: ".*nia$", $options: "i"}
})
// Not
db.alumnos.distinct("nombre", {
nombre: {$not: {$regex: ".*nia$", $options: "i"}}
})
db.alumnos.find({
nombre: {$not: {$regex: ".*nia$", $options: "i"}}
}, {
_id: false, nombre: true, curp: true, email: true
})
db.alumnos.find({
$and: [
{nombre: {$not: {$regex: "^ale.*", $options: "i"}}},
{ciudad: {$not: {$regex: ".*quer.*", $options: "i"}}},
{ciudad: {$exists: true}}
]
}, {
_id: false, nombre: true, curp: true, email: true
})
db.alumnos.find({
nombre: {$regex: "(juan|maria)", $options: "i"}
}, {
_id: false, nombre: true, curp: true, email: true
})
db.alumnos.find({
nombre: {$regex: "(^juan|^maria)", $options: "i"}
}, {
_id: false, nombre: true, curp: true, email: true
})
db.alumnos.find({
$and: [
{nombre: {
$not: {$regex: "^ale.*", $options: "i"}}
},
{ciudad: {$not: {$in: [
{$regex: ".*quer.*", $options: "i"},
{$regex: ".*qro.*", $options: "i"}
]}}
},
{ciudad: {$exists: true}}
]
}, {
_id: false, nombre: true, curp: true, email: true
})
//let ciudades = db.alumnos.distinct("ciudad", {ciudad: {$not: {$in: [/.*quer.*/i,/.*quer.*/]}}})
db.alumnos.find({
$and: [
{email: {$not: /[0-9]/i}},
{email: {$exists: true}}
]
}, {_id: 0, nombre: 1, curp: 1, email: 1}).sort({nombre: 1})
db.alumnos.find({
$and: [
{email: /^[^@]+@[^@]+\.[a-zA-Z]{2,}$/},
{email: {$not: /(gmail|hotmail)/}}
]
}, {_id: 0, nombre: 1, curp: 1, email: 1}).sort({nombre: 1})
db.alumnos.find({
curp: /[A-Z][A,E,I,O,U,X][A-Z]{2}[0-9]{2}[0-1][0-9][0-3][0-9][M,H][A-Z]{2}[B,C,D,F,G,H,J,K,L,M,N,Ñ,P,Q,R,S,T,V,W,X,Y,Z]{3}[0-9,A-Z][0-9]/
},{_id:0, nombre: 1, curp:1})
db.alumnos.find({
email: /[[:punct:]]/
},{_id:0, nombre: 1, curp:1, email:1})
db.alumnos.find({
email: /^[[:digit:]]/
},{_id:0, nombre: 1, curp:1, email:1})
// Tres digitos seguidos
db.alumnos.find({
email: /[[:digit:]]{3}/
},{_id:0, nombre: 1, curp:1, email:1})
// Almenos tres
db.alumnos.find({
email: /[[:digit:]]{3,}/
},{_id:0, nombre: 1, curp:1, email:1})
// Almenos 4 y maximo 6
db.alumnos.find({
email: /[[:digit:]]{4, 6}/
},{_id:0, nombre: 1, curp:1, email:1})
// Contiene algun digito
db.alumnos.find({
email: /\d/
},{_id:0, nombre: 1, curp:1, email:1})
// Contiene algun valor alfanumerico
db.alumnos.find({
email: /\D/
},{_id:0, nombre: 1, curp:1, email:1})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment