Mongodb

From Bitpost wiki

UI tools

  • MongoDB_Admin
git clone https://github.com/hatamiarash7/MongoDB_Admin
cd MongoDB_Admin
npm start &
  • Robo 3T
~/apps/robo3t/bin/robo3t &

Querying

  • OR
{"$or": [{"firstName": "Michael"}, {"firstName": "Tom"}]}
  • To remove all documents that do not have a certain value:
db.inventory.remove( { type : { $ne: "food" } } )
  • To remove all documents that do not have a range of values:
db.inventory.remove( { type : { $nin: ["Apple", "Mango"] } } )

Run a script or statement from command line

# This will drop the bookstore db
mongo bookstore --eval "db.dropDatabase()"

Upgrade

Upgrading is a NIGHTMARE and, like postgres, you should export / import the data to guarantee you don't lose it.

At LEAST do a mongodump before starting!

The problem is... mongo devs are TOO LAZY to write the code to auto-update. So you have to follow their stupid little instructions, which basically involve moving through upgrade steps across specific versions. And each version requires a bunch of apt foo. It SUCKS and MAKES ME SAD. It is utterly shameful.

Upgrade 3.6 to 4.0

mongo
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) # make sure it's 3.6
cfg = rs.conf();
cfg.protocolVersion=1;
rs.reconfig(cfg);
quit()

Now upgrade apt version, following 4.0 install instructions...

wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
# ignore the fact that that was for 18.04.  Fucking mongo morons.
sudo apt update
sudo apt-get install -y mongodb-org
# WOW it was a fuckign mess, had to do this, then redo:
sudo apt -f remove mongo*
sudo apt-get install -y mongodb-org
# OMG they just fucking HAD to rename the config file!
diff /etc/mongodb.conf /etc/mongod.conf
# migrate the old settings to the new location and push to git
# NOTE that the service name changed from mongodb to mongod!  stop the old, start the new.

Upgrade 4.0 to 4.2

mongo
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) # make sure it's 4.0
db.adminCommand( { setFeatureCompatibilityVersion: "4.0" } )
quit()
# stop dependencies (eg rc)
sudo service mongod stop
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
# (yes it's for 18.04, stupid asses)
sudo apt update
sudo apt-get install -y mongodb-org mongodb-org-shell mongodb-org-server mongodb-org-tools
sudo service mongod start
# start any deps (eg rc)

Upgrade 4.2 to 4.4

mongo
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) # make sure it's 4.2
db.adminCommand( { setFeatureCompatibilityVersion: "4.2" } )
quit()
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
sudo apt update
# stop dependencies (eg rc)
sudo service mongod stop
sudo apt remove mongodb-org*
sudo apt install -y mongodb-org mongodb-org-shell mongodb-org-server mongodb-org-tools
sudo service mongod start
# start any deps (eg rc)

Yer dun.

At first try, I skipped the featureCompatibilityVersion fix, and FUKING PAID FOR IT... read on if you must...

GOD GOD GOD DAMN DAMNDAMN IT it will NOT run bc i didn't do their FUCKING STUPID featureCompat BULLSHIT now i have to DOWNGRADE< fix it, and REINSTALL

sudo apt remove mongodb-org*
sudo rm /etc/apt/sources.list.d/mongodb-org-4.4.list 
sudo apt update
sudo apt install -y mongodb-org mongodb-org-shell mongodb-org-server mongodb-org-tools
sudo service mongod start
mongo
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) # make sure it's 4.2
# FUCK ME, yep it's 4.0
db.adminCommand( { setFeatureCompatibilityVersion: "4.2" } )
quit()

FUCKING START THE UPGRADE AGAIN FUCK YOU MONGO YOU ARE TERRIBLE DEVS

🦈 m@jaws  [~/config/etc] sudo rm /etc/apt/sources.list.d/mongodb-org-4.0.list 
🦈 m@jaws  [~/config/etc] sudo rm /etc/apt/sources.list.d/mongodb-org-4.2.list
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
sudo apt remove mongodb-org*
sudo apt update
sudo apt install -y mongodb-org mongodb-org-shell mongodb-org-server mongodb-org-tools
sudo service mongod start

NOW it's happy. FUCK OFF ASSHOLES. YOU ARE NOT AS IMPORTANT AS YOU THINK YOU ARE. NO ONE CARES ABOUT YOUR DEV INTERNALS FUCK OFF.

Upgrade 4.4 to 5.0

mongo
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) # make sure it's 4.4
db.adminCommand( { setFeatureCompatibilityVersion: "4.4" } )
quit()
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
sudo rm /etc/apt/sources.list.d/mongodb-org-4.4.list 
# stop dependencies (eg rc)
sudo service mongod stop
sudo apt remove mongodb-org*
sudo apt update
sudo apt install -y mongodb-org mongodb-org-shell mongodb-org-server mongodb-org-tools mongodb-mongosh
sudo service mongod start
# start any deps (eg rc)

OMG IT IS CORING. ALL IT IS DOING IS CORING. Bc the ASSHOLES now require advanced CPU flags, and couldn't be FUCKED to check for them - they just core.

To fix it, change proxmox CPU type to "host". A very capable CPU type that is >= Sandy Bridge.

The world REALLY needs to stop using this difficult bullshit. Ahh... remember grasshoppa... postgres is even worse...

Upgrade 4.4 Replica Set to 5.0

Ensure all nodes are healthy, connected; repeat steps as needed for each node.

# pick a SECONDARY node
mongo
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } ) # make sure it's 4.4
db.adminCommand( { setFeatureCompatibilityVersion: "4.4" } )
use admin
db.shutdownServer()
quit()
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
sudo rm /etc/apt/sources.list.d/mongodb-org-4.4.list 
sudo service mongod stop
sudo apt remove mongodb-org*
sudo apt update && sudo apt upgrade && sudo apt autoremove
# OR... upgrade the whole OS while you're down, if needed!
sudo apt install -y mongodb-org mongodb-org-shell mongodb-org-server mongodb-org-tools mongodb-mongosh
sudo service mongod start
# repeat for all SECONDARIES
# step down PRIMARY to SECONDARY and repeat upgrade
# step it back up to PRIMARY (if desired)
# Update the feature set on PRIMARY (it will replicate to others)
db.adminCommand( { setFeatureCompatibilityVersion: "5.0" } )

Convert a single node to a replica set

This is apparently required for transactions to work...?

  • CONVERTING TO SINGLE-NODE REPLICA SET:
       sudo emacs -nw /etc/mongod.conf
           # -----------------------
           # MBM enable replication
           replication:
               replSetName: esrs1
           # -----------------------

       sudo service mongod restart
       sudo mongo # may take a minute to be available...
         rs.initiate()

       # hit return after a moment to ensure it becomes PRIMARY

Installation

mh-install-mongodb (NEEDS WORK)

Manually:

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
sudo apt update && sudo apt install -y mongodb-org # or mongodb-org-shell for just client shell

Install mongosh on Debian bullseye

Just painful enough to be a pita. We often only need the shell but you need to set up full mongo apt repo. Shell isn't released for Bullseye so you have to use the Buster repo. Here's a mashup of the bullshit needed...

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-org mongodb-org-shell mongodb-org-server mongodb-org-tools mongodb-mongosh
sudo service mongod start

Repair

When mongo cores on startup, first verify that it's not the CPU instruction bug described in the 5.0 upgrade. After you've checked the logs and ruled out other problems, try a repair:

sudo mongod --dbpath /var/lib/mongodb --repair
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo service mongod start