Tuesday, 8 September 2020

 

                     M001: MongoDB Basics




Chapter 1: Introduction

Lab 1.1: Install Compass and Connect

Problem:

If you haven't downloaded Compass yet then please go back to this lecture Connecting to MongoDB Using Compass in Chapter 0 and follow the instructions mentioned there for downloading Compass and connecting to the class Atlas cluster.

If you have already downloaded Compass then please use this connection string for connecting to the class Atlas cluster :

mongodb+srv://m001-student:m001-mongodb-basics@cluster0-jxeqq.mongodb.net/test
  • Once connected, navigate to the Schema tab in Compass and hit Analyze Schema and answer the following question.

Question:

Which of the following field names appear in documents in the movies collection of the video database. Check all that apply.

Lab 1.2: Determine the Value Type, Part 1

Problem:

What is the value type of the ts field for documents in the 100YWeatherSmall.data collection?

Lab 1.4: Determine the Value Type, Part 3

Problem:

What is the value type of the year field for documents in the video.movies collection?

Lab 1.5: Scavenger Hunt, Part 1

Problem:

How many movies in the video collection were directed by Patty Jenkins. Stated more precisely, how many documents in the video.movies collection have a value of "Patty Jenkins" for the director field?
Lab 1.6: Scavenger Hunt, Part 2

Chapter 2: The MongoDB Query Language + Atlas

Lab 2.1: How Many Comedies?

Problem:

  1. If you have not already loaded the video.movieDetails collection, please review the lesson "Loading Data into Your Sandbox Cluster" for a tutorial. Then, use the script, loadMovieDetailsDataset.js, provided in the handouts for the lesson, "Connecting to an Atlas Cluster from the Mongo Shell" to load the movieDetails collection.
  2. Use Compass to connect to your sandbox cluster.
  3. In Compass, view the video.movieDetails collection and apply the filter {genres: "Comedy"}.

How many documents in video.movieDetails match the filter {genres: "Comedy"}?

Lab 2.2: How Many Inserted?

Problem:

If the collection video.myMovies is currently empty, how many documents would be inserted by the following call to insertMany().

Lab 2.4: Queries on Array Fields, Part 1

Problem:

Explore the movieDetails collection that you loaded into your Atlas sandbox cluster and then issue a query to answer the following question. How many movies in the movieDetails collection list "Family" among its genres?

You will find the count() method useful in answering this question using the mongo shell.

Lab 2.5: Queries on Array Fields, Part 2

Problem:

Explore the movieDetails collection that you loaded into your Atlas sandbox cluster and then issue a query to answer the following question. How many movies in the movieDetails collection list "Western" second among its genres?

You will find the count() method useful in answering this question using the mongo shell.

Lab 2.6: Update Operators

Problem:

To work on this lab, you need to connect to the sandbox cluster in which you have imported the video.movieDetails collection.

Suppose you wish to update the value of the plot field for one document in our movieDetails collection to correct a typo. Which of the following update operators and modifiers would you need to use to do this?


Chapter 3: Deeper Dive into the MongoDB Query Language

Challenge Problem: Single Value in an Array of Integers

Solution: for shell => db.score.find({results:{$elemMatch: {$gte: 70},{$lte: 80 }}})

For compass =>{results: {$elemMatch: {"score": {$gte: 70}, "score": {$lte: 80 }}}}



Final Exam

Final Exam: Question 1

Problem:

Connect to our class Atlas cluster from Compass and view the citibike.trips collection.

Use the schema view and any filters you feel are necessary to determine the range of values for the usertype field. Which of the following are values found in this collection for the field usertype?

Solution: 

Final Exam: Question 2

Problem:

Connect to our class Atlas cluster from Compass and view the 100YWeatherSmall.data collection.

Using the Schema view, explore the wind field. The wind field has the value type of document. Which of the following best describes the schema of this embedded document?

Final Exam: Question 3

Problem:

Connect to our class Atlas cluster from Compass and view the 100YWeatherSmall.data collection.

What is the value type of the "wind.speed.rate" field?

Final Exam: Question 4

Problem:

Please connect to our class Atlas cluster and view the citibike database.

How many documents in the citibike.trips collection have the key tripduration set to null? Ignore any documents that do not contain the tripduration key.

Final Exam: Question 5

Final Exam: Question 6

Problem:

Please connect to our class Atlas cluster from the mongo shell or Compass and view the video.movies collection.

How many movies match the following criteria? - The cast includes either of the following actors: "Jack Nicholson", "John Huston". - The viewerRating is greater than 7. - The mpaaRating is "R".


