MongoDB - Tutorial

Page content

Good tutorial video

https://www.youtube.com/watch?v=pWbMrx5rVBE

Handson

Install on Ubuntu

sudo su
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
apt update
sudo apt install -y mongodb-org
systemctl start mongodb

Playground in local Docker container

docker run -p 27017:27017 -d mongo:5.0-focal

Toolkit

curl https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2004-x86_64-100.3.0.deb -O
sudo apt install ./mongodb-database-tools-ubuntu2004-x86_64-100.3.0.deb

Mongo shell

mkdir mongosh
cd mongosh
curl https://downloads.mongodb.com/compass/mongosh-0.8.3-linux.tgz -O
tar -zxvf mongosh-0.8.3-linux.tgz
chmod +x bin/mongosh
sudo ln -s /home/atlex/mongosh/bin/mongosh /usr/local/bin/

Use MongoDB from Mongo Shell

mongosh --host localhost --port 27017
# mongosh --host remote.atlex00.com --port 27017 --username mymongouser

> show dbs
admin   32.8 kB
config  12.3 kB
local   32.8 kB

> show users
[]

> use newdb;
switched to db newdb

> show collections;

> db.createCollection("mycollection") ;
{ ok: 1 }
> show collections
mycollection


> db.mycollection.insert( {field:"first", second:"third"} );
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany or bulkWrite.
{
  acknowledged: true,
  insertedIds: { '0': ObjectId("604608c3a330a2ccdfb207dc") }
}
> db.mycollection.find();
[
  {
    _id: ObjectId("604608c3a330a2ccdfb207dc"),
    field: 'first',
    second: 'third'
  }
]

> db.dropDatabase();

> quit()

MongoDB HA

Replicaset

https://www.mongodb.com/basics/clusters

Snippet for investigation

# Check replica sets status
> rs.status()