mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-06-08 02:37:13 +00:00
show line numbers in conde snippets
This commit is contained in:
parent
cd1df0e376
commit
255a68d673
82 changed files with 1249 additions and 1251 deletions
|
@ -25,7 +25,7 @@ MongoDB automatically creates an `ObjectId()` if it's not provided.
|
|||
To create a database is sufficient to switch towards a non existing one with `use <database>` (implicit creation).
|
||||
The database is not actually created until a document is inserted.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
show dbs # list all databases
|
||||
use <database> # use a particular database
|
||||
show collections # list all collection for the current database
|
||||
|
@ -38,7 +38,7 @@ db.<collection>.insertOne({document}) # implicit collection creation
|
|||
|
||||
## Operators (MQL Syntax)
|
||||
|
||||
```json
|
||||
```json linenums="1"
|
||||
/* --- Update operators --- */
|
||||
{ "$inc": { "<key>": "<increment>", ... } } // Increment value
|
||||
{ "$set": { "<key>": "<value>", ... } } // Set value
|
||||
|
@ -79,7 +79,7 @@ db.<collection>.insertOne({document}) # implicit collection creation
|
|||
|
||||
> **Note**: `$<key>` is used to access the value of the field dynamically
|
||||
|
||||
```json
|
||||
```json linenums="1"
|
||||
{ "$expr": { "<expression>" } } // aggregation expression, variables, conditional expressions
|
||||
{ "$expr": { "$<comparison_operator>": [ "$<key>", "$<key>" ] } } // compare field values (operators use aggregation syntax)
|
||||
```
|
||||
|
@ -95,7 +95,7 @@ Insertion results:
|
|||
- error -> rollback
|
||||
- success -> entire documents gets saved
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
# explicit collection creation, all options are optional
|
||||
db.createCollection( <name>,
|
||||
{
|
||||
|
@ -128,7 +128,7 @@ db.<collection>.insertMany([ { document }, { document } ] , { "ordered": false }
|
|||
|
||||
### Querying
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.findOne() # find only one document
|
||||
db.<collection>.find(filter) # show selected documents
|
||||
db.<collection>.find().pretty() # show documents formatted
|
||||
|
@ -172,7 +172,7 @@ db.<collection>.find().hint( { $natural : -1 } ) # force the query to perform a
|
|||
|
||||
[Update Operators](https://docs.mongodb.com/manual/reference/operator/update/ "Update Operators Documentation")
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.replaceOne(filter, update, options)
|
||||
db.<collection>.updateOne(filter, update, {upsert: true}) # modify document if existing, insert otherwise
|
||||
|
||||
|
@ -181,7 +181,7 @@ db.<collection>.updateOne(filter, { "$push": { ... }, "$set": { ... }, { "$inc":
|
|||
|
||||
### Deletion
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.deleteOne(filter, options)
|
||||
db.<collection>.deleteMany(filter, options)
|
||||
|
||||
|
@ -199,7 +199,7 @@ Utility to import all docs into a specified collection.
|
|||
If the collection already exists `--drop` deletes it before reuploading it.
|
||||
**WARNING**: CSV separators must be commas (`,`)
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
mongoimport <options> <connection-string> <file>
|
||||
|
||||
--uri=<connectionString>
|
||||
|
@ -218,7 +218,7 @@ mongoimport <options> <connection-string> <file>
|
|||
|
||||
Utility to export documents into a specified file.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
mongoexport --collection=<collection> <options> <connection-string>
|
||||
|
||||
--uri=<connectionString>
|
||||
|
@ -272,7 +272,7 @@ Indexes _slow down writing operations_ since the index must be updated at every
|
|||
|
||||
### Diagnosis and query planning
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.find({...}).explain() # explain won't accept other functions
|
||||
db.explain().<collection>.find({...}) # can accept other functions
|
||||
db.explain("executionStats").<collection>.find({...}) # more info
|
||||
|
@ -280,7 +280,7 @@ db.explain("executionStats").<collection>.find({...}) # more info
|
|||
|
||||
### Index Creation
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.createIndex( <key and index type specification>, <options> )
|
||||
|
||||
db.<collection>.createIndex( { "<key>": <type>, "<key>": <type>, ... } ) # normal, compound or multikey (field is array) index
|
||||
|
@ -302,7 +302,7 @@ db.<collection>.createIndex(
|
|||
|
||||
### [Index Management](https://docs.mongodb.com/manual/tutorial/manage-indexes/)
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
# view all db indexes
|
||||
db.getCollectionNames().forEach(function(collection) {
|
||||
indexes = db[collection].getIndexes();
|
||||
|
@ -339,7 +339,7 @@ handling connections, requests and persisting the data.
|
|||
|
||||
### Basic Shell Helpers
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<method>() # database interaction
|
||||
db.<collection>.<method>() # collection interaction
|
||||
rs.<method>(); # replica set deployment and management
|
||||
|
@ -378,7 +378,7 @@ Log Verbosity Level:
|
|||
- `0`: Default Verbosity (Information)
|
||||
- `1 - 5`: Increases the verbosity up to Debug messages
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.getLogComponents() # get components and their verbosity
|
||||
db.adminCommand({"getLog": "<scope>"}) # retrieve logs (getLog must be run on admin db -> adminCommand)
|
||||
db.setLogLevel(<level>, "<component>"); # set log level (output is OLD verbosity levels)
|
||||
|
@ -404,7 +404,7 @@ Events captured by the profiler:
|
|||
|
||||
> **Note**: Logs are saved in the `system.profile` _capped_ collection.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.setProfilingLevel(n) # set profiler level
|
||||
db.setProfilingLevel(1, { slowms: <ms> })
|
||||
db.getProfilingStatus() # check profiler status
|
||||
|
@ -454,7 +454,7 @@ Built-in Roles Groups and Names:
|
|||
- Backup/Restore: `backup`, `restore`
|
||||
- Super User: `root`
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.createUser(
|
||||
{
|
||||
user: "<username>",
|
||||
|
@ -509,7 +509,7 @@ Variable syntax in aggregations:
|
|||
|
||||
Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{ "$match": { "<query>" } },
|
||||
|
||||
|
@ -541,7 +541,7 @@ Passes along the documents with the requested fields to the next stage in the pi
|
|||
- [`$sum`][$sum_docs]
|
||||
- [`$avg`][$avg_docs]
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{
|
||||
"$project": {
|
||||
|
@ -598,7 +598,7 @@ db.<collection>.aggregate([
|
|||
Adds new fields to documents (can be result of computation).
|
||||
`$addFields` outputs documents that contain _all existing fields_ from the input documents and newly added fields.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate({
|
||||
{ $addFields: { <newField>: <expression>, ... } }
|
||||
})
|
||||
|
@ -610,7 +610,7 @@ db.<collection>.aggregate({
|
|||
|
||||
The $`group` stage separates documents into groups according to a "group key". The output is one document for each unique group key.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{
|
||||
"$group": {
|
||||
|
@ -629,7 +629,7 @@ db.<collection>.aggregate([
|
|||
Deconstructs an array field from the input documents to output a document for each element.
|
||||
Each output document is the input document with the value of the array field replaced by the element
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{ "$unwind": "<array-key>" }
|
||||
|
||||
|
@ -647,7 +647,7 @@ db.<collection>.aggregate([
|
|||
|
||||
### [`$count` Aggregation Stage][$count_docs]
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{ "$count": "<count-key>" }
|
||||
])
|
||||
|
@ -657,7 +657,7 @@ db.<collection>.aggregate([
|
|||
|
||||
### [`$sort` Aggregation Stage][$sort_docs]
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{
|
||||
"$sort": {
|
||||
|
@ -676,7 +676,7 @@ db.<collection>.aggregate([
|
|||
|
||||
### [`$skip` Aggregation Stage][$skip_docs]
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{ "$skip": "<positive 64-bit integer>" }
|
||||
])
|
||||
|
@ -686,7 +686,7 @@ db.<collection>.aggregate([
|
|||
|
||||
### [`$limit` Aggregation Stage][$limit_docs]
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{ "$limit": "<positive 64-bit integer>" }
|
||||
])
|
||||
|
@ -701,7 +701,7 @@ The `$lookup` stage adds a new array field to each input document. The new array
|
|||
|
||||
> **Note**: To combine elements from two different collections, use the [`$unionWith`][$unionWith_docs] pipeline stage.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{
|
||||
"$lookup": {
|
||||
|
@ -724,7 +724,7 @@ Performs a recursive search on a collection, with options for restricting the se
|
|||
|
||||
The collection on which the aggregation is performed and the `from` collection can be the same (in-collection search) or different (cross-collection search)
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{
|
||||
$graphLookup: {
|
||||
|
@ -754,7 +754,7 @@ Each output document contains two fields: an `_id` field containing the distinct
|
|||
|
||||
The documents are sorted by count in descending order.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
db.<collection>.aggregate([
|
||||
{ $sortByCount: <expression> }
|
||||
])
|
||||
|
|
|
@ -10,14 +10,14 @@ Often Redis it is called a *data structure* server because it has outer key-valu
|
|||
|
||||
### Server Startup
|
||||
|
||||
```bash
|
||||
```bash linenums="1"
|
||||
redis-server # start the server
|
||||
redis-cli
|
||||
```
|
||||
|
||||
### [Key-Value Pairs](https://redis.io/commands#generic)
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
SET <key> <value> [ EX <seconds> ] # store a key-value pair, TTL optional
|
||||
GET <key> # read a key content
|
||||
EXISTS <key> # check if a key exists
|
||||
|
@ -40,7 +40,7 @@ PERSIST <key> # make the key permanent
|
|||
|
||||
A list is a series of ordered values.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
RPUSH <key> <value1> <value2> ... # add one or more values to the end of the list
|
||||
LPUSH <key> <value1> <value2> ... # add one or more values to the start of a list
|
||||
|
||||
|
@ -55,7 +55,7 @@ RPOP # remove and return the last item fro the list
|
|||
|
||||
A set is similar to a list, except it does not have a specific order and each element may only appear once.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
SADD <key> <value1> <value2> ... # add one or more values to the set (return 0 if values are already inside)
|
||||
SREM <key> <value> # remove the given member from the set, return 1 or 0 to signal if the member was actually there or not.
|
||||
SPOP <key> <value> # remove and return value from the set
|
||||
|
@ -72,7 +72,7 @@ Sets are a very handy data type, but as they are unsorted they don't work well f
|
|||
|
||||
A sorted set is similar to a regular set, but now each value has an associated score. This score is used to sort the elements in the set.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
ZADD <key> <score> <value> # add a value with it's score
|
||||
|
||||
ZRANGE <key> <start_index> <end_index> # return a subset of the sortedSet
|
||||
|
@ -84,7 +84,7 @@ ZRANGE <key> <start_index> <end_index> # return a subset of the sortedSet
|
|||
|
||||
Hashes are maps between string fields and string values, so they are the perfect data type to represent objects.
|
||||
|
||||
```sh
|
||||
```sh linenums="1"
|
||||
HSET <key> <field> <value> [ <field> <value> ... ] # set the string of a hash field
|
||||
HSETNX <key> <field> <value> # set the value of a hash field, only if the field does not exist
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
## DDL
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
show databases; -- mostra database
|
||||
CREATE DATABASE <database>; -- database creation
|
||||
use <database_name>; -- usa un database particolare
|
||||
|
@ -16,7 +16,7 @@ show tables; -- mostra tabelle del database
|
|||
|
||||
### Table Creation
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
CREATE TABLE <table_name>
|
||||
(<field_name> <field_type> <option>,
|
||||
...);
|
||||
|
@ -24,7 +24,7 @@ CREATE TABLE <table_name>
|
|||
|
||||
### PRIMARY KEY from multiple fields
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
CREATE TABLE <table_name>(
|
||||
...,
|
||||
PRIMARY KEY (<field1>, ...),
|
||||
|
@ -33,7 +33,7 @@ CREATE TABLE <table_name>(
|
|||
|
||||
### Table Field Options
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
PRIMARY KEY -- marks primary key as field option
|
||||
NOT NULL -- marks a necessary field
|
||||
REFERENCES <table> (<field>) -- adds foreign key reference
|
||||
|
@ -43,7 +43,7 @@ UNIQUE (<field>) -- set field as unique (MySQL)
|
|||
|
||||
### Table Modification
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
ALTER TABLE <table>
|
||||
ADD PRIMARY KEY (<field>, ...), -- definition of PK after table creation
|
||||
ADD <field_name> <field_type> <option>; -- addition of a new field, field will have no value in the table
|
||||
|
@ -63,20 +63,20 @@ ALTER TABLE <table>
|
|||
|
||||
### Data Insertion
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
INSERT INTO <table> (field_1, ...) VALUES (value_1, ...), (value_1, ...);
|
||||
INSERT INTO <table> VALUES (value_1, ...), (value_1, ...); -- field order MUST respect tables's columns order
|
||||
```
|
||||
|
||||
### Data Update
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
UPDATE <table> SET <field> = <value>, <field> = <value>, ... WHERE <condition>;
|
||||
```
|
||||
|
||||
### Data Elimination
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
DELETE FROM <table> WHERE <condition>
|
||||
DELETE FROM <table> -- empty the table
|
||||
```
|
||||
|
@ -85,7 +85,7 @@ DELETE FROM <table> -- empty the table
|
|||
|
||||
`*`: denotes all table fields
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT * FROM <table>; -- show table contents
|
||||
SHOW columns FROM <table>; -- show table columns
|
||||
DESCRIBE <table>; -- shows table
|
||||
|
@ -93,13 +93,13 @@ DESCRIBE <table>; -- shows table
|
|||
|
||||
### Alias
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT <field> as <alias>; -- shows <field/funzione> with name <alias>
|
||||
```
|
||||
|
||||
### Conditional Selection
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT * FROM <table> WHERE <condition>; -- shows elements that satisfy the condition
|
||||
AND, OR, NOT -- logic connectors
|
||||
|
||||
|
@ -108,7 +108,7 @@ SELECT * FROM <table> WHERE <field> Between <value_1> AND <value_2>;
|
|||
|
||||
### Ordering
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT * FROM <table> ORDER BY <field>, ...; -- shows the table ordered by <field>
|
||||
SELECT * FROM <table> ORDER BY <field>, ... DESC; -- shows the table ordered by <field>, decreasing order
|
||||
SELECT * FROM <table> ORDER BY <field>, ... LIMIT n; -- shows the table ordered by <field>, shows n items
|
||||
|
@ -117,7 +117,7 @@ SELECT TOP(n) * FROM <table> ORDER BY <field>, ...; -- T-SQL
|
|||
|
||||
## Grouping
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT * FROM <table> GROUP BY <field>;
|
||||
SELECT * FROM <table> GROUP BY <field> HAVING <condition>;
|
||||
SELECT DISTINCT <field> FROM <table>; -- shows elements without repetitions
|
||||
|
@ -127,7 +127,7 @@ SELECT DISTINCT <field> FROM <table>; -- shows elements without repetitions
|
|||
|
||||
`%`: any number of characters
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT * FROM <table> WHERE <field> LIKE '<char>%'; -- selects items in <field> that start with <char>
|
||||
SELECT * FROM <table> WHERE <field> LIKE '%<char>'; -- selects items in <field> that end with <char>
|
||||
SELECT * FROM <table> WHERE <field> LIKE '%<char>%'; -- selects items in <field> that contain <char>
|
||||
|
@ -136,14 +136,14 @@ SELECT * FROM <table> WHERE <field> NOT LIKE '%<char>%'; -- selects items in
|
|||
|
||||
### Selection from multiple tables
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT a.<field>, b.<field> FROM <table> AS a, <table> AS b
|
||||
WHERE a.<field> ...;
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT COUNT(*) FROM <field>; -- count of items in <field>
|
||||
SELECT MIN(*) FROM <table>; -- min value
|
||||
SELECT MAX(*) FROM <table>; -- max value
|
||||
|
@ -154,7 +154,7 @@ ANY (SELECT ...)
|
|||
|
||||
## Nested Queries
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT * FROM <table> WHERE EXISTS (SELECT * FROM <table>) -- selected field existing in subquery
|
||||
SELECT * FROM <table> WHERE NOT EXISTS (SELECT * FROM <table>) -- selected field not existing in subquery
|
||||
```
|
||||
|
@ -163,7 +163,7 @@ SELECT * FROM <table> WHERE NOT EXISTS (SELECT * FROM <table>) -- selected fiel
|
|||
|
||||
Create new table with necessary fields:
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
CREATE TABLE <table> (
|
||||
(<field_name> <field_type> <option>,
|
||||
...);
|
||||
|
@ -172,14 +172,14 @@ CREATE TABLE <table> (
|
|||
|
||||
Fill fields with data from table:
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
INSERT INTO <table>
|
||||
SELECT <fields> FROM <TABLE> WHERE <condition>;
|
||||
```
|
||||
|
||||
## Join
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT * FROM <table1> JOIN <table2> ON <table1>.<field> = <table2>.<field>;
|
||||
SELECT * FROM <table1> LEFT JOIN <table2> ON <condition>;
|
||||
SELECT * FROM <table1> RIGHT JOIN <table2> ON <condition>
|
||||
|
@ -189,7 +189,7 @@ SELECT * FROM <table1> RIGHT JOIN <table2> ON <condition>
|
|||
|
||||
## Multiple Join
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
SELECT * FROM <table1>
|
||||
JOIN <table2> ON <table1>.<field> = <table2>.<field>
|
||||
JOIN <table3> ON <table2>.<field> = <table3>.<field>;
|
||||
|
@ -203,7 +203,7 @@ JOIN <table3> ON <table2>.<field> = <table3>.<field>;
|
|||
|
||||
### T-SQL Insert From table
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
USE [<db_name>]
|
||||
|
||||
SET IDENTITY_INSERT [<destination_table>] ON
|
||||
|
@ -217,7 +217,7 @@ SET IDENTITY_INSERT [<destination_table>] OFF
|
|||
|
||||
### T-SQL Parametric Query
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
-- variable declaration
|
||||
DECLARE @var_name <type>
|
||||
|
||||
|
@ -238,7 +238,7 @@ GO
|
|||
|
||||
A view represents a virtual table. Join multiple tables in a view and use the view to present the data as if the data were coming from a single table.
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
CREATE VIEW <name> AS
|
||||
SELECT * FROM <table> ...
|
||||
```
|
||||
|
@ -250,7 +250,7 @@ SELECT * FROM <table> ...
|
|||
|
||||
Stored Procedure Definition:
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
CREATE PROCEDURE <Procedure_Name>
|
||||
-- Add the parameters for the stored procedure here
|
||||
<@Param1> <Datatype_For_Param1> = <Default_Value_For_Param1>,
|
||||
|
@ -268,7 +268,7 @@ GO
|
|||
|
||||
Stored Procedure call in query:
|
||||
|
||||
```sql
|
||||
```sql linenums="1"
|
||||
USE <database>
|
||||
GO
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue