Add/Edit/Delete Schema
SQL Command |
MongoDB Command |
CREATE DATABASE dbName |
use [dbName] |
CREATE TABLE tableName (column1 datatype, column2 datatype, ...) |
db.createCollection("tableName") |
CREATE TABLE tableName (column1 datatype, column2 datatype, ...) |
db.testTable.insert({ column1Value, column2Value, ... }) |
CREATE INDEX idx_test ON tableName (column) |
db.testTable.ensureIndex({ column: 1 }) |
DROP TABLE tableName |
db.testTable.drop() |
CRUD
Insert Methods |
Description |
db.collection.insertOne(document)
|
Inserts a single document into the collection. |
db.collection.insertMany([{}, {}, {}])
|
Inserts multiple documents into a collection (requires square brackets). |
Read Methods |
db.collection.find(filter, options)
|
Finds all documents matching the specified filter. |
db.collection.findOne(filter, options)
|
Finds the first document matching the specified filter. |
Update Methods |
db.collection.updateOne(filter, options)
|
Updates at most a single document that matches a specified filter. |
db.collection.updateMany(filter, options)
|
Updates all documents that match a specified filter. |
db.collection.replaceOne(filter, options)
|
Replaces at most a single document that matches a specified filter. |
Delete Methods |
db.collection.deleteOne(filter, options)
|
Deletes at most a single document that matches a specified filter. |
db.collection.deleteMany(filter, options)
|
Deletes all documents that match a specified filter. |
db.collection.remove(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 automatically set to the _id field.) |
Foreign Key |
Reference |
Partition |
Shard |
Basic Commands MongoDB
Name |
Description |
|
Show the name of the current database |
|
Start the database |
|
Connect to the database/Start the mongo shell |
|
Show databases |
|
Switch to a specific database |
|
Display current database collections |
|
Get help |
Comparison Query Operators
Name |
Description |
|
Matches values that are equal to a specified value. |
|
Matches values that are greater than a specified value. |
|
Matches values that are greater than or equal to a specified value. |
|
Matches any of the values specified in an array. |
|
Matches values that are less than a specified value. |
|
Matches values that are less than or equal to a specified value. |
|
Matches all values that are not equal to a specified value. |
|
Matches none of the values specified in an array. |
Logical Query Operators
Name |
Description |
|
Joins query clauses with a logical AND and returns all documents that match the conditions of both clauses. |
|
Inverts the effect of a query expression and returns documents that do not match the query expression. |
|
Joins query clauses with a logical NOR and returns all documents that fail to match both clauses. |
|
Joins query clauses with a logical OR and returns all documents that match the conditions of either clause. |
Element Query Operators
Name |
Description |
|
Matches documents that have the specified field. |
|
Selects documents if a field is of the specified type. For example, {field: {$type: "string"}} 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 |
"double" |
Represents a double-precision floating-point number (64 bits). |
String |
2 |
"string" |
Represents a UTF-8 string. |
Object |
3 |
"object" |
Represents a nested BSON document. |
Array |
4 |
"array" |
Represents an ordered list of values. |
Binary data |
5 |
"binData" |
Represents binary data. |
Undefined |
6 |
"undefined" (Deprecated) |
Deprecated and no longer used. |
ObjectId |
7 |
"objectId" |
Represents a 12-byte identifier typically employed as a unique identifier for documents. |
Boolean |
8 |
"bool" |
Represents a boolean value (true or false). |
Date |
9 |
"date" |
Represents a 64-bit integer timestamp, indicating a specific moment in time. |
Null |
10 |
"null" |
Represents a null value. |
Regular Expression |
11 |
"regex" |
Represents a regular expression. |
DBPointer |
12 |
"dbPointer" (Deprecated) |
Deprecated and no longer used. |
JavaScript |
13 |
"javascript" |
Represents JavaScript code. |
Symbol |
14 |
"symbol" (Deprecated) |
Deprecated and no longer used. |
JavaScript code with scope |
15 |
"javascriptWithScope" (Deprecated 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 |
"timestamp" |
Represents a timestamp, typically used for insertion operations. |
64-bit integer |
18 |
"long" |
Represents a 64-bit integer. |
Decimal 128 |
19 |
"decimal" |
Represents a 128-bit decimal value. |
Min key |
-1 |
"minKey" |
Represents the smallest BSON element value. |
Max key |
127 |
"maxKey" |
Represents the largest BSON element value. |
|