La información de esta guía rápida es de naturaleza educativa y está destinada principalmente a estudiantes y desarrolladores de software que comienzan con MongoDB, específicamente con el uso de MongoSH, por esta razón, la información relacionada con la administración de la base de datos, clustering, tuning u otras funciones avanzadas no esta incluida.
show dbs
db
use myMongoDB
db.dropDatabase()
db.createCollection('myCollection')
show collections
db.myCollection.countDocuments({})
db.myCollection.countDocuments(filtro)
db.myCollection.drop()
# Operadores Relacionales
Igual (=) { campo: valor }
Menor que (<) { campo: { $lt:valor }}
Menor o igual que (<=) { campo: { $lte:valor }}
Mayor que (>) { campo: { $gt:valor }}
Mayor o igual que (>=) { campo: { $gte:valor }}
Diferente de (!=) { campo: { $ne:valor }}
Valores en un array { campo: { $in: [ valor, valor, valor ]}}
Valores diferentes a los del array { campo: { $nin: [ valor, valor, valor ]}}
# Operadores Lógicos
AND { $and: [ { campo:valor }, { campo:valor }, { campo:valor } ] }
OR { $or: [ { campo:valor }, { campo:valor }, { campo:valor } ] }
NOR { $nor: [ { campo:valor }, { campo:valor }, { campo:valor } ] }
NOT { $not: [ { campo:valor }, { campo:valor }, { campo:valor } ] }
db.myCollection.insertOne({
item: "card",
qty: 15
})
db.myCollection.insertMany([
{
item: "card",
qty: 15
},
{
item: "box",
qty: 20
},
{
item: "envelopes",
qty: 30
}
])
db.myCollection.find()
db.myCollection.find().pretty()
db.myCollection.find({ item: "card" })
db.myCollection.find({ _id: ObjectId("616d7ca2cb1032dfa6345840") })
db.myCollection.find({ qty: { $gt: 2 } })
db.myCollection.find({ qty: { $gte: 7 } })
db.myCollection.find({ qty: { $lt: 7 } })
db.myCollection.find({ qty: { $lte: 7 } })
# Ascendente
db.myCollection.find().sort({ item: 1 }).pretty()
# Descendente
db.myCollection.find().sort({ item: -1 }).pretty()
db.myCollection.find().count()
db.myCollection.find({ item: "card" }).count()
db.myCollection.find().limit(2)
db.myCollection.find().skip(2)
db.myCollection.find().limit(2).sort({ item: 1 }).pretty()
db.myCollection.find().forEach(function(doc) {
print("Item name: " + doc.item)
})
db.myCollection.findOne({ item: "card" })
# A menos que el campo _id esté explícitamente excluido, siempre se mostrara en los resultados.
# 0 = ocultar campo, 1 = mostrar campo
db.myCollection.find({ item: "card" }, {
_id: 0,
item: 1
})
db.myCollection.find({
address: {
$elemMatch: {
name: "Office"
}
}
}
)
db.myCollection.find({
$text: {
$search: "\"card\""
}
})
db.myCollection.updateOne({ item: "card" },
{
item: "card"
qty: 50
},
{
upsert: true
})
db.myCollection.updateOne({ item: "card" },
{
$set: {
qty: 50
}
})
db.myCollection.updateMany({ },
{
$set: {
age: 0
}
})
db.myCollection.updateOne({ item: "card" },
{
$unset: {
qtyii: ""
}
})
db.myCollection.updateMany({ },
{
$unset: {
qtyii: ""
}
})
db.myCollection.updateOne({ item: "card" },
{
$inc: {
qty: 5
}
})
db.myCollection.update({ item: "card" },
{
$rename: {
qtyii: 'qty'
}
})
db.myCollection.updateOne({ userName: 'Jhon_Doe' },
{
$set: {
address: [
{
name: "Home",
mainAddress: "20341 My home",
phone: "123456789"
},
{
name: "Office",
mainAddress: "20341 My Office",
phone: "123456789"
}
]
}
})
db.myCollection.deleteOne({ _id: ObjectId("616d7ca2cb1032dfa6345840") })
db.myCollection.deleteMany({})
Fabian A. Becerra M. https://github.com/fabecerram
Esta información ha sido recoplilada en base a la documentación oficial de MongoDB, para más detalles ver el manual de MongoSH en https://www.mongodb.com/docs/manual/reference/method/
Code and documentation copyright 2019-2022 the authors. Code released under the MIT License.