DEV Community

Jayesh Tembhekar ⚡
Jayesh Tembhekar ⚡

Posted on • Edited on

2

A MongoDB survival pill

When you think about Web development, there are tons of technologies, tools, & blah blah things to keep in mind. The chances of getting overwhelmed becomes 🔝.

Same goes with Databases. Today, we are talking about the non relational database.

mongodb

MongoDB 😅

A quick guide for handling MongoDB database from our local machine through command prompt 💻


Let's get started

  • Show All Databases 🔎
show dbs
Enter fullscreen mode Exit fullscreen mode
  • Show Current Database 👨‍💻
db
Enter fullscreen mode Exit fullscreen mode
  • Create Or Switch Database ⏭
use <database_name>
Enter fullscreen mode Exit fullscreen mode
  • Drop Database 🔥
db.dropDatabase()
Enter fullscreen mode Exit fullscreen mode
  • Create Collection 👨‍🔧 > "collection" is similar to "table" in the SQL .

We call "tables" as "collections" in MongoDB .

Here, 'posts' is the name of our collection.

db.createCollection('posts')
Enter fullscreen mode Exit fullscreen mode
  • Show Collections 🔎
show collections
Enter fullscreen mode Exit fullscreen mode

Now, we have created a 'collection' called "posts" & then we will populate our collection with some data. To do that paste below schema into command prompt 👇

Insert Row

db.posts.insert({
  title: 'Post 1',
  body: 'Hello, this is post one',
  category: 'Sports',
  tags: ['young', 'stadium'],
  user: {
    name: 'Jayesh Tembhekar',
    status: 'author'
  },
  date: Date()
})
Enter fullscreen mode Exit fullscreen mode

To speed up the insertion, we can insert multiple rows at a time like this...

  • Insert Multiple Rows 🥇
db.posts.insertMany([
  {
    title: 'Post 2',
    body: 'Hello, this is post two',
    category: 'Technology',
    date: Date()
  },
  {
    title: 'Post 3',
    body: 'Hello, this is post three',
    category: 'Politics',
    date: Date()
  },
  {
    title: 'Post 4',
    body: 'Hello, this is post four',
    category: 'Entertainment',
    date: Date()
  }
])
Enter fullscreen mode Exit fullscreen mode

to be continued...

Have a great day !

Author: Jayesh 🧡

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more