Mongodb guide to basics

database by pexels

mongodb is a document type database aka NoSQL. The power of NoSQL DBs are its flexibility of unfixed schemas unlike in mysql/oracle SQL based database. A schema enforces fixed columns; thus columns will need to be defined before the database can be used. NoSQL type on the other hand allows key:value type of definition in each document (just like records), and these can differ from other documents in the same set.

In this guide, are references to basic mongodb commands that should get you started. 

installing mongodb

on macs use homebrew (https://brew.sh/) if you do not have it; get it. once installed, you can view the list of brew packages via the following command in terminal:

$ brew search [yourpackage]
$ brew search mongo

now you can install mongodb via

$ brew install mongodb

on linux (debian or ubuntu) use the following: (reference :https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ )

$ sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 9DA31620334BD75D9DCB49F368818C72E52529D4
$ echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
$ sudo apt-get update
$ sudo apt-get install -y mongodb-org

you are highly encouraged to refer to the above url (of mongodb community) to check the keys as well as the version of your OS so that you can install the appropriate distribution package intended for.

adding root user

If no users are configured in admin.system.users, one may access the database from the localhost interface without authenticating. Thus, from the server running the database (and thus on localhost), run the database shell and configure an administrative user:

$ mongo
$ use admin
$ db.createUser( { user: “mongoroot”, pwd: “mysuperdupermongorooterpassword”, roles: [ { role: “root” , db: “admin” } ] } )
$ db.createUser( { user: “mongoadmin”, pwd: “mysuperdupermongorooterpassword”, roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ] } )

now remove admin user

$ use admin
$ db.dropUser(“mongoadmin”)

turn on authentication by editing mongo configuration file. go to approximately line 28 and remove the # from auth=true. if you are not familar with vim, nano is just as good.

$ vim /etc/mongod.conf

now relaunch mongodb

$ service mongod restart

test logging in by using the mongo command with parsing password

$ mongo –username mongoroot –password –authenticationDatabase admin

setup single db admin

$ use mynewdb
$ db.createUser( { user: “accountUser”, pwd: “password”, roles: [ “readWrite”, “dbAdmin” ] } )

ref: http://docs.mongodb.org/manual/reference/method/db.createUser/

mongodb default location

  • configuration: /etc/mongodb.conf
  • database: /var/lib/mongodb

if you install via homebrew on mac, this will be different

mongodb basic commands

list all databases

$ show dbs

select a database

$ use mynewdb

show current db

$ db

add user

$ use mynewdb
$ db.createUser( { user: “mynewadmin”, pwd: “passwordchangeme”, roles: [ “readWrite”, “dbAdmin” ] } )

change password

$ db.changePassword(“mynewadmin”, “yetanothernewpassword”)

drop user

$ db.dropUser(“mynewadmin”)

add master admin

$ use admin
$ db.createUser( { user: “mongoroot”, pwd: “mysuperdupermongorooterpassword”, roles: [ { role: “root” , db: “admin” } ] } )
$ db.createUser( { user: “mongoadmin”, pwd: “mysuperdupermongoadminpassword”, roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ] } )

restarting mongo

$ service mongod restart

a sample setup mongodb

  • user: mongoprodroot
  • user: mongoprodadmin

$ db.createUser( { user: “mongoprodroot”, pwd: “yetanotheruniquepassword”, roles: [ { role: “root” , db: “admin” } ] } )
$ db.createUser( { user: “mongoprodadmin”, pwd: “yetanotheruniquepassword”, roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ] } )

vultr referral link

if you are setting up a cloud server, my referral link banner is below


