Skip to content

MongoDB

Install 5


sudo apt install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
sudo apt update
#sudo apt install -y mongodb-mongosh=1.0.1
#sudo apt install -y mongodb-mongosh=1.2.2
#sudo apt install -y mongodb-mongosh=1.2.3
sudo apt install -y mongodb-mongosh

Connect to db


mongosh "mongodb://{IP}:27017"

Show dbs


show databases

Use db


use {database-name}

Create Collection


db.createCollection("users")
db.createCollection("roles")

Show collections from db


show collections
db.getCollectionNames()

Show collection stats


db.{collection-name}.stats()

Insert


db.users.insert({id:"1", username: "admin", email: "admin@labs.labs", password: "pass123"})
db.users.insert({id:"2", username: "user0", email: "user0@labs.labs", password: "pass456"})

Update


db.users.update({id:"1"}, {$set: {username: "super-admin"}});

Get all


db.{collection-name}.find()

Get first record from collection


db.{collection-name}.findOne()