Mongodb

From Bitpost wiki
Revision as of 16:33, 9 June 2021 by M (talk | contribs)

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

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

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