Wednesday, 15 July 2020

mongod: error while loading shared libraries: libnetsnmpmibs.so.31: cannot open shared object file: No such file or directory


Error Details: 

[mongod@opsmanager bin]$ ./mongod -f /u01/app/mongo/data/mongod.conf
./mongod: error while loading shared libraries: libnetsnmpmibs.so.31: cannot open shared object file: No such file or directory

Solution install package net-snmp

mongod@opsmanager bin]$ yum install net-snmp
Loaded plugins: fastestmirror, langpacks
You need to be root to perform this command.
[mongod@opsmanager bin]$ sudo yum install net-snmp
[sudo] password for mongod:
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.mirror.lstn.net
 * extras: repos-tx.psychz.net
 * updates: repo1.ash.innoscale.net
base                                                                                                                                 | 3.6 kB  00:00:00
extras                                                                                                                               | 2.9 kB  00:00:00
updates                                                                                                                              | 2.9 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package net-snmp.x86_64 1:5.7.2-48.el7_8.1 will be installed
--> Processing Dependency: net-snmp-libs = 1:5.7.2-48.el7_8.1 for package: 1:net-snmp-5.7.2-48.el7_8.1.x86_64
--> Processing Dependency: net-snmp-agent-libs = 1:5.7.2-48.el7_8.1 for package: 1:net-snmp-5.7.2-48.el7_8.1.x86_64
--> Processing Dependency: libnetsnmptrapd.so.31()(64bit) for package: 1:net-snmp-5.7.2-48.el7_8.1.x86_64
--> Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: 1:net-snmp-5.7.2-48.el7_8.1.x86_64
--> Processing Dependency: libnetsnmpagent.so.31()(64bit) for package: 1:net-snmp-5.7.2-48.el7_8.1.x86_64
--> Running transaction check
---> Package net-snmp-agent-libs.x86_64 1:5.7.2-48.el7_8.1 will be installed
---> Package net-snmp-libs.x86_64 1:5.7.2-37.el7 will be updated
---> Package net-snmp-libs.x86_64 1:5.7.2-48.el7_8.1 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================
 Package                                    Arch                          Version                                      Repository                      Size
============================================================================================================================================================
Installing:
 net-snmp                                   x86_64                        1:5.7.2-48.el7_8.1                           updates                        332 k
Installing for dependencies:
 net-snmp-agent-libs                        x86_64                        1:5.7.2-48.el7_8.1                           updates                        707 k
Updating for dependencies:
 net-snmp-libs                              x86_64                        1:5.7.2-48.el7_8.1                           updates                        751 k

Transaction Summary
============================================================================================================================================================
Install  1 Package  (+1 Dependent package)
Upgrade             ( 1 Dependent package)

Total size: 1.7 M
Total download size: 1.0 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/updates/packages/net-snmp-5.7.2-48.el7_8.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY --:--:-- ETA
Public key for net-snmp-5.7.2-48.el7_8.1.x86_64.rpm is not installed
(1/2): net-snmp-5.7.2-48.el7_8.1.x86_64.rpm                                                                                          | 332 kB  00:00:00
(2/2): net-snmp-agent-libs-5.7.2-48.el7_8.1.x86_64.rpm                                                                               | 707 kB  00:00:00
------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                       912 kB/s | 1.0 MB  00:00:01
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 1:net-snmp-libs-5.7.2-48.el7_8.1.x86_64                                                                                                  1/4
  Installing : 1:net-snmp-agent-libs-5.7.2-48.el7_8.1.x86_64                                                                                            2/4
  Installing : 1:net-snmp-5.7.2-48.el7_8.1.x86_64                                                                                                       3/4
  Cleanup    : 1:net-snmp-libs-5.7.2-37.el7.x86_64                                                                                                      4/4
  Verifying  : 1:net-snmp-agent-libs-5.7.2-48.el7_8.1.x86_64                                                                                            1/4
  Verifying  : 1:net-snmp-5.7.2-48.el7_8.1.x86_64                                                                                                       2/4
  Verifying  : 1:net-snmp-libs-5.7.2-48.el7_8.1.x86_64                                                                                                  3/4
  Verifying  : 1:net-snmp-libs-5.7.2-37.el7.x86_64                                                                                                      4/4

Installed:
  net-snmp.x86_64 1:5.7.2-48.el7_8.1

Dependency Installed:
  net-snmp-agent-libs.x86_64 1:5.7.2-48.el7_8.1