Recent Posts
Why Job Sites Are Disappearing? · Apprentice, Journeyman, and Master · List of Docker Registries · Installing Debian 10 buster on virtualbox · Cross platform terminal emulators · Keep active keep happy · Tools to secure your devices · Secrets of the Face and Palm · Vim as an IDE for developers · WordPress multisite on Debian · Mongodb guide to basics · Jagged Alliance 2 some historic notes · Antivirus solutions and why its needed · A taovilla review and history · MacOS X high sierra running out of disk space · Airprint on bonjour missing scan function for all-in-one printers · Improving terminal load times · Knowing website design costs and pitfalls · Keeping secure and checking if your password is safe? · Installing Virtualbox Guest Additions for Debian Stretch · Windows XP Virtualbox on Mac OS X High Sierra · DNS settings and getting on public DNS · Extracting OVF from VMware ESXI 6.0 · Installing LMDE 2 Betsy · SSL Certificates and Encryption · Machine Learning with Spark · Installing Lubuntu in Virtualbox · Getting started with Docker (docker-toolbox) · Ubuntu upgrade limited boot space · Top 10 Browsers Overview · Taovilla Oracle app beta built using meteorjs · Setting up Debian on VirtualBox · The HomeBrew doctor · Afterlife Buddhist Chants · Caffeine icon for ubuntu / lubuntu · Updating wordpress without FTP · Preinstalled Languages on mainstream OS · Reminisce of Xcom · Java install on Ubuntu · Display image as ASCII art · VirtualBox Resizing Lubuntu Guest Linux · Introducing Samurai · Buddhist Chants · Macbook Pro Key mapping for Lubuntu · Common Apache Nginx commands · The nothingness between Zen and Tao · Tao, Zen and Theravada · Meditation for Growth · Buddhism · Refreshing Git versioned files · Basic Environments for Jira · Coffee Shops and Feng Shui · Converting a hg mercurial to git repository · Long term domain name costs comparison · SVG Graphing · 2 ways to Php install on mac · New EBook Launched Secrets of Human Face and Palm · Introducing Samurai and Installing Meteorjs to own server · Improving the speed of Linux Mint · Ma Zu Deity in Malaysia Sabah · SVG graphics with Javascript · FTP Clients for Mac OS X · Windows Apps on Mac · Programming Languages Compared · Hackers Natural Cure · Publishing on Ebooks · Editors such as SublimeText2 · Good Coding Practice · LMDE Linux Mint Debian Edition · Heroku Webapp · Hadoop Single node 32bit on 12.10 Mint XFCE · Tracing User Behaviour FRUBO · Installing Nginx on MacOSX · Installing Ruby on MacOS X · Turn off annoying compress outlook message · HTML5 Apps on Cordova PhoneGap with MacOSX · Choosing and using Latex · Reviewing Javascript World · Gemstones · Han Xin the Hero of Han · Gathered Wisdoms · Oriental Calendar · Li Qi Feng Shui · Five Elements Feng Shui · Taoist Philosophy Overview · 25th Anniversary Singapore Taoist Philosophy Group · Draw up an Ubuntu Server Environment · Draw up a Lightweight Mail Server · Draw up a Web Database · Php CMS and Frameworks · Drawing up a Web Service · Draw up a Virtual Cloud Server · Unlocking and Flashing your ZTE Blade · Tai Chi and Qi Gong · Pip error in Python with Homebrew · Setting up homebrew for OSX · Personality by Month · Life and Death in Oriental Beliefs · Windows and Linux Desktop Security · Ruan Tou Feng Shui · Life Analysis and Prediction · Cemetery Feng Shui · Taoism · Confucius · Guan Gong · Meditation · Gu Qin · TexMaker with Glossary on Mac · Quick way to OpenStack on Lubuntu · Ancient IT Dictionary · Oriental Poems · Fan Li · IChing · Dao De Jing · TeaTime for Basic Python · CVs and Jobhunting · Chinese Input on Linux Ubuntu · Research in Computer Science · Reviewing Applications on My Mac · Reviewing Applications on My Windows · Installing Windows XP · Installing wireless on Linux Ubuntu or Mint · Restoring an Amazon EC2 Backup · Mac Update PASV problem · Reviewing Green Computing · Consuming the Cloud Storage Space · Using Cherokee and WordPress Whine · Disabling Lubuntu Screensaver · Installing Lubuntu from USB drive · Searching for a Micro Mini CMS · ReStructuredText in Python · Rackspacecloud Vs Amazon · Hacking your old PSP · Attending Atlanta 55th MMM Conference · Smart devices for everyday use · Creative Programmers · Linux Settings · Embarking on Further Education · Machine Learning · Mac Tips – Viewing hidden files on desktop · Creating PDF files for free · Online Media for All · C# ASP Settings · SVN Git and Mercurial Versioning · Getting the last inserted id using datasets · Think Lucky and be Happy · AngieGallery Design · AceTravel Design · Letter to Mum · Prison Vs Work · Irish Daughter · Are you a carrot, an egg or a coffee bean? · Home Media Entertainment Setup · Collection of Interesting sites · Studying in London · Great Oriental eats in London · CMS Joomla, WordPress, Drupal compared ·

© 2024 · mirageglobe · using zenifywp theme by www.mirageglobe.com ·
  · RSS Feed