Repository containing the queries developed in 👉 MongoDB 👈
git clone https://aur.archlinux.org/mongodb-bin.git
cd mongodb-bin
makepkg -si
systemctl start mongodb
systemctl enable mongodb
sudo pecl install mongodb
brew services start mongodb-community
brew tap mongodb/brew
brew info mongodb-community
mongosh
help
use <database>
use db
db.createCollection('states')
db.createCollection('persons')
show dbs
show collections
db.states.drop()
db.persons.drop()
db.persons.insert({ name: "John Doe", country: "United States of America" })
db.persons.insert({ name: "Bia", country: "Brazil" })
db.persons.insert({ name: "Bruna", country: "Brazil", age: 24 })
db.persons.insert({ name: "Hannah", country: "UK" })
db.persons.insert({ name: "Lucia", country: "Brazil" })
db.persons.insert({ name: "Maria", country: "Brazil" })
db.persons.insert({ name: "Roberta", country: "Brazil" })
db.states.insert({
name: "Rio Grande do Sul",
cities: [
{
_id: ObjectId(),
name: "Novo Hamburgo",
population: 1000
},
{
_id: ObjectId(),
name: "Porto Alegre",
population: 5000
},
{
_id: ObjectId(),
name: "São Leopoldo",
population: 2000
}
]
})
db.states.insert({
name: "Santa Catarina",
cities: [
{
_id: ObjectId(),
name: "Florianópolis",
population: 1000
},
{
_id: ObjectId(),
name: "Joinville",
population: 4000
},
{
_id: ObjectId(),
name: "Blumenau",
population: 5000
},
{
_id: ObjectId(),
name: "Balneário Camboriú",
population: 7000
}
]
})
db.persons.find()
db.persons.find().pretty()
db.states.findOne({ name: "Rio Grande do Sul" })
db.states.find({ name: "Rio Grande do Sul" }).pretty()
db.persons.find({ $or: [{ name: "Bia" }, { name: "Hannah" }] }).pretty()
db.persons.find({ $and: [{ country: "Brazil" }, { name: "Bruna" }] }).pretty()
db.persons.find({ age: { $exists: true } })
db.persons.find().skip(0).limit(1)
db.persons.countDocuments()
db.persons.estimatedDocumentCount()
db.persons.find({ country: "Brazil" }, { name: 1, country: 1, _id: 0 }).pretty()
db.states.find({ name: "Rio Grande do Sul" }, { "cities.name": 1, "_id": 0 }).pretty()
db.states.aggregate([
{ $project: { name: 1, "cities.name": 1, _id: 0 } }
])
db.states.aggregate([
{ $project: { population: { $sum: "$cities.population" }, name: 1, _id: 0 } },
{ $group: { _id: null, total: { $sum: "$population" } } },
{ $project : { _id: 0, total: 1 } }
])
db.states.aggregate([
{ $match: { "cities.name": "Novo Hamburgo" } },
{ $unwind: "$cities" },
{ $match: { "cities.name": "Novo Hamburgo" } },
{ $project: { _id: "$cities._id" } }
]).pretty()
db.persons.update( { name: "Hannah" }, { $set: { age: 27 } } )
db.states.update( { name: "Rio Grande do Sul" }, { $push: { cities: { _id: ObjectId(), name: "Campo bom", population: 3000 } } } )
db.persons.remove( { name: "John Doe" }, 1)
db.persons.remove( { age: { $exists: false } }, 1 )
db.persons.remove( { age: { $lt: 30 } } )
db.createCollection('companies')
db.companies.insert( { name: "Santander", city_id: ObjectId("5f87961d8ddbbac1f99f9783") } )
db.companies.insert( { name: "Bradesco", state_id: ObjectId("5f87961d8ddbbac1f99f9786") } )
db.companies.aggregate([
{ $match: { name: "Bradesco" } },
{ $lookup: { from: "states", localField: "state_id", foreignField: "_id", as: "state" } }
]).pretty()
db.companies.aggregate([
{ $match: { name: "Santander" } },
{ $lookup: { from: "states", localField: "city_id", foreignField: "cities._id", as: "state" } },
{ $unwind: "$state" },
{ $unwind: "$state.cities" },
{ $addFields: { sameCity: { $cmp: [ "$state.cities._id", "$city_id" ] } } },
{ $match: { sameCity: 0 } }
]).pretty()