Mongodb

From Bitpost wiki
Revision as of 17:37, 19 April 2022 by M (talk | contribs)

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

Upgrade 4.0 to 4.2

https://www.mongodb.com/docs/v4.2/release-notes/4.2-upgrade-replica-set/

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