Quick Access To Mongodb Commands Cheat Sheet

21 January,2019 by

MongoDB is an NoSQL  document-oriented database program.  These are  my quick notes to  support MongoDB.

Mongo

How to install MongoDB Enterprise on Red Hat Linux

–create a repo file and insert the text below

vi /etc/yum.repos.d/mongodb-enterprise.repo

[mongodb-enterprise]

name=MongoDB Enterprise Repository

baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/3.4/$basearch/

gpgcheck=1

enabled=1

gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

–install the package

sudo yum install -y mongodb-enterprise

MongoDB version

db.version()

Log Into MongoDB

mongo – <my_username> -p <password> –authenticationDatabase <my_db>

Log into MongoDB Remotely

mongo -host <servername:port> -u <my_username> -p <password> –authenticationDatabase <my_db>

Show all DBs

show dbs

Create the user Administrator 

Note:Once you’ve created the admin Users, you’ll need to disconnect from the Mongo Shell & restart the Mongo Server

use admin
db.createUser(
{
user: “myUserAdmin”,
pwd: “abc123”,
roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ]
}
)

Start the MongoDB service

sudo service mongod start

Start at the next system reboot

sudo chkconfig mongod on

To allow remote connections

Step1 : how to create a user in a db with readWrite 

db.createUser({

user: ‘robert’,

pwd: ‘myM0ng0_pw’,

roles: [{ role: ‘readWrite’, db:’testdb’}]

})

Step 2 : Get the private address

/sbin/ifconfig | grep “inet addr” | awk -F: ‘{print $2}’ | awk ‘{print $1}’

Step 3:edit mongo configuration file

–get private address from step 2 and add to bindip

vim /etc/mongod.conf

–change #security to:

security:

authorization: enabled

Step 4: Then from the terminal you can run:

First you must have the mongo cli installed in your system (https://docs.mongodb.org/manual/installation/).

mongo someurl.mongodomain.com:45475/database_name -u username -p password

List all databases on a Mongo Server

show dbs

Create a database

To create a new database in MongoDB, first: select the database to use

use my_new_database

To then check which database you’re on

> db

my_new_database

You must then enter at least one document to create the db

> db.new_collection.insert({ some_key: "some_value" })

Show current Mongo db

db

db.getName()

Switch database used

use MY_DATABASE_NAME

List users 

db.getUsers()    or    show users

MongoDB Admin password change

Note:the user making the change must have Administrator privileges

db.changeUserPassword(‘adminusr’,’<new_password>’)

Drop a user

use testdb
db.dropUser(“myUser1”, {w: “majority”, wtimeout: 5000})

Create a collection and insert on the fly

db.jacktest.insert ({ name: “Jack Vamvas”, company: “dba-ninja.com” })

List all collections in current database (extended information)

db.getCollectionInfos();

List all collections in current database (abbreviated)

db.getCollectionNames();

Read all rows in a collection

db.collection_name.find()

Get the current status of the replicaset

Note:returns the current status of the replicaset perspective from the server where command is executed

db.adminCommand( { replSetGetStatus: 1 } );

There are other NoSQL offers in other major DBMS such as SQL Server , and I’ll post a feature comparison soon . Asking the question : What are your data needs?   is the first step in deciding the platform

Author: Jack Vamvas

Leave a Reply

Discover more from ciquery

Subscribe now to keep reading and get access to the full archive.

Continue reading