Skip to main content

MongoDB Basics

MongoDB database basics

{
_id: Object("765uhgsld9900dc"),
name: "Sam",
age: 99,
}
{
_id: Object("560wzlsse2910pb"),
name: "John",
age: 32,
}

  • mongo: mongo is the command-line shell that connect to a specific instance of mongod
  • mongod: This is the primary daemon process for the MongoDB system
  • mongos: sharded cluster
  • mongosh: Mongodb shell is a fully functional javascript and node.js REPL environment

mongosh commands

connect through docker bash

docker exec -it [container-name] bash

launch mongo shell mongosh

mongosh -u [user-name] -p [password]

Passing environmental variable to docker

docker run -d --network some-network --name some-mongo \
-e MONGO_INITDB_ROOT_USERNAME=mongoadmin \
-e MONGO_INITDB_ROOT_PASSWORD=secret \
mongo
docker run -it --rm --network some-network mongo \
mongosh --host some-mongo \
-u mongoadmin \
-p secret \
--authenticationDatabase admin \
some-db

Basic commands

list all the databases

show dbs

create database

This command will create the database if it doesn't exist and switch to that database

use [database-name]

drop database

Switch to the database and execute the command below

db.dropDatabase()

create collection and insert document

collection name is visitor and has document with name and age as fields

db.visitor.insertOne({"name":"sam", "age":23})

switching to secopsdb and creating visitor collection with data in it.

test> use secopsdb
switched to db secopsdb
schooldb> db.visitor.insertOne({"name":"sam", "age":23})

create collection with validator

This command creates a collection secopsuser with required fields name and age in the document

secopsdb> db.createCollection("secopsuser", {validator:{$jsonSchema:{bsonType:"object", required:["name","age"], properties:{name:{bsonType:"string", description:"Must be a String and is required"}, age:{bsonType:"int", description:"Must be an Integer and is required"},}}}})

list all the collections

show collections

get detailed information of collection

db.getCollectionInfos({name:"secopsuser"})

drop the colleciton

This command will drop the collection visitor

db.visitor.drop()

insert One user into the collection

db.secopsuser.insertOne({name:"johnny",age:45})

insert Many user into the collection

db.secopsuser.insertMany([{name:"jack",age:22},{name:"sammy",age:39}])

list all documents in the collection

db.secopsuser.find()

list only 2 documents

db.secopsuser.find().limit(2)

find document with field name has john

db.secopsuser.find({name:"john"})

update a document

updates the document with field name containing john to John Smith

db.secopsuser.updateOne({name:"john"}, {$set:{name:"John Smith"}})

delete One document

db.secopsuser.deleteOne({name:"sammy"})

delete Many documents

deletes all the documents with fields with age 25

db.secopsuser.deleteMany({age:25})

User management

Authorization and roles management

Create user

db.createUser({user:"bond",pwd:"123456", roles:[{role:"read",db:"secopsdb"}]})

Create Admin user

db.createUser({user:"superbond",pwd:"123456", roles:["root"]})

list users

show users

delete user

db.dropUser("bond")

Enable Authorization

Update the authorization option enabled in the mongod conf file

security:
authorization: enabled

Authenticate User access db

mongosh --port 27017 --authenticateDatabase "secopsdb" -u "bond" -p "123456"