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 ๐
A quick guide for handling MongoDB database from our local machine through command prompt ๐ป
Let's get started
- Show All Databases ๐
show dbs
- Show Current Database ๐จโ๐ป
db
- Create Or Switch Database โญ
use <database_name>
- Drop Database ๐ฅ
db.dropDatabase()
- 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')
- Show Collections ๐
show collections
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()
})
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()
}
])
to be continued...
Have a great day !
Author: Jayesh ๐งก
Top comments (0)