Mongodb Shell
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()