You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/60-schema-validation/2-validate-users.mdx
+112-4
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,10 @@
1
+
importTabsfrom'@theme/Tabs';
2
+
importTabItemfrom'@theme/TabItem';
3
+
1
4
# 👐 Enable Validation for the Users Collection
2
5
3
6
In this exercise, you will explore the pre-written JSON validation schema for the `users` collection, run a script to apply it to the collection, and test the schema validation by inserting a document that does not match the schema.
4
7
5
-
:::info
6
-
Note for Java users: schema validation rules are typically applied via a Javascript script! Although there's nothing stopping us to write this code in Java, this is usually a DB Admin task.
7
-
:::
8
-
9
8
## Database user permissions
10
9
11
10
To update the validator for any database collection, your database user must have admin privileges. Follow these steps to ensure your user has the correct permissions:
@@ -22,8 +21,20 @@ Atlas will deploy the change in a few seconds.
22
21
23
22
## Explore the JSON schema
24
23
24
+
<TabsgroupId="server">
25
+
<TabItemvalue="node"label="🚀 NodeJS/Express">
26
+
25
27
The JSON schema for the `users` collection is stored in the `server/src/schema-validation/apply-schema.ts`[file](https://github.com/mongodb-developer/library-management-system/blob/main/server/src/schema-validation/apply-schema.ts). Open the file in your GitHub codespace and examine the schema.
26
28
29
+
</TabItem>
30
+
31
+
<TabItemvalue="java"label="☕️ Java Spring Boot">
32
+
33
+
JSON schema for the `users` collection:
34
+
35
+
</TabItem>
36
+
</Tabs>
37
+
27
38
```ts
28
39
const userSchema = {
29
40
bsonType: 'object',
@@ -49,6 +60,10 @@ The schema defines the following constraints:
49
60
50
61
## Explore the script to apply the schema
51
62
63
+
64
+
<TabsgroupId="server">
65
+
<TabItemvalue="node"label="🚀 NodeJS/Express">
66
+
52
67
Examine the lines immediately following the schema definition in the `apply-schema.ts` file. You will see a function that applies the schema to the `users` collection.
53
68
54
69
```js
@@ -73,8 +88,51 @@ The function uses the `db.command()` method to apply the schema to the `users` c
73
88
- If you set it to `error`, MongoDB rejects any insert or update that violates the validation criteria.
74
89
- If you set it to `warn`, the operation proceeds, but the violation is recorded in the MongoDB log.
.append("description", "must be a boolean and is required"))
108
+
)
109
+
);
110
+
111
+
Document command =newDocument("collMod", "users")
112
+
.append("validator", userSchema)
113
+
.append("validationLevel", "strict")
114
+
.append("validationAction", "error");
115
+
116
+
Document result = database.runCommand(command);
117
+
118
+
if (result.getDouble("ok") !=1.0) {
119
+
System.err.println("Failed to enable schema validation!");
120
+
System.exit(1);
121
+
} else {
122
+
System.out.println("Schema validation enabled!");
123
+
}
124
+
}
125
+
}
126
+
```
127
+
128
+
</TabItem>
129
+
</Tabs>
130
+
76
131
## Apply the schema to the `users` collection
77
132
133
+
<TabsgroupId="server">
134
+
<TabItemvalue="node"label="🚀 NodeJS/Express">
135
+
78
136
You need to run the script to apply the schema to the `users` collection.
79
137
80
138
1. Open a new terminal emulator tab in your GitHub codespace.
@@ -102,12 +160,42 @@ You need to run the script to apply the schema to the `users` collection.
102
160
Schema validation enabled!
103
161
```
104
162
163
+
</TabItem>
164
+
165
+
<TabItemvalue="java"label="☕️ Java Spring Boot">
166
+
167
+
1. Stop the running app.
168
+
1. Locate the bottom panel and click on the `TERMINAL` tab.
169
+
1. Press Ctrl+C to interrup the running app
170
+
171
+
1. Copy above code for `applySchemaValidation` as a method in the `LibraryApplication.java` class.
172
+
1. Modify the `run` method to call `applySchemaValidation`
173
+
```java
174
+
@Override
175
+
publicvoid run(String... args) {
176
+
log.info("🚀 App Started");
177
+
applySchemaValidation();
178
+
}
179
+
```
180
+
1. Restart the app typing in the Terminal:
181
+
182
+
```bash
183
+
mvn spring-boot:start
184
+
```
185
+
186
+
</TabItem>
187
+
</Tabs>
188
+
189
+
105
190
:::caution
106
191
If you see an error related to your user permissions, go back to the [Database User Permissions](#database-user-permissions) section and update your user permissions.
107
192
:::
108
193
109
194
## Test the schema validation
110
195
196
+
<TabsgroupId="server">
197
+
<TabItemvalue="node"label="🚀 NodeJS/Express">
198
+
111
199
Now that the schema validation is enabled for the `users` collection, you can test it by inserting a document that does not match the schema.
112
200
113
201
1. Open the file `server/src/schema-validation/test-validation.ts`[file](https://github.com/mongodb-developer/library-management-system/blob/main/server/src/schema-validation/test-validation.ts).
@@ -122,6 +210,26 @@ The error describes that the `name` and `isAdmin` fields are required but missin
122
210
123
211
Modify the script to insert a document again with the `name` and `isAdmin` fields and you should see the document inserted successfully.
124
212
213
+
214
+
</TabItem>
215
+
216
+
<TabItemvalue="java"label="☕️ Java Spring Boot">
217
+
218
+
Now that the schema validation is enabled for the `users` collection, you can test it by inserting a document that does not match the schema, or you can check the Validation tab in Compass to check for the new validation rules.
219
+
220
+
You can also check it in the mongosh typing:
221
+
222
+
```
223
+
db.getCollectionInfos({ name: "users" })
224
+
```
225
+
226
+
If schema validation is not enabled, the "validator" field will be missing or empty.
227
+
228
+
229
+
</TabItem>
230
+
</Tabs>
231
+
232
+
125
233
## Summary
126
234
127
235
In this exercise, you explored the JSON schema for the `users` collection, ran a script to apply the schema to the collection, and tested the schema validation by inserting a document that does not match the schema.
Copy file name to clipboardexpand all lines: docs/70-indexing/1-create-compound-index.mdx
+57-6
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,19 @@
1
-
# 👐 Build a compound index following the ESR rule
1
+
importTabsfrom'@theme/Tabs';
2
+
importTabItemfrom'@theme/TabItem';
2
3
3
-
:::info
4
-
Note for Java users: we will create indexes via a Javascript script! Although there's nothing stopping us to write this code in Java, this is usually a DB Admin task.
5
-
:::
4
+
# 👐 Build a compound index following the ESR rule
6
5
7
6
In this exercise, you will build a compound index following the ESR rule, compare the query explain plans before and after creating the index, and analyze them.
8
7
9
8
## Explore the code
10
9
10
+
11
+
<TabsgroupId="server">
12
+
<TabItemvalue="node"label="🚀 NodeJS/Express">
13
+
11
14
1. Open the file `server/src/indexing/borrowed-books-index.ts`[file](https://github.com/mongodb-developer/library-management-system/blob/main/server/src/indexing/borrowed-books-index.ts) in your GitHub Codespace.
12
15
13
-
2. Examine the code to build a compound index on the `borrowedBooks` collection.
16
+
2. Examine the code to build a compound index on the `issueDetails` collection.
14
17
15
18
```js
16
19
/**
@@ -33,7 +36,7 @@ In this exercise, you will build a compound index following the ESR rule, compar
33
36
```
34
37
35
38
:::info
36
-
The index is created on the `borrowedBooks` collection to support the query that finds the issue details for a user with a specific `userID`, where the `borrowDate` is greater than or equal to a specific `date`, and sorts the results by `returnedDate` in descending order.
39
+
The index is created on the `issueDetails` collection to support the query that finds the issue details for a user with a specific `userID`, where the `borrowDate` is greater than or equal to a specific `date`, and sorts the results by `returnedDate` in descending order.
37
40
38
41
This is compound index and it follows the ESR rule: Equality, Sort, and Range. This ensures optimal performance for the query.
39
42
:::
@@ -73,6 +76,54 @@ In this exercise, you will build a compound index following the ESR rule, compar
73
76
Notice the difference in the winning plan stage and the number of documents examined before and after creating the index.
74
77
:::
75
78
79
+
80
+
</TabItem>
81
+
82
+
<TabItemvalue="java"label="☕️ Java Spring Boot">
83
+
84
+
1. Open the file `java-server/java-server/src/main/java/com/mongodb/devrel/library/model/IssueDetail.java`[file](https://github.com/mongodb-developer/library-management-system/blob/java-server/java-server/src/main/java/com/mongodb/devrel/library/model/IssueDetail.java) in your GitHub Codespace.
85
+
86
+
1. Examine the code to build a compound index on the `issueDetails` collection.
The index is created on the `issueDetails` collection to support the query that finds the issue details for a user with a specific `userID`, where the `borrowDate` is greater than or equal to a specific `date`, and sorts the results by `returnedDate` in descending order.
105
+
106
+
This is compound index and it follows the ESR rule: Equality, Sort, and Range. This ensures optimal performance for the query.
107
+
:::
108
+
1. Add to `application.properties` this line to enable automatic creation of indexes
109
+
110
+
```
111
+
spring.data.mongodb.auto-index-creation=true
112
+
```
113
+
1. Stop the running app.
114
+
1. Locate the bottom panel and click on the `TERMINAL` tab.
115
+
1. Press Ctrl+C to interrup the running app
116
+
117
+
1. Restart the app typing in the Terminal:
118
+
119
+
```bash
120
+
mvn spring-boot:start
121
+
```
122
+
123
+
</TabItem>
124
+
</Tabs>
125
+
126
+
76
127
## 🦸♀️ Try different indexes
77
128
78
129
Modify the compound index by adding and removing fields, and observe the changes in the query explain plans.
0 commit comments