Skip to content
/ MongoDB Public

Repository containing the queries developed in MongoDB

License

Notifications You must be signed in to change notification settings

Adath/MongoDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 

Repository files navigation

Repository containing the queries developed in 👉 MongoDB 👈

License License License License

Website

Installation on Manjaro Linux

    git clone https://aur.archlinux.org/mongodb-bin.git
    cd mongodb-bin
    makepkg -si
    systemctl start mongodb
    systemctl enable mongodb

Installation on MacOS

sudo pecl install mongodb
brew services start mongodb-community
brew tap mongodb/brew
brew info mongodb-community
mongosh

Listing Commands

    help

Create Database / Collection [states, persons]

    use <database>
    use db
    db.createCollection('states')
    db.createCollection('persons')
    show dbs
    show collections

Droping Databases / Collection [states, persons]

    db.states.drop()
    db.persons.drop()

Inserting Data

    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
            }
        ]
    })

Listing Data

    db.persons.find()

    db.persons.find().pretty()

Querys

    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 } })

Skip / Limit

    db.persons.find().skip(0).limit(1)

Count

    db.persons.countDocuments()
    db.persons.estimatedDocumentCount()

Pretty

    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()

Aggregate

    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()

Update

    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 } } } )

Remove

    db.persons.remove( { name: "John Doe" }, 1)
    db.persons.remove( { age: { $exists: false } }, 1 )
    db.persons.remove( { age: { $lt: 30 } } )

Lookup

    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()

About

Repository containing the queries developed in MongoDB

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published