Professional Documents
Culture Documents
02 - Creating and Manipulating Database
02 - Creating and Manipulating Database
2
Learning objectives
3
Contents
1. Create databases
2. Create collections
3. JSON document structure
4. Document validation
5. Import – Export databases
4
1. Create databases
Using the MongoDB Compass
▪ Example:
6
1. Create databases
Using the MongoDB Atlas UI
7
Contents
1. Create databases
2. Create collections
3. JSON document structure
4. Document validation
5. Import – Export databases
8
2. Create collections
Explicit creation
9
2. Create collections
Implicit creation
10
Contents
1. Create databases
2. Create collections
3. JSON document structure
4. Document validation
5. Import – Export databases
11
3. JSON document structure
Name Value
▪ JSON format:
o Start and end with { } {
"id" : 14,
o Separate each field and value with colon :
"firstName" : "Mario",
o Separate each field : value pair with comma , "lastName" : "Rossi",
o Field must be surrounded by quotation mark " " "active" : true
}
12
3. JSON document structure
JSON values
▪ Numbers: no quotes
▪ String: in double quotes
▪ Boolean: true, false
▪ Null: null
▪ Nested JSON object: It is a collection of field-value pairs and
always separated by a comma and enclosed in curly brackets
▪ Array: It is an ordered sequence of values separated
13
3. JSON document structure
Example
Nested JSON
object
JSON Array
14
3. JSON document structure
Example
15
3. JSON document structure
Pros – Cons of JSON
Friendly Text–based
Readable Space-Consuming
1. Create databases
2. Create collections
3. JSON document structure
4. Document validation
5. Import – Export databases
17
3. Document validation
18
3. Document validation
19
3. Document validation
20
Contents
1. Create databases
2. Create collections
3. JSON document structure
4. Document validation
5. Import – Export databases
21
4. Import – Export databases
How does MongoDB store data?
}
{
JSON BSON
Can be natively MongoDB stores
stored and data in BSON,
retrieved in internally and
MongoDB over the network
22
4. Import – Export databases
How does MongoDB store data?
23
4. Import – Export databases
How does MongoDB store data?
▪ Optimized for:
o Speed
o Space
o Flexibility to achieve high performance
24
4. Import – Export databases
{ }
Export to a Import from a
different system local machine
JSON BSON
mongoexport mongodump
mongoimport mongorestore
26
4. Import – Export databases
▪ <options>:
o -d/--database: database to use
o -c/--collection: collection to use
o -u/--username: username for authentication
o -p/--password: password for authentication
o -o/--out: output directory
o -h/--host: mongodb host to connect
o -p/--port: server port (can also use –h hostname:port)
o …
27
4. Import – Export databases
Export: BSON
2. From Atlas:
mongodump --uri
mongodb+srv://mongobasic:pass@cluster0.msr5i.mongodb.net/sample_restaurants
-c restaurants –o backup
creates a dump file that contains only the collection named restaurants of sample_restaurants
database
28
4. Import – Export databases
Import: BSON
2. From Atlas:
mongorestore --uri mongodb+srv://mongobasic:pass@cluster0.msr5i.mongodb.net
-d backup -c restaurants backup/dbtest/restaurants.bson
restore from a backup/dbtest/ directory to MongoDB Atlas Cluster.
29
4. Import – Export databases
Export: JSON
2. From Atlas:
mongoimport --uri
mongodb+srv://mongobasic:pass@cluster0.msr5i.mongodb.net/backup
--drop -c restaurants res.json
Import from the res.json file to MongoDB Atlas Cluster. 31
Question?
32