Mongodb: Difference between revisions

From Bitpost wiki
Line 65: Line 65:
  sudo service mongod start
  sudo service mongod start
  # start any deps (eg rc)
  # start any deps (eg rc)
=== Upgrade 4.2 to 4.4 ===
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)
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 ===


== Convert a single node to a replica set ==
== Convert a single node to a replica set ==

Revision as of 19:23, 19 April 2022

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 "printjson(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

https://www.mongodb.com/docs/v4.2/release-notes/4.2-upgrade-replica-set/
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

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)

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

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

Manually:

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 && sudo apt install -y mongodb-org # or mongodb-org-shell for just client shell