quarta-feira, 30 de novembro de 2022

[Mongo DB] - Basic sort and read operations (or "R" from CRUD)

Hello all

In the last post (here), I have talked about create operations and running inserts, or the "C" from CRUD.

Today, I'm going to talk about executing read operations in Mongo DB, the "R" in CRUD. I'll talk about sort, as well.

But remember, I'm talking about basic operations. As we know, the Mongo DB world is much bigger than this post.  

Let's get started!!!

The references are here and here.

-- List all
db.movies.find()


-- Find the first row matching the object
db.movies.findOne({trilogy: "Transformers"})


-- Limit the number of rows
db.movies.find().limit(3)


-- Count the number of rows
db.movies.find().count()


-- Where trilogy="Transformers"
db.movies.find( {trilogy: "Harry Potter"} )


-- List fields _id, codigo, sequence, year
db.movies.find( {trilogy: "Transformers"}, { codigo: 1, sequence: 1, year: 1 })


-- List fields withouth id
db.movies.find( {trilogy: "Transformers"}, { _id: 0, codigo: 1, sequence: 1, year: 1 })


-- Concat Trilogy - Sequence
db.movies.find({trilogy: "Transformers"},{ _id: 0, codigo: 1, year: 1, name: {$concat: ["$trilogy"," - ","$sequence"]} })


-- List rows with codigo are between 5 and 12
db.movies.find({ codigo: { $gt: 5, $lt: 12 } },{ _id: 0, codigo: 1, year: 1, name: {$concat: ["$trilogy"," - ","$sequence"]} })


-- List rows with codigo are between 5 and 12 and Trilogy="Transformers"
db.movies.find({ codigo: { $gt: 5, $lt: 12 }, trilogy: "Transformers"},{ _id: 0, codigo: 1, year: 1, name: {$concat: ["$trilogy"," - ","$sequence"]} })


-- List rows with codigo are between 5 and 12 and Sort - Year Desc, Name Asc
db.movies.find({ codigo: { $gt: 5, $lt: 12 } },{ _id: 0, codigo: 1, year: 1, name: {$concat: ["$trilogy"," - ","$sequence"]} }).sort({"year": -1, "name": 1 })


That's all folks!!!

Yes, the reading operations are easy!!! 

I hope that it helps you!!!

Regards
Mario 

quinta-feira, 24 de novembro de 2022

[MongoDB] - Logical Structures and basic create operations (or "C" from CRUD)

Hello all

Today, we talk about Logical Structures in MongoDB and how do you insert one or many register into a collection.

But first, we need to understand what a database is, a collection, a document and a field in MongoDB.

For example, one database can have many collections. A collection can have many documents, and documents can have fields. It's easy :) 

As the glossary definitions:

Database
A physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.

Collection
A grouping of MongoDB documents. A collection is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection have a similar or related purpose. 

Document
A record in a MongoDB collection and the basic unit of data in MongoDB. Documents are analogous to JSON objects but exist in the database in a more type-rich format known as BSON.

Documents have the following restrictions on field names:
  • The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array. If the _id contains subfields, the subfield names cannot begin with a ($) symbol.
  • Field names cannot contain the null character.
  • The server permits storage of field names that contain dots (.) and dollar signs ($). MongoDB 5.0 adds improved support for the use of ($) and (.) in field names. There are some restrictions. 
Field
A name-value pair in a document. A document has zero or more fields. Fields are analogous to columns in relational databases. 

Ok, it's easy. Now, we'll create a new database, a new collection and insert the first documents on the collection. Of course, we'll list the documents.

View all databases
show dbs
show databases





Create new database or switch database 
use thordb

View current Database
db

Show all collections in database
show collections

Create collection call movies
db.createCollection('movies')

As soon as possible, we'll have a post just talking about Create Collections.









Insert a single row
db.movies.insertOne({codigo: 1, trilogy: "Harry Potter", sequence: "The Philosophers Stone", year: "2001"})







