DEV Community

Jasterix
Jasterix

Posted on

5 Mongo shell methods to get you started

Along my journey to become a JavaScript ninja, I recently started learning MongoDb (the M in the "MERN" stack). Coming from relational databases like MySQL and PostgreSQL, it's been a challenge to get started.

Dynamic schemas are great until you sit down to design your schema and realize that the way you think about schemas doesn't work in a NoSQL world. My initial approach to MongoDB was ineffective. Instead of jumping right into a project, it makes more sense to get the basics down, which is why I'm writing this blog.

I downloaded the community version for MongoDB, which honestly requires its own blog for getting set up. But to get more comfortable with MongoDB, I recommend getting the following programs:

Getting started and navigating databases:

I set up a test cluster sample information in Atlas.

  • show dbs to view the databases in my cluster:
admin               0.000GB
local               4.957GB
sample_airbnb       0.053GB
sample_supplies     0.001GB
Enter fullscreen mode Exit fullscreen mode
  • use sample_supplies to select and switch to the sample_supplies database

  • show collections to view the collections i.e. data records in the sample_supplies database:

sales
Enter fullscreen mode Exit fullscreen mode
  • db.collection.find() to view records in a collection
  • db.sales.find({ }) would return all of the records in the sales collection
    • attaching .pretty() to the end will format the results as shown
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.sales.find({ }).pretty()
{
    "_id" : ObjectId("5bd761dcae323e45a93ccfe8"),
    "saleDate" : ISODate("2015-03-23T21:06:49.506Z"),
    "items" : [
        {
            "name" : "printer paper",
            "tags" : [
                "office",
                "stationary"
            ],
            "price" : NumberDecimal("40.01"),
            "quantity" : 2
        },
        {
            "name" : "notepad",
            "tags" : [
                "office",
                "writing",
                "school"
            ],
        },
    "couponUsed" : false,
    "purchaseMethod" : "In store"
},
{
...
Enter fullscreen mode Exit fullscreen mode
  • db.sales.find({ "couponUsed" : false }) would return all of the records that match the query

  • db.help() returns a list of common MongoDB methods

While this isn't a list of methods to perform full CRUD actions, these 5 methods were enough to keep me busy using the mongo shell for a couple of hours. I use these methods to move between databases, get familiar with the mongo shell syntax and to get a sense of how collections are structured.

While NoSQL is uncomfortable for someone who's only used SQL databases, this is an easy way to get started with MongoDB. I would highly recommend downloading the 3 tools at the start of this post and jumping right in.

But for anyone interesting in learning more about performing CRUD actions in the Mongo shell, checkout this 30 minute Traversy Media tutorial.

Top comments (0)