Professional Documents
Culture Documents
Chapter3a - QuanLy Database Và Collection Trong MongoDB
Chapter3a - QuanLy Database Và Collection Trong MongoDB
1. Database
a. Create Database
• Chạy file mongo.exe để mở MongoDB shell:
"C:\Program Files\MongoDB\Server\4.1\bin\mongo.exe"
1
8/19/2020
1. Database
1. Database
2
8/19/2020
1. Database
1. Database
3
8/19/2020
1. Database
1. Database
4
8/19/2020
1. Database
10
1. Database
11
5
8/19/2020
1. Database
12
1. Database
13
6
8/19/2020
1. Database
14
1. Database
15
7
8/19/2020
1. Database
b. Drop Database
• Drop a existing database:
> db.dropDatabase()
o Ví dụ: Tạo db mới tên là db_test3 và insert document vào đó
16
1. Database
17
8
8/19/2020
1. Database
18
1. Database
19
9
8/19/2020
2. Collection
a. Create Collection
• Cú pháp cơ bản để tạo một collection:
> db.createCollection(name, options)
o name: tên gọi của collection muốn tạo
o Options: a document và được dùng để xác định cấu hình
(configuration) của collection.
2. Collection
a. Create Collection
• Danh sách các tùy chọn (options):
Field Type Description (Optional)
True enable một collection giới hạn (một collection kích
thước cố định, tự động ghi đè các mục cũ nhất của nó khi đạt
capped Boolean
đến kích thước tối đa)
=> cần xác định tham số kích thước (size) nếu chọn true
True tự động tạo index trên trường _id
autoIndexId Boolean
Mặc định là false. (Không còn được dùng từ version 4.1)
Chỉ định kích thước tối đa ở dạng bytes cho một capped
size number collection.
Nếu capped là true cần chỉ định giá trị cho trường này
21
max number Chỉ định số lượng document tối đa trong capped collection
10
8/19/2020
2. Collection
a. Create Collection
o Ví dụ, tạo collection bằng cú pháp basic, không lựa chọn các
options.
22
2. Collection
a. Create Collection
• Cú pháp kiểm tra các collection đã tạo trong db hiện
hành:
> show collections
23
11
8/19/2020
2. Collection
a. Create Collection
o Ví dụ, tạo collection bằng cú pháp basic có chỉ định options
24
2. Collection
a. Create Collection
• Note: Mongo cho phép insert document mà không cần
tạo collection trước.
25
12
8/19/2020
2. Collection
a. Create Collection
26
2. Collection
b. Drop Collection
• Cú pháp xóa collection khỏi database:
> db.Collection_name.drop()
o Ví dụ, xóa collection tên là Collection4 khỏi database
db_MongoCourse
27
13
8/19/2020
2. Collection
b. Drop Collection
• Note: phương thức drop() trả về true nếu xóa thành
công, ngược lại thì trả về false
28
2. Collection
b. Drop Collection
• Cú pháp xem các documents của collection:
> db.Collection_name.find()
o Ví dụ, xem các documents của collection5 trong database
db_MongoCourse
29
14
8/19/2020
2. Collection
b. Drop Collection
• Hiển thị các documents của collection ở dạng chuỗi
JSON
> db.Collection_name.find().pretty()
30
2. Collection
31 GO
15
8/19/2020
2. Collection
32
2. Collection
33
16
8/19/2020
2. Collection
34
2. Collection
35
17
8/19/2020
2. Collection
36
2. Collection
18
8/19/2020
38
39
19
8/19/2020
cd "c:\program files\mongodb\server\4.1\bin\”
c:\program files\mongodb\server\4.1\bin>dir
40
41
20
8/19/2020
43
21
8/19/2020
cd c:\data\
c:\data>dir
44
45
22
8/19/2020
46
47
23
8/19/2020
48
49
24
8/19/2020
51
25
8/19/2020
52
53
26
8/19/2020
3a. Collection
• Practice 5a: Viết lệnh thực hiện các yêu cầu sau
• Export dữ liệu từ Collection6 của db_MongoCourse ra file
JSON
• File được lưu ở thư mục data\temp ngoài ổ C
• Practice 5b: Viết lệnh thực hiện các yêu cầu sau
• Export dữ liệu từ collection Users của MDB_Course2 (practice
3) ra file csv
• Các trường cần export là tên và mật khẩu
• File được lưu ở thư mục data\temp ngoài ổ C
54 BT 6
55
27
8/19/2020
cd "c:\program files\mongodb\server\4.1\bin\”
c:\program files\mongodb\server\4.1\bin>dir
56
57
28
8/19/2020
58
59
29
8/19/2020
>show collections
60
cd "c:\program files\mongodb\server\4.1\bin\”
61
30
8/19/2020
62
63
31
8/19/2020
64
3b. Collection
• Practice 6a: Viết lệnh thực hiện các yêu cầu sau
• Import dữ liệu từ file Collection6.json thu được ở Practice 5a
vào db_MongoCourse.
• Collection được import dữ liệu tên là Collection6
• Practice 6b: Viết lệnh thực hiện các yêu cầu sau
• Import dữ liệu từ file Collection7.csv thu được ở Practice 5b
vào db_MongoCourse.
• Collection được import dữ liệu tên là Collection7
65
32
8/19/2020
1. Hiển thị danh sách các database đang có để lựa chọn database
muốn export
show dbs
66
67
33
8/19/2020
68
69
34
8/19/2020
70
71
35
8/19/2020
72
73
36
8/19/2020
74
75
37
8/19/2020
76
38
8/19/2020
78
Summary
• Database:
39
8/19/2020
Summary
• Collection
80
Summary
• Document
>db.collection.insert() :: insert document(s) into
collectionName.
>db.collection.insertOne() :: insert 1 document into
collectionName.
>db.collection.insertMany([...]) :: insert many documents
into collectionName.
>db.collection.save(variableName) :: save variable value
into collectionName
81
40
8/19/2020
Summary
• Export Collection
>mongoexport –d database –c collection –o path\fileName
export collection to JSON string
82
Summary
• Import Collection
>mongoimport –d database –c collection --file path\fileName
import collection from JSON string
83
41
8/19/2020
Summary
• Export Database
>mongodump –d database –o path
export database to path
>mongodump –d database –o path --gzip
export database to zip folder
>mongorestore -d db_name --gzip --archive=filename.gz
export database to zip file
84
Summary
• Import Database
>mongorestore –d database path
Import database from path
>mongorestore –d database path --gzip
Import database from zip folder
>mongorestore –d database --gzip --archive=filename.gz
Import database from zip file
85
42
8/19/2020
Summary
• Practice 7a: Viết lệnh thực hiện các yêu cầu sau
• Tạo database tên là 06_DHCNTTX (X là số hiệu của lớp)
• Tạo collection tên là Members để lưu thông tin cá nhân (Stud_Id,
fullname, skills, favourite)
• Thêm các bản ghi của nhóm mình (3-5 người) vào collection trên
• Export Members ra json string và csv file
• Export 06_DHCNTTX ra 1 file zip
• Practice 7b: Viết lệnh thực hiện các yêu cầu sau:
• Import Members từ json string hoặc csv file của nhóm khác
• Import 06_DHCNTTX từ file zip của nhóm khác
86
5. Datatypes
• Một vài kiểu dữ liệu lưu trữ dữ liệu thường dùng trong MongoDB:
Type Number Alias Notes Descriptions
Double 1 “double” Lưu trữ các giá trị số thực dấu phẩy động
Kiểu dữ liệu phổ biến nhất để lưu trữ
String 2 “string”
data. Định dạng phải là UTF-8
Object 3 “object” Sử dụng cho các embedded documents.
Lưu trữ các mảng hoặc danh sách hoặc
Array 4 “array”
nhiều giá trị vào trong một key.
43
8/19/2020
5. Datatypes
• Một vài kiểu dữ liệu lưu trữ dữ liệu thường dùng trong MongoDB:
Type Number Alias Notes Descriptions
Boolean 8 “bool” Lưu trữ một giá trị Boolean (true/false)
lưu trữ date và time hiện tại trong định
Date 9 “date”
dạng UNIX time.
Null 10 “null” Lưu một giá trị Null.
Regular
11 “regex” Lưu trữ biểu thức
Expression
5. Datatypes
• Một vài kiểu dữ liệu lưu trữ dữ liệu thường dùng trong MongoDB:
Type Number Alias Notes Descriptions
Sử dụng để so sánh một giá trị với các
Min key -1 “minKey”
phần tử BSON thấp nhất
Sử dụng để so sánh một giá trị với các
Max key 127 “maxKey” phần tử BSON cao nhất.
89
44
8/19/2020
$type
• $type chọn các tài liệu trong đó giá trị của trường là một
thể hiện của loại BSON (BSON type) được chỉ định.
• Truy vấn theo data type rất hữu ích khi xử lý dữ liệu phi
cấu trúc cấp cao trong đó các loại dữ liệu không thể dự
đoán được.
• Cú pháp:
{ field: { $type: <BSON type> } }
• hoặc:
{ field: { $type: [ <BSON type1> , <BSON type2>, ... ] } }
$type
• Available Types
Double 1 “double”
String 2 “string”
Object 3 “object”
Array 4 “array”
Binary data 5 “binData”
Undefined 6 “undefined” Deprecated.
ObjectId 7 “objectId”
91
Boolean 8 “bool”
45
8/19/2020
$type
Type Number Alias Notes
Date 9 “date”
Null 10 “null”
Regular
11 “regex”
Expression
$type
Type Number Alias Notes
Timestamp 17 “timestamp”
93
46
8/19/2020
$type
Example Results
{ $type: "a" } "string"
{ $type: /a/ } "regex"
{ $type: 1 } "double"
{ $type: NumberLong(627) } "long"
{ $type: { x: 1 } } "object"
{ $type: [ [ 1, 2, 3 ] ] } "array"
94
• Truy vấn cho minKey hay maxKey với $type sẽ chỉ trả
về các trường (fields) mà khớp với giá trị của MinKey
hoặc MaxKey
95
47
8/19/2020
96
48
8/19/2020
98
99
49
8/19/2020
100
db.grades.insertMany(
[
{ "_id" : 1, name : "Alice King" , classAverage : 87.333333333333333 },
{ "_id" : 2, name : "Bob Jenkins", classAverage : "83.52" },
{ "_id" : 3, name : "Cathy Hart", classAverage: "94.06" },
{ "_id" : 4, name : "Drew Williams" , classAverage : 93 }
]
)
101
50
8/19/2020
102
51
8/19/2020
52
8/19/2020
53
8/19/2020
db.restaurants.find(
{ "grades.grade" : { $type : "minKey" } }
)
108
54
8/19/2020
db.restaurants.find(
{ "grades.grade" : { $type : "maxKey" } }
)
110
55
8/19/2020
{
"_id": 1, Querying by Array Type
"readings": [25, 23, [ "Warn: High Temp!", 55 ],
[ "ERROR: SYSTEM
• SensorReading SHUTDOWN!",
collection 66 ] ]
chứa các documents sau:
},
{
"_id": 2, "readings": [ 25, 25, 24, 23 ]
},
{
"_id": 3, "readings": [ 22, 24, [] ]
},
{
"_id": 4, "readings": []
},
{
"_id": 5, "readings": 24
112
}
113
56
8/19/2020
{
"_id": 1, Querying by Multiple Data Type
"readings": [ 25, 23, [ "Warn: High Temp!", 55 ],
[ "ERROR: SYSTEM SHUTDOWN!", 66 ] ]
},
{
"_id": 2,
"readings": [ 25, 25, 24, 23 ]
},
{
"_id": 3,
"readings": [ 22, 24, [] ]
},
{
"_id": 4, "readings": []
}
114
Summary
57
8/19/2020
DEMO
116
58