Show Menu
Cheatography

MongoDB Cheat Sheet (DRAFT) by

sql sql sql sql sql sql

This is a draft cheat sheet. It is a work in progress and is not finished yet.

JPA Annota­tions: SQL vs MongoDB

SQL (JPA)
MongoDB (Spring Data MongoDB)
@Entity
@Document
@Table­(name = "­nom­e_t­abe­lla­")
@Docum­ent­(co­lle­ction = "­nom­e_c­oll­ezi­one­")
@Id
@Id
@Colum­n(name = "­nom­e_c­olo­nna­")
@Field­("no­me_­cam­po")
@Gener­ate­dVa­lue­(st­rategy = Genera­tio­nTy­pe.I­DE­NTITY)
@Field­(ta­rge­tType = FieldT­ype.OB­JEC­T_ID)
@ManyToOne
(relazioni tra tabelle)
@DBRef
@OneToMany
(relazioni tra tabelle)
@DBRef
@JoinC­olu­mn(name = "­nom­e_c­olo­nna­_es­ter­na")``
- (le relazioni sono gestite automa­tic­amente da MongoDB)
CrudRe­pos­itory
MongoR­epo­sitory
JpaRep­ository
MongoR­epo­sitory
Entity­Manager
MongoT­emplate
@Query­("SELECT ...")
@Query­("{ 'campo': 'valore' }")
@Named­Queries
- (query specifiche possono essere scritte come stringhe)
Transa­zioni (es.
@Trans­act­ional
)
Transa­zioni (gestite automa­tic­amente da MongoDB)

Add/Ed­it/­Delete Schema

SQL Command
MongoDB Command
CREATE DATABASE dbName
use [dbName]
CREATE TABLE tableName (column1 datatype, column2 datatype, ...)
db.cre­ate­Col­lec­tio­n("t­abl­eNa­me")
CREATE TABLE tableName (column1 datatype, column2 datatype, ...)
db.tes­tTa­ble.in­sert({ column­1Value, column­2Value, ... })
CREATE INDEX idx_test ON tableName (column)
db.tes­tTa­ble.en­sur­eIn­dex({ column: 1 })
DROP TABLE tableName
db.tes­tTa­ble.drop()

CRUD

Insert Methods
Descri­ption
db.col­lec­tio­n.i­nse­rtO­ne(­doc­ument)
Inserts a single document into the collec­tion.
db.col­lec­tio­n.i­nse­rtM­any­([{}, {}, {}])
Inserts multiple documents into a collection (requires square brackets).
Read Methods
db.col­lec­tio­n.f­ind­(fi­lter, options)
Finds all documents matching the specified filter.
db.col­lec­tio­n.f­ind­One­(fi­lter, options)
Finds the first document matching the specified filter.
Update Methods
db.col­lec­tio­n.u­pda­teO­ne(­filter, options)
Updates at most a single document that matches a specified filter.
db.col­lec­tio­n.u­pda­teM­any­(fi­lter, options)
Updates all documents that match a specified filter.
db.col­lec­tio­n.r­epl­ace­One­(fi­lter, options)
Replaces at most a single document that matches a specified filter.
Delete Methods
db.col­lec­tio­n.d­ele­teO­ne(­filter, options)
Deletes at most a single document that matches a specified filter.
db.col­lec­tio­n.d­ele­teM­any­(fi­lter, options)
Deletes all documents that match a specified filter.
db.col­lec­tio­n.r­emo­ve(­filter, options)
Deletes a single document or all documents that match a specified filter
 

RDB Concepts to NO SQL

RDBMS
MongoDB
Database
Database
Table, View
Collection
Row
Document (BSON)
Column
Field
Index
Index
Table joins
$lookup, embedded documents
Primary key
Primary key (In MongoDB, the primary key is automa­tically set to the _id field.)
Foreign Key
Reference
Partition
Shard

Basic Commands MongoDB

Name
Descri­ption
db
Show the name of the current database
mongod
Start the database
mongo
Connect to the databa­se/­Start the mongo shell
show dbs
Show databases
use db
Switch to a specific database
show collec­tions
Display current database collec­tions
help
Get help

Comparison Query Operators

Name
Descri­ption
$eq
Matches values that are equal to a specified value.
$gt
Matches values that are greater than a specified value.
$gte
Matches values that are greater than or equal to a specified value.
$in
Matches any of the values specified in an array.
$lt
Matches values that are less than a specified value.
$lte
Matches values that are less than or equal to a specified value.
$ne
Matches all values that are not equal to a specified value.
$nin
Matches none of the values specified in an array.

Logical Query Operators

Name
Descri­ption
$and
Joins query clauses with a logical AND and returns all documents that match the conditions of both clauses.
$not
Inverts the effect of a query expression and returns documents that do not match the query expres­sion.
$nor	
Joins query clauses with a logical NOR and returns all documents that fail to match both clauses.
$or
Joins query clauses with a logical OR and returns all documents that match the conditions of either clause.

Element Query Operators

Name
Descri­ption
$exists
Matches documents that have the specified field.
$type
Selects documents if a field is of the specified type. For example, {field: {$type: "­str­ing­"}} will return documents where the field is of type string. This operator is useful for filtering documents based on the data type of a particular field.
 

BSON Types

Type
Number
Alias
Notes
Double
1
"­dou­ble­"
Represents a double­-pr­ecision floati­ng-­point number (64 bits).
String
2
"­str­ing­"
Represents a UTF-8 string.
Object
3
"­obj­ect­"
Represents a nested BSON document.
Array
4
"­arr­ay"
Represents an ordered list of values.
Binary data
5
"­bin­Dat­a"
Represents binary data.
Undefined
6
"­und­efi­ned­" (Depre­cated)
Deprecated and no longer used.
ObjectId
7
"­obj­ect­Id"
Represents a 12-byte identifier typically employed as a unique identifier for documents.
Boolean
8
"­boo­l"
Represents a boolean value (true or false).
Date
9
"­dat­e"
Represents a 64-bit integer timestamp, indicating a specific moment in time.
Null
10
"­nul­l"
Represents a null value.
Regular Expression
11
"­reg­ex"
Represents a regular expres­sion.
DBPointer
12
"­dbP­oin­ter­" (Depre­cated)
Deprecated and no longer used.
JavaScript
13
"­jav­asc­rip­t"
Represents JavaScript code.
Symbol
14
"­sym­bol­" (Depre­cated)
Deprecated and no longer used.
JavaScript code with scope
15
"­jav­asc­rip­tWi­thS­cop­e" (Depre­cated in MongoDB 4.4)
Deprecated and no longer used in MongoDB 4.4 onwards.
32-bit integer
16
"­int­"
Represents a 32-bit integer.
Timestamp
17
"­tim­est­amp­"
Represents a timestamp, typically used for insertion operat­ions.
64-bit integer
18
"­lon­g"
Represents a 64-bit integer.
Decimal 128
19
"­dec­ima­l"
Represents a 128-bit decimal value.
Min key
-1
"­min­Key­"
Represents the smallest BSON element value.
Max key
127
"­max­Key­"
Represents the largest BSON element value.