Insert many rows
db.movies.insertMany ([
{codigo: 2, trilogy: "Harry Potter", sequence: "Harry Potter and the Chamber of Secrets", year: "2002"},
{codigo: 3, trilogy: "Harry Potter", sequence: "Harry Potter and the Prisoner of Azkaban", year: "2004"},
{codigo: 4, trilogy: "Harry Potter", sequence: "Harry Potter and the Goblet of Fire", year: "2005"},
{codigo: 5, trilogy: "Harry Potter", sequence: "Harry Potter and the Order of the Phoenix", year: "2007"},
{codigo: 6, trilogy: "Transformers", sequence: "Transformers: Dark of the Moon", year: "2011"},
{codigo: 7, trilogy: "Transformers", sequence: "Transformers: Age of Extinction", year: "2014"},
{codigo: 8, trilogy: "Transformers", sequence: "Transformers: The Last Knight", year: "2017"},
{codigo: 9, trilogy: "Harry Potter", sequence: "Harry Potter and the Half-Blood Prince", year: "2009"},
{codigo: 10, trilogy: "Harry Potter", sequence: "Harry Potter and the Deathly Hallows P1", year: "2010"},
{codigo: 11, trilogy: "Transformers", sequence: "Transformers: Revenge of the Fallen", year: "2009"},
{codigo: 12, trilogy: "Harry Potter", sequence: "Harry Potter and the Deathly Hallows P2", year: "2011"},
{codigo: 13, trilogy: "Transformers", sequence: "Transformers", year: "2007"},
{codigo: 14, trilogy: "Transformers", sequence: "Bumblebee", year: "2018"},
{codigo: 15, trilogy: "Harry Potter", sequence: "Fantastic Beasts and Where to Find Them", year: "2016"}
])



















Show all rows in a collection 
db.movies.find()




























Yes baby, today you learned the basic about the "C" or "Create Operations" from CRUD in MongoDB.

That's it. As I said, it was easy. The basic always is easy!!!

On next posts, we'll learn about the "R" or "Read Operations" and we'll learn about array operations. 

I hope that it helps you.

Regards 
Mario

Previous articles:

quinta-feira, 17 de novembro de 2022

[MongoDB] - How to install?

Hello all

As you know, I have been studying new products, and I started to play with MongoDb. 


This is being fantastic journey at the moment. 

And I have thought, while I'm learning, I can share it with you, and we can learn together.

Today, we gonna install the MongoDB for tests.

Yes, it's the first installation and the real world may be different - and it's, probably, but this post may help people like me who need a push to take the first step into a new technology. 

But just remember:
1) This work for me, but it might not work for you.
2) It's a basic install. As we go along, we'll get better and better.
3) 
This post is just for study and tests as well and it has no concern for Best Practices, at this moment.
        4) On next posts, we'll think in Best Practices and Security.

All steps we find here:

Thank you, my friend Adriano Bonacin for the Yadax Blog. The blog is helping me a lot.

So, let's go to the party!!!

For my installation, I created an EC2 instance in Free Tier from AWS using CentOS.

1) Configure yum repo.

sudo vi /etc/yum.repos.d/mongodb-org-6.0.repo 
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc

sudo yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: download.cf.centos.org
* extras: download.cf.centos.org
* updates: download.cf.centos.org
repo id                          repo name                    status
base/7/x86_64              CentOS-7 - Base              10,072
extras/7/x86_64            CentOS-7 - Extras             515
mongodb-org-6.0/7       MongoDB Repository        55
updates/7/x86_64          CentOS-7 - Updates         4,385

2) Install mongodb-org

sudo yum install -y mongodb-org
    Installed:
    mongodb-org.x86_64 0:6.0.3-1.el7

    Dependency Installed:
    cyrus-sasl.x86_64 0:2.1.26-24.el7_9                            
    cyrus-sasl-gssapi.x86_64 0:2.1.26-24.el7_9
    cyrus-sasl-plain.x86_64 0:2.1.26-24.el7_9
    mongodb-database-tools.x86_64 0:100.6.1-1
    mongodb-mongosh.x86_64 0:1.6.0-1.el8
    mongodb-org-database.x86_64 0:6.0.3-1.el7
    mongodb-org-database-tools-extra.x86_64 0:6.0.3-1.el7
    mongodb-org-mongos.x86_64 0:6.0.3-1.el7             
    mongodb-org-server.x86_64 0:6.0.3-1.el7
    mongodb-org-tools.x86_64 0:6.0.3-1.el7
    Complete!