Dependency Updated:
  net-snmp-libs.x86_64 1:5.7.2-48.el7_8.1

Complete!

[mongod@opsmanager bin]$ ./mongod -f /u01/app/mongo/data/mongod.conf

about to fork child process, waiting until server is ready for connections.

forked process: 20740

child process started successfully, parent exiting

step-5 connect database


Friday, 10 July 2020

WARNING: Unable to raise nofile/nproc limit to 64000. Ops Manager may not function correctly.


Warning:

WARNING: Unable to raise nofile limit to 64000. Ops Manager may not function correctly.
WARNING: Unable to raise nproc limit to 64000. Ops Manager may not function correctly.


Solution: 

Add below lines to file /etc/security/limits.d/20-nproc.conf  


mongo     soft    nproc   2047
mongo     hard    nproc   64000
mongo     soft    nofile  4096
mongo     hard    nofile  65536
mongo     soft    stack   10240

Note: here mongo is my os user from which i am installing ops manager.

[mongo@opsmanager bin]$ cat  /etc/security/limits.d/20-nproc.conf
# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.

*          soft    nproc     64000
root       soft    nproc     unlimited
mongo     soft    nproc   2047
mongo     hard    nproc   64000
mongo     soft    nofile  4096
mongo     hard    nofile  65536
mongo     soft    stack   10240

Thursday, 9 July 2020

Start mongo database(mongod) automatically after system reboot

After OS patching process we need a database administrator support to stop and start database.To avoid such small dependency we can write small shell script which will take care of database startup process after system Reboot. In below example we are creating a shell script and seting it in crontab, so database will be start automatically after system reboot.  

1] creating a test.js file for specify database commands to test connectivity.

[mongo@localhost ~]$ cat test.js
print("Successfully connect to Test database ");
db.getName();

2] creating main shell script db_startup.sh

[mongo@localhost ~]$ cat db_startup.sh
#!/bin/bash

export PATH=/u01/mongo/mongodb-linux-x86_64-rhel70-4.2.8/bin:$PATH

export MONGO_HOME=/u01/mongo/mongodb-linux-x86_64-rhel70-4.2.8

export LOG_FILE="/home/mongo/STARTUP_LOG.txt"
export MAIL_FROM=abc@axmple.com
export MAIL_TO=pqr@example.com
cd /u01/mongo/mongodb-linux-x86_64-rhel70-4.2.8/bin

echo "===========Starting Database========" > $LOG_FILE
echo " " >> $LOG_FILE
echo " " >> $LOG_FILE

mongod -f /u01/DATA/appdb/mongod.conf >> $LOG_FILE

echo  " "  >> $LOG_FILE
echo  " "  >> $LOG_FILE
echo "-------checking database status ------" >> $LOG_FILE

echo  " " >> $LOG_FILE
echo  " " >> $LOG_FILE

if [ `ps -ef | grep -i mongod.conf | wc -l` -le 1 ]; then
    echo "mongodb not running" >> $LOG_FILE
    exit 1
else
    echo "database started successfully" >> $LOG_FILE
fi

echo "=======checking connectivity of database======" >> $LOG_FILE
echo " ">> $LOG_FILE
echo " ">> $LOG_FILE
./mongo -u admin -p Admin1234 < /home/mongo/test.js  >> $LOG_FILE

cat $LOG_FILE | mailx -r $MAIL_FROM -s "mongodb status After server reboot" -a $LOG_FILE $MAIL_TO

exit

3] specify the entry in crontab for above shell script. 

[mongo@localhost ~]$ crontab -e
crontab: installing new crontab

[mongo@localhost ~]$ crontab -l
@reboot sleep 60 && /home/mongo/db_startup.sh > /home/mongo/db_startup.log
[mongo@localhost ~]$

4] reboot the system for test the script.

5] Output of log file:

[mongo@localhost ~]$ cat STARTUP_LOG.txt

===========Starting Database========


about to fork child process, waiting until server is ready for connections.
forked process: 10178
child process started successfully, parent exiting


-------checking database status ------


database started successfully
=======checking connectivity of database======


MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("bbd16589-fc43-48ca-a16b-1b65cd77c9f0") }
MongoDB server version: 4.2.8
Successfully connect to Test database
test
bye

Login to the database as user C##GGUSER failed because of error Error while trying to retrieve text for error ORA-01804.

 Error :  2026-09-19 11:28:18  ERROR   OGG-00303  Unable to connect to database using user C##GGUSER@CDB. Ensure that the necessary privileg...