Mongo is a NoSQL database with many interesting features such as replication, flexible schemas, and transparent high availability among others.
The main features to highlight about Mongo are:
- Speed by eliminating functionalities (God bless them)
- No joins
- No transactions
The MySQL -> Mongo equivalent would be:
- Collection: Table
- Document: Row
An important recommendation in Mongo is not to repeat the value of the key in a JSON:
{
"name" : "John",
"age" : 25,
"name" : "Tom",
}
Mongo uses a data representation called BSON:
- Internal representation of data, so Mongo operates internally faster.
- On disk, data is in BSON, over the network it also travels in BSON, and the app driver finally transforms it to the native structure of the app, it does NOT have to go through JSON.
Now that we have a basic idea about Mongo, we proceed with the basic commands and their equivalent in MySQL:
- use pcat: create database pcat
- db.runCommand( { create: “products” } ): Create table
- show dbs: show databases
- show collections: show tables
We compile and install MongoDB:
We bind the service to all IPs:
bind_ip = 0.0.0.0
We access the MongoDb CLI:
We create the admin user:
use admin
db.createUser(
{
user: "admin",
pwd: "PASSWORD",
roles: [ "root" ]
}
)
We enable authentication:
security:
authorization: enabled
We restart the service:
We access using the credentials:
If we want to enable autologin, we can specify the credentials in the mongorc.js file:
db.getSiblingDB("admin").auth("admin", "PASSWORD");
Queries:
db.products.find() -> SELECT * FROM products
NOTE: The good thing about Mongo is that it applies a limit to queries by default. If we want to see the next records, we must press “it”. This way, we won’t end up with a console spitting out unnecessary collection (table) data.
db.products.findOne() -> SELECT * FROM products limit 1
db.products.find().limit(3) -> SELECT * FROM products limit 3
db.products.count() -> SELECT COUNT(*) FROM products
All queries in Mongo follow the following syntax:
db.products.find({FILTER},{FIELDS_TO_SHOW})
Some examples:
db.products.find({},{name:1,brand:1}) -> SELECT name,brand FROM products
db.products.find({name:12}) -> SELECT * FROM products WHERE name=12
db.products.find({name:33},{name:1,brand:1}) -> SELECT name,brand FROM products WHERE name=33
db.products.find({price:{$gte:33}},{name:1,price:1}) -> SELECT name,price FROM products WHERE price >= 33
db.products.find({price:{$gte:33}, name:{12}},{name:1,price:1}) -> SELECT name,price FROM products WHERE price >= 33 AND name=12
db.products.find({},{name:1,brand:1}).sort({price:1}) -> Ordena por precio ascendente
db.products.find({},{name:1,brand:1}).sort({price:-1}) -> Ordena por precio descendente
NOTE: The order of the documents does NOT have to be the same in the DBs of the members of a replicaset. If we want the order to be the same, we must sort it.
Not all documents in a collection have to have the same fields. In the following query, we query the documents where a field exists:
db.products.find({price: {$exist: true}}) -> SELECT * FROM products WHERE price EXISTS
The use of cursors prevents the server from crashing when headless queries are made. Instead of serving a million results, only a few are served, and using the cursor, the next ones are requested. This way, the server does not explode due to an incorrect query.
var cursor = db.products.find()
cursor.next() -> Shows the obtained data one by one
Something really useful is Mongo’s help, for example:
db.products.find().help()
Writing db.COLLECTION.COMMAND can be a bit tedious, we can do the following:
t = db.sample
t.find()
NOTE: Mongo has a limit of 16MB per document, this is imposed to prevent a programmer from storing nonsense. Also, when the document is requested over the network, only the 16MB will travel.
On more than one occasion, we will need to import data from a json, it is as simple as: