Mongodb Shell

sourav mandal
Feb 1, 2021

--

mongoshow dbsuse <dbname># to create database
use <newdatabasename>
show collectionsdb.dropDatabase()dbdb.createCollection('posts')#inserting datadb.posts.insert({
title: 'Post One',
body: 'Body of post one',
category: 'News',
tags: ['news', 'events'],
user: {
name: 'John Doe',
status: 'author'
},
date: Date()
})
db.posts.insertMany([
{
title: 'Post Two',
body: 'Body of post two',
category: 'Technology',
date: Date()
},
{
title: 'Post Three',
body: 'Body of post three',
category: 'News',
date: Date()
},
{
title: 'Post Four',
body: 'Body of post three',
category: 'Entertainment',
date: Date()
}
])
db.posts.find()db.find().pretty()db.posts.find({ category: 'News' })# sorting rows# asc
db.posts.find().sort({ title: 1 }).pretty()
# desc
db.posts.find().sort({ title: -1 }).pretty()

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

sourav mandal
sourav mandal

Written by sourav mandal

just a coder who forgets things so makes blogs so he can remember later

No responses yet

Write a response