Skip to content

Commit f7f1e37

Browse files
committed
docsp-32718 - add comments (#770)
(cherry picked from commit 11461c3)
1 parent a4ca550 commit f7f1e37

File tree

9 files changed

+77
-13
lines changed

9 files changed

+77
-13
lines changed

source/code-snippets/crud/arrayFilters.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ const client = new MongoClient(uri);
77

88
async function printData() {
99
try {
10+
11+
// Get the database and collection on which to run the operation
1012
const myDB = client.db("test");
1113
const myColl = myDB.collection("testColl");
1214

15+
// Print all documents
1316
console.log(JSON.stringify(await myColl.find().toArray()));
1417
} finally {
1518
await client.close();
@@ -18,18 +21,28 @@ async function printData() {
1821

1922
async function runFirstArrayElement() {
2023
try {
24+
25+
// Get the database and collection on which to run the operation
2126
const myDB = client.db("test");
2227
const myColl = myDB.collection("testColl");
2328

29+
// Print the result
2430
console.log(JSON.stringify(await myColl.find().toArray()));
2531

2632
// start firstArrayElement example
33+
// Query for all elements in entries array where the value of x is a string
2734
const query = { "entries.x": { $type : "string" } };
35+
36+
// On first matched element, increase value of y by 33
2837
const updateDocument = {
2938
$inc: { "entries.$.y": 33 }
3039
};
40+
41+
// Execute the update operation
3142
const result = await myColl.updateOne(query, updateDocument);
3243
// end firstArrayElement example
44+
45+
// Print all documents
3346
console.log(result.modifiedCount);
3447
console.log(JSON.stringify(await myColl.find().toArray()));
3548
} finally {
@@ -39,19 +52,30 @@ async function runFirstArrayElement() {
3952

4053
async function runAllArrayElements() {
4154
try {
55+
56+
// Get the database and collection on which to run the operation
4257
const myDB = client.db("test");
4358
const myColl = myDB.collection("testColl");
4459

60+
// Print all documents
4561
console.log(JSON.stringify(await myColl.find().toArray()));
4662

4763
// start allArrayElement example
64+
// Query for all documents where date is the string "5/15/2023"
4865
const query = { date: "5/15/2023" };
66+
67+
// For each matched document, remove duration field from all entries in calls array
4968
const updateDocument = {
5069
$unset: { "calls.$[].duration": "" }
5170
};
71+
72+
// Execute the update operation
5273
const result = await myColl.updateOne(query, updateDocument);
5374
// end allArrayElement example
75+
5476
console.log(result.modifiedCount);
77+
78+
// Print all documents
5579
console.log(JSON.stringify(await myColl.find().toArray()));
5680
} finally {
5781
await client.close();
@@ -60,16 +84,24 @@ async function runAllArrayElements() {
6084

6185
async function arrayFiltersIdentifier() {
6286
try {
87+
88+
// Get the database and collection on which to run the operation
6389
const myDB = client.db("test");
6490
const myColl = myDB.collection("testColl");
6591

92+
// Print all documents
6693
console.log(JSON.stringify(await myColl.find().toArray()));
6794

6895
// start arrayFiltersIdentifier example
96+
// Query for all documents where date is the string "11/12/2023"
6997
const query = { date: "11/12/2023" };
98+
99+
// For each matched document, change the quantity of items to 2
70100
const updateDocument = {
71101
$mul: { "items.$[i].quantity": 2 }
72102
};
103+
104+
// Update only non-oil items used for fried rice
73105
const options = {
74106
arrayFilters: [
75107
{
@@ -78,10 +110,13 @@ async function arrayFiltersIdentifier() {
78110
}
79111
]
80112
};
113+
114+
// Execute the update operation
81115
const result = await myColl.updateOne(query, updateDocument, options);
82116
// end arrayFiltersIdentifier example
83117
console.log(result.modifiedCount);
84118

119+
// Print all documents
85120
console.log(JSON.stringify(await myColl.find().toArray()));
86121
} finally {
87122
await client.close();

source/code-snippets/indexes/text.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const client = new MongoClient(uri);
1010
async function run() {
1111
try {
1212
// begin-idx
13+
// Get the database and collection on which to create the index
1314
const myDB = client.db("testDB");
1415
const myColl = myDB.collection("blogPosts");
1516

@@ -23,8 +24,13 @@ async function run() {
2324
console.log(`Index created: ${result}`);
2425

2526
// begin-query
27+
// Query for documents where body or title contain "life ahead"
2628
const query = { $text: { $search: "life ahead" } };
29+
30+
// Show only the title field
2731
const projection = { _id: 0, title: 1 };
32+
33+
// Execute the find operation
2834
const cursor = myColl.find(query).project(projection);
2935
// end-query
3036
for await (const doc of cursor) {

source/code-snippets/monitoring/cpm-subscribe.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const client = new MongoClient(uri);
99

1010
// Replace <event name> with the name of the event you are subscribing to.
1111
const eventName = "<event name>";
12+
13+
// Subscribe to the event
1214
client.on(eventName, (event) =>
1315
console.log("\nreceived event:\n", event)
1416
);

source/code-snippets/usage-examples/distinct.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@ const client = new MongoClient(uri);
77

88
async function run() {
99
try {
10-
// define a database and collection on which to run the method
10+
11+
// Get the database and collection on which to run the operation
1112
const database = client.db("sample_mflix");
1213
const movies = database.collection("movies");
1314

14-
// specify the document field
15+
// Specify the document field to find distinct values for
1516
const fieldName = "year";
1617

17-
// specify an optional query document
18+
// Specify an optional query document to narrow results
1819
const query = { directors: "Barbra Streisand" };
1920

21+
// Execute the distinct operation
2022
const distinctValues = await movies.distinct(fieldName, query);
2123

24+
// Print the result
2225
console.log(distinctValues);
2326
} finally {
2427
await client.close();

source/code-snippets/usage-examples/find.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,30 @@ const client = new MongoClient(uri);
77

88
async function run() {
99
try {
10+
11+
// Get the database and collection on which to run the operation
1012
const database = client.db("sample_mflix");
1113
const movies = database.collection("movies");
1214

13-
// query for movies that have a runtime less than 15 minutes
15+
// Query for movies that have a runtime less than 15 minutes
1416
const query = { runtime: { $lt: 15 } };
1517

1618
const options = {
17-
// sort returned documents in ascending order by title (A->Z)
19+
// Sort returned documents in ascending order by title (A->Z)
1820
sort: { title: 1 },
1921
// Include only the `title` and `imdb` fields in each returned document
2022
projection: { _id: 0, title: 1, imdb: 1 },
2123
};
2224

25+
// Execute query
2326
const cursor = movies.find(query, options);
2427

25-
// print a message if no documents were found
28+
// Print a message if no documents were found
2629
if ((await movies.countDocuments(query)) === 0) {
2730
console.log("No documents found!");
2831
}
2932

33+
// Print returned documents
3034
for await (const doc of cursor) {
3135
console.dir(doc);
3236
}

source/code-snippets/usage-examples/findOne.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@ const client = new MongoClient(uri);
77

88
async function run() {
99
try {
10+
11+
// Get the database and collection on which to run the operation
1012
const database = client.db("sample_mflix");
1113
const movies = database.collection("movies");
1214

1315
// Query for a movie that has the title 'The Room'
1416
const query = { title: "The Room" };
1517

1618
const options = {
17-
// sort matched documents in descending order by rating
19+
// Sort matched documents in descending order by rating
1820
sort: { "imdb.rating": -1 },
1921
// Include only the `title` and `imdb` fields in the returned document
2022
projection: { _id: 0, title: 1, imdb: 1 },
2123
};
2224

25+
// Execute query
2326
const movie = await movies.findOne(query, options);
2427

25-
// since this method returns the matched document, not a cursor, print it directly
28+
// Print the document returned by findOne()
2629
console.log(movie);
2730
} finally {
2831
await client.close();

source/code-snippets/usage-examples/insertMany.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@ const client = new MongoClient(uri);
77

88
async function run() {
99
try {
10+
11+
// Get the database and collection on which to run the operation
1012
const database = client.db("insertDB");
1113
const foods = database.collection("foods");
1214

13-
// create an array of documents to insert
15+
// Create an array of documents to insert
1416
const docs = [
1517
{ name: "cake", healthy: false },
1618
{ name: "lettuce", healthy: true },
1719
{ name: "donut", healthy: false }
1820
];
1921

20-
// this option prevents additional documents from being inserted if one fails
22+
// Prevent additional documents from being inserted if one fails
2123
const options = { ordered: true };
2224

25+
// Execute insert operation
2326
const result = await foods.insertMany(docs, options);
27+
28+
// Print result
2429
console.log(`${result.insertedCount} documents were inserted`);
2530
} finally {
2631
await client.close();

source/code-snippets/usage-examples/replaceOne.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ const client = new MongoClient(uri);
77

88
async function run() {
99
try {
10+
11+
// Get the database and collection on which to run the operation
1012
const database = client.db("sample_mflix");
1113
const movies = database.collection("movies");
1214

13-
// create a query for a movie to update
15+
// Create a query for documents where the title contains "The Cat from"
1416
const query = { title: { $regex: "The Cat from" } };
15-
// create a new document that will be used to replace the existing document
17+
18+
// Create the document that will replace the existing document
1619
const replacement = {
1720
title: `The Cat from Sector ${Math.floor(Math.random() * 1000) + 1}`,
1821
};
1922

23+
// Execute the replace operation
2024
const result = await movies.replaceOne(query, replacement);
25+
26+
// Print the result
2127
console.log(`Modified ${result.modifiedCount} document(s)`);
2228
} finally {
2329
await client.close();

source/fundamentals/crud/write-operations/embedded-arrays.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ code:
283283
:language: javascript
284284
:start-after: start arrayFiltersIdentifier example
285285
:end-before: end arrayFiltersIdentifier example
286-
:emphasize-lines: 3, 6-11
286+
:emphasize-lines: 6, 11-16
287287
:dedent:
288288

289289
The update multiplied the ``quantity`` value by ``2`` for

0 commit comments

Comments
 (0)