sudo yum install -y mongodb-org-6.0.2 mongodb-org-database-6.0.2 mongodb-org-server-6.0.2 mongodb-mongosh-6.0.2 mongodb-org-mongos-6.0.2 mongodb-org-tools-6.0.2
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: download.cf.centos.org
     * extras: download.cf.centos.org
     * updates: download.cf.centos.org
    Package matching mongodb-org-6.0.2-1.el7.x86_64 already installed. Checking for update.
    Package matching mongodb-org-database-6.0.2-1.el7.x86_64 already installed. Checking for update.
    Package matching mongodb-org-server-6.0.2-1.el7.x86_64 already installed. Checking for update.
    No package mongodb-mongosh-6.0.2 available.
    Package matching mongodb-org-mongos-6.0.2-1.el7.x86_64 already installed. Checking for update.
    Package matching mongodb-org-tools-6.0.2-1.el7.x86_64 already installed. Checking for update.
    Nothing to do

3) Today, we'll use the Default Directories
    /var/lib/mongo      (the data directory)
    /var/log/mongodb (the log directory)

The package manager creates the default directories during installation, and the the owner and group name as well. The user and group are mongod.

    id mongod
    uid=996(mongod) gid=994(mongod) groups=994(mongod)

4) Install the SElinux Policy
    
    sudo yum install git make checkpolicy policycoreutils selinux-policy-devel
        
        Installed:
git.x86_64 0:1.8.3.1-23.el7_8
        selinux-policy-devel.noarch 0:3.13.1-268.el7_9.2

Dependency Installed:
m4.x86_64 0:1.4.16-10.el7             
        perl.x86_64 4:5.16.3-299.el7_9               
        perl-Carp.noarch 0:1.26-244.el7         
        perl-Encode.x86_64 0:2.51-7.el7
        perl-Error.noarch 1:0.17020-2.el7
        perl-Exporter.noarch 0:5.68-3.el7
        perl-File-Path.noarch 0:2.09-2.el7
        perl-File-Temp.noarch 0:0.23.01-3.el7
        perl-Filter.x86_64 0:1.49-3.el7
        perl-Getopt-Long.noarch 0:2.40-3.el7
        perl-Git.noarch 0:1.8.3.1-23.el7_8
        perl-HTTP-Tiny.noarch 0:0.033-3.el7
        perl-PathTools.x86_64 0:3.40-5.el7
        perl-Pod-Escapes.noarch 1:1.04-299.el7_9
        perl-Pod-Perldoc.noarch 0:3.20-4.el7
        perl-Pod-Simple.noarch 1:3.28-4.el7
        perl-Pod-Usage.noarch 0:1.63-3.el7
        perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 
        perl-Socket.x86_64 0:2.010-5.el7
        perl-Storable.x86_64 0:2.45-3.el7
        perl-TermReadKey.x86_64 0:2.30-20.el7
        perl-Text-ParseWords.noarch 0:3.29-4.el7
        perl-Time-HiRes.x86_64 4:1.9725-3.el7
        perl-Time-Local.noarch 0:1.2300-2.el7
        perl-constant.noarch 0:1.27-2.el7
        perl-libs.x86_64 4:5.16.3-299.el7_9
        perl-macros.x86_64 4:5.16.3-299.el7_9
        perl-parent.noarch 1:0.225-244.el7
        perl-podlators.noarch 0:2.5.1-3.el7
        perl-threads.x86_64 0:1.87-4.el7
        perl-threads-shared.x86_64 0:1.43-6.el7
        policycoreutils-devel.x86_64 0:2.5-34.el7
        Complete!

