Published on

How to install mongoDB in Ubuntu22.04

Authors
  • Name
    Twitter

Steps

sudo apt update
sudo apt install wget curl gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release
sudo curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-6.gpg
sudo echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt update
sudo apt install mongodb-org
sudo systemctl enable --now mongod
sudo systemctl status mongod
mongod --version

MongoDB insert test

testing> show dbs
admin   40.00 KiB
config  12.00 KiB
local   40.00 KiB
testing> db
DBQuery  DBRef    db

testing> db.message.insertOne({
... user: "tom",
... age: "23",
... log: "Hello, MongoDB!"
... })
{
  acknowledged: true,
  insertedId: ObjectId("64a6ed38759a9d8c994b8567")
}
testing> show collections
message
testing> db.message.insertOne({ user: "billy", age: 23, log: "Hello, MongoDB is Fun!!" })
{
  acknowledged: true,
  insertedId: ObjectId("64a6ed67759a9d8c994b8568")
}
testing> db.message
db.message

testing> db.message.find()
[
  {
    _id: ObjectId("64a6ed38759a9d8c994b8567"),
    user: 'tom',
    age: '23',
    log: 'Hello, MongoDB!'
  },
  {
    _id: ObjectId("64a6ed67759a9d8c994b8568"),
    user: 'billy',
    age: 23,
    log: 'Hello, MongoDB is Fun!!'
  }
]

Reference