The information in this quick guide is educational in nature, and is intended primarily for students and software developers who start with MongoDB, specifically with the use of MongoSH, for this reason information regarding database administration, clustering, tuning, or other advanced features is not included.
show dbs
db
use myMongoDB
db.dropDatabase()
db.createCollection('myCollection')
show collections
db.myCollection.countDocuments({})
db.myCollection.countDocuments(filtro)
db.myCollection.drop()
# Relational Operator
Equal (=) { field: value }
Less Than (<) { field: { $lt:value }}
Less Than Equal (<=) { field: { $lte:value }}
Greater Than (>) { field: { $gt:value }}
Greater Than Equal (>=) { field: { $gte:value }}
Not Equal (!=) { field: { $ne:value }}
Values in an array { field: { $in: [ value, value, value ]}}
Values not in array { field: { $nin: [ value, value, value ]}}
# Logical operators
AND { $and: [ { field:value }, { field:value }, { field:value } ] }
OR { $or: [ { field:value }, { field:value }, { field:value } ] }
NOR { $nor: [ { field:value }, { field:value }, { field:value } ] }
NOT { $not: [ { field:value }, { field:value }, { field:value } ] }
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 } })
# asc
db.myCollection.find().sort({ item: 1 }).pretty()
# desc
db.myCollection.find().sort({ item: -1 }).pretty()
db.myCollection.find().count()
db.myCollection.find({ item: "card" }).count()
db.myCollection.find().limit(2).pretty()
db.myCollection.find().skip(2).pretty()
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" })
# Unless the _id field is explicitly excluded in the projection document, the _id field is returned.
# 0 = Hide field, 1 = Show field
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
This information has been extracted from the official MongoDB documentation, for more details see the MongoSH manual at https://www.mongodb.com/docs/manual/reference/method/
Code and documentation copyright 2019-2022 the authors. Code released under the MIT License.