sudo git clone https://github.com/mongodb/mongodb-selinux
Cloning into 'mongodb-selinux'...
remote: Enumerating objects: 45, done.
remote: Counting objects: 100% (45/45), done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 45 (delta 17), reused 25 (delta 7), pack-reused 0
Unpacking objects: 100% (45/45), done.

cd mongodb-selinux
sudo make
(cd selinux; make -f /usr/share/selinux/devel/Makefile)
make[1]: Entering directory `/root/mongodb-selinux/selinux'
Compiling targeted mongodb module
/usr/bin/checkmodule:  loading policy configuration from tmp/mongodb.tmp
/usr/bin/checkmodule:  policy configuration loaded
/usr/bin/checkmodule:  writing binary representation (version 19) to tmp/mongodb.mod
Creating targeted mongodb.pp policy package
rm tmp/mongodb.mod tmp/mongodb.mod.fc
make[1]: Leaving directory `/root/mongodb-selinux/selinux'
mkdir -p build/targeted
mv selinux/mongodb.pp build/targeted/

sudo make install
cp build/targeted/mongodb.pp /usr/share/selinux/targeted/mongodb.pp
/usr/sbin/semodule --priority 200 --store targeted --install /usr/share/selinux/targeted/mongodb.pp
libsemanage.semanage_direct_install_info: Overriding mongodb module at lower priority 100 with module at priority 200.
/sbin/fixfiles -R mongodb-enterprise-server restore || true
Warning: Skipping the following R/O filesystems:
/sys/fs/cgroup
mongodb-enterprise-server not found
/sbin/fixfiles -R mongodb-org-server restore || true
Warning: Skipping the following R/O filesystems:
/sys/fs/cgroup
/sbin/restorecon -R /var/lib/mongo || true
/sbin/restorecon -R /run/mongodb || true

5) Start Mongo DB

    sudo systemctl start mongod

    sudo mongod --version
  

    sudo systemctl status mongod

6) Check the startup status
    systemctl is-enabled mongod; echo $?
        disabled
        0

    sudo systemctl enable mongod

You can follow important messages on logfile:
sudo tail -f /var/log/mongodb/mongod.log

7) And that's it, your MongoDb is installed.

Yes, I know that we need to do some configurations, but we'll make this on next post.

I hope that help you!!!

Regards
Mario

terça-feira, 1 de novembro de 2022

Oracle RAC - Install & Patch at the same time - From 19.3.0.0 to 19.17.0.0

Hello all

Yesterday, I was installing a new Oracle RAC 19c and I had an unusual error in the Oracle Base directory.

I was installing the base version of Grid, 19.3.0.0.

The error was this: 
INS-4400 - The Oracle Home location contains directories or files on following remote nodes.


I asked in the GUOB user group and after many checks, the solution was to apply the RU before installation. My friends Vini, Mufalani & Rodrigo Jorge helped me to check these points. Thanks.
This is the Step-by-Step:

1) Create a new GRID HOME
cd /u01/app
mkdir -p 19.17.0.0/grid
export GRID_HOME=/u01/app/19.17.0.0/grid
2) Update Opatch
Check & download Opatch last version (6880880)
cd $GRID_HOME
unzip /u01/INSTALL/opatch/p6880880_190000_LINUX.zip -o /u01/app/19.17.0.0/grid
3) Important (tip from Rodrigo Jorge 😎):



4) Applying the patch "GI RELEASE UPDATE 19.17.0.0.0 (System Patch)"
cd $GRID_HOME
unzip p34416665_190000_Linux-x86-64.zip
./gridSetup.sh -applyRU 34416665
Preparing the home to patch...
Applying the patch 34416665...
Successfully applied the patch.
The log can be found at: /tmp/GridSetupActions2022-10-31_04-51-49PM/installerPatchActions_2022-10-31_04-51-49PM.log

After that, it was easy to continue the installation.
I hope that help you.

Regards
Mario


Postagem em destaque

[ORACLE] How to move a datafile to a new location in ASM.

Hello everyone! I hope you're doing well! Today, I needed to move a datafile to another ASM Disk Group. The process was simple, but reme...