MongoDB Learning Notes and Useful Commands
MongoDB is one of the typical NoSQL databases. It’s a document databased where data is stored in the form of json-type document. Understanding MongoDB is crucial for back-end development, particularly for agile development.
Useful Commands
A. Search Commands
- start mongosh
- show databases
- show collections
- switch to the database
- find all datas in the database
- delete collection
- insert data
- get the count of the data
- find data in page n ( skip((n-1)*pageSize)) (Mongodb can show data by pages)
- find data by AND conditions
- find data by OR conditions
- find the first data
- find data with age greater than 22
- find data with age greater than and equals to 22
mongosh
show dbs
show collections
use databaseName
db.collectionName.find()
db.collectionName.drop()
db.collectionName.insert({"username": "Tom", "age": 18})
db.collectionName.find().count()
db.collectionName.find().skip((n-1) * pageSize).limit(pageSize)
db.collectionName.find({"username": "Tom", "age": 18})
db.collectionName.find({$or:[{"username":"Tom", "username":Mary}]})
db.collectionName.findOne()
db.collectionName.find({age:{$gt:22}})
db.collectionName.find({age:{$gte:22}})
B. Update Commands
Notes: don’t forget $set when updating data.
- update name from “Tom” to “Jack”
- add sex in one specified data(name=Jack, age=18)
- update multi data at one time
- increase age by 1
db.collectionName.update({"name": "Tom"}, {$set:{"name": "Jack"}})
db.collectionName.update({"name":"Jack", "age": 18}, {$set:{"sex": "male"}})
db.collectionName.update({"age": 18}, {$set:{"sex": "female"}}, {multi:true})
db.collectionName.update({"name": "Tom"}, {inc:{age: 1}}, false, true)
C. Delete Commands
- delete one specified data
- delete multiple data (age > 18)
- delete just one data which satisfied the condition
db.collectionName.remove({"name": "Tom"}})
db.collectionName.remove({"age": {$gt: 18}})
db.collectionName.remove({"age": {$gt: 18}}, {justOne:true})
D. Advanced MongoDB
Setting an index can make the search speed faster.
- set an index
- get the indexes
- set multiple indexes
- set unique index (A unique index means that the value cannot be repeated.) ```zsh db.collectionName.ensureIndex({“username”: 1})
db.collectionName.getIndexes()
db.collectionName.dropIndex({“username”: 1})
db.collectionName.ensureIndex({“username”: 1}, {“age”: 1})
db.collectionName.ensureIndex({“age”: 1}, {“unique”: true})
#### E. The relationships of Databases
##### 1. One-to-one
Like National ID to student ID.
##### 2. One-to-many
Like order ID to order items. In one order, there can be many items.
##### 3. Many-to-many
Like products to users. One product can be collected by multiple users.
One user can collect multiple products.
In many-to-many situation, we ofter use intermediate table to mark the correspondence.
#### F. Aggregation Pipeline
Aggregation pipeline in MongoDB is used to transfer and group documents.
Usage: Table Association Query and Data Statistics
##### 1. $project
$project is used to edit the structure of the documents. It's useful to rename, add and delete the data.
```zsh
db.collectionName.aggregate([
{$project: {trade_no: 1, all_price: 1}}
])
2. $match
$match is used to filter the documents.
db.collectionName.aggregate([
{$project: {trade_no: 1, all_price: 1}},
{$match: {"all_price": {$gte: 90}}}
])
3. $group
$group is used to filter the documents.
In the following example, data is grouped by order_id and then sum up by the num.
db.collectionName.aggregate([
{$group: {$_id: "order_id", total: {$sum: "$num"}}},
])
4. $sort
$sort the documents in the collection. {$sort:{“all_price”: -1}}
db.collectionName.aggregate([
{$project: {trade_no: 1, all_price: 1}},
{$match: {"all_price": {$gte: 90}}},
{$sort:{"all_price": -1}}
])
4. $limit
$limit stands for only returning one data. Only showing one data.
db.collectionName.aggregate([
{$project: {trade_no: 1, all_price: 1}},
{$match: {"all_price": {$gte: 90}}},
{$sort:{"all_price": -1}},
{$limit: 1}
])
5. $skip
$skip stands for skip data. Skip one data:
db.collectionName.aggregate([
{$project: {trade_no: 1, all_price: 1}},
{$match: {"all_price": {$gte: 90}}},
{$sort:{"all_price": -1}},
{$skip: 1}
])
6. $lookup
$lookup can connect two tables together.
db.collectionName.aggregate([
{$lookup:{
from:"order_item",
localField:"order_id",
foreignField: "order_id",
as:"items"
}},
{$project: {trade_no: 1, all_price: 1}},
{$match: {"all_price": {$gte: 90}}},
{$sort:{"all_price": -1}},
{$skip: 1}
])