MongoDB

MongoDB is a document database and can be installed locally or hosted in the cloud.


        {mongosh "mongodb+srv://cluster0.ex4ht.mongodb.net/myFirstDatabase"
         --apiVersion 1}
         
         --username YOUR_USER_NAME

MongoDB mongosh Create Database

If you have used the connection string provided from the MongoDB Atlas dashboard, you should be connected to the myFirstDatabase database.

{
        To see all available databases, in your terminal type show dbs.
        Notice that myFirstDatabase is not listed. This is because the database is empty. 
        An empty database is essentially non-existant.
    }

Create Collection using mongosh

There are 2 ways to create a collection.

{
        db.createCollection("posts")
    },{
        db.posts.insertOne(object)
    }

Find Data

To find the datain database

{
        db.posts.find()
    },{
        db.posts.findOne()
    }

Update Document{

To update an existing document we can use the updateOne() or updateMany() methods. The first parameter is a query object to define which document or documents should be updated. The second parameter is an object defining the updated data.

{
        db.posts.find( { title: "Post Title 1" } ) 
    },{
        db.posts.updateOne( { title: "Post Title 1" }, { $set: { likes: 2 } } ) 
    }

MongoDB mongosh Delete

We can delete documents by using the methods deleteOne() or deleteMany(). These methods accept a query object. The matching documents will be deleted.

{
        db.posts.deleteOne({ title: "Post Title 5" })
    },{
        db.posts.deleteMany({ category: "Technology" })
    }

MongoDB Update Operators

The following operators can be used to update fields: $currentDate: Sets the field value to the current date $inc: Increments the field value $rename: Renames the field $set: Sets the value of a field $unset: Removes the field from the document

{
        db.posts.updateOne({ title: "Post Title 5" })
    }

MongoDB Charts

MongoDB Charts Setup From the MongoDB Atlas dashboard, go to the Charts tab. If you've never used Charts before, click the Activate Now button. This will take about 1 minute to complete. You'll see a new dashboard. Click the dashboard name to open it.

{
        Creating a Chart
Create a new chart by clicking the Add Chart button.

Visually creating a chart is intuitive. Select the data sources that you want to use.
    }