-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_schemas.js
More file actions
89 lines (81 loc) · 2.4 KB
/
1_schemas.js
File metadata and controls
89 lines (81 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// This file contains example schemas that will be used throughout the tutorial
// Note: MongoDB is schema-less by default, but defining schemas helps in maintaining data consistency
// User Schema
const userSchema = {
// _id will be automatically added by MongoDB
username: String, // Required, unique
email: String, // Required, unique
age: Number, // Optional
created_at: Date, // Automatically set
address: { // Nested document example
street: String,
city: String,
country: String
},
interests: [String] // Array example
};
// Product Schema (for e-commerce example)
const productSchema = {
name: String, // Required
price: Number, // Required
description: String,
category: String,
in_stock: Boolean,
tags: [String],
specifications: Object // Flexible object for various product specs
};
// Order Schema (showing relationships)
const orderSchema = {
user_id: 'ObjectId', // Reference to User collection
products: [{ // Array of embedded documents
product_id: 'ObjectId', // Reference to Product collection
quantity: Number,
price: Number
}],
total_amount: Number,
status: String, // e.g., 'pending', 'completed', 'cancelled'
order_date: Date,
shipping_address: Object
};
// Comments Schema (for demonstrating nested relationships and arrays)
const commentSchema = {
user_id: 'ObjectId',
content: String,
post_id: 'ObjectId',
likes: Number,
replies: [{
user_id: 'ObjectId',
content: String,
created_at: Date
}],
created_at: Date
};
// Export schemas for reference in other files
module.exports = {
userSchema,
productSchema,
orderSchema,
commentSchema
};
/* Schema Usage Notes:
1. These schemas are for reference only - MongoDB doesn't enforce them by default
2. For schema validation, you can use:
- MongoDB Schema Validation
- Mongoose (ODM for MongoDB)
- Application-level validation
3. MongoDB data types include:
- String
- Number
- Date
- Boolean
- ObjectId
- Array
- Object (for nested documents)
- Binary Data
- Null
4. Best Practices:
- Use appropriate data types
- Consider indexing requirements
- Plan for data growth
- Choose between embedding and referencing based on use case
*/