Skip to content

Commit 2a31e49

Browse files
authored
DOCSP-27445: instantiate collection in fundamental pgs + implement "myColl" variable naming (#517)
* DOCSP-27445: instantiate collection in insert examples * changed to myColl and/or instantiated collection * agreement fixes * MW PR fixes 1 * MW PR fixes 2
1 parent 4cff72b commit 2a31e49

33 files changed

+205
-171
lines changed

source/code-snippets/collation/aggregation-collation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
collection.aggregate(
1+
myColl.aggregate(
22
[
33
{ $group: { "_id": "$first_name", "nameCount": { "$sum": 1 } } },
44
{ $sort: { "_id": 1 } },

source/code-snippets/collation/collection-collation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ db.createCollection("souvenirs", {
66
// end create collection with collation
77

88
// start collection query without collation
9-
collection.find({type: "photograph"});
9+
myColl.find({type: "photograph"});
1010
// end collection query without collation
1111

1212
// start collection query with collation
13-
collection.find({type: "photograph"},
13+
myColl.find({type: "photograph"},
1414
{ collation: { locale: "is", caseFirst: "upper" } }
1515
);
1616
// end collection query with collation
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
collection.find({ city: "New York" }, { collation: { locale: "de" } })
1+
myColl.find({ city: "New York" }, { collation: { locale: "de" } })
22
.sort({ name: 1 });

source/code-snippets/collation/findOneAndDelete-collation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// start findOneAndDelete example without collation
2-
await collection.findOneAndDelete({ a: { $gt: "100" } });
2+
await myColl.findOneAndDelete({ a: { $gt: "100" } });
33
// end findOneAndDelete example without collation
44

55
// start findOneAndDelete example with collation
6-
collection.findOneAndDelete(
6+
myColl.findOneAndDelete(
77
{ a: { $gt: "100" } },
88
{ collation: { locale: "en", numericOrdering: true } },
99
);

source/code-snippets/collation/findOneAndUpdate-collation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// start findOneAndUpdate without collation
2-
collection.findOneAndUpdate(
2+
myColl.findOneAndUpdate(
33
{ first_name : { $lt: "Gunter" } },
44
{ $set: { verified: true } }
55
);
66
// end findOneAndUpdate without collation
77

88
// start findOneAndUpdate with collation
9-
collection.findOneAndUpdate(
9+
myColl.findOneAndUpdate(
1010
{ first_name: { $lt: "Gunter" } },
1111
{ $set: { verified: true } },
1212
{ collation: { locale: "de@collation=phonebook" } },
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
collection.createIndex(
1+
myColl.createIndex(
22
{ 'title' : 1 },
33
{ 'collation' : { 'locale' : 'en_US' } });
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// start query index collation
2-
collection.find({"year": 1980}, {"collation" : {"locale" : "en_US" }})
2+
myColl.find({"year": 1980}, {"collation" : {"locale" : "en_US" }})
33
.sort({"title": -1});
44
// end query index collation
55

66
// start query without index collation
77
// no collation specified
8-
collection.find({"year": 1980})
8+
myColl.find({"year": 1980})
99
.sort({"title": -1});
1010

1111
// collation differs from the one on the index
12-
collection.find({"year": 1980}, {"collation" : {"locale" : "en_US", "strength": 2 }})
12+
myColl.find({"year": 1980}, {"collation" : {"locale" : "en_US", "strength": 2 }})
1313
.sort({"title": -1});
1414
// end query without index collation

source/code-snippets/crud/cursor.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,60 @@ const uri =
66
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
77
const client = new MongoClient(uri);
88

9-
async function forEachIteration(collection) {
9+
async function forEachIteration(myColl) {
1010
// start foreach cursor example
11-
const cursor = collection.find({});
11+
const cursor = myColl.find({});
1212
await cursor.forEach(doc => console.log(doc));
1313
// end foreach cursor example
1414
console.log(await cursor.count());
1515
}
1616

17-
async function asyncIteration(collection) {
17+
async function asyncIteration(myColl) {
1818
// start async cursor example
19-
const cursor = collection.find({});
19+
const cursor = myColl.find({});
2020
console.log("async");
2121
for await (const doc of cursor) {
2222
console.log(doc);
2323
}
2424
// end async cursor example
2525
}
2626

27-
async function manualIteration(collection) {
27+
async function manualIteration(myColl) {
2828
// start manual cursor example
29-
const cursor = collection.find({});
29+
const cursor = myColl.find({});
3030

3131
while (await cursor.hasNext()) {
3232
console.log(await cursor.next());
3333
}
3434
// end manual cursor example
3535
}
3636

37-
async function streamAPI(collection) {
37+
async function streamAPI(myColl) {
3838
// start stream cursor example
39-
const cursor = collection.find({});
39+
const cursor = myColl.find({});
4040
cursor.stream().on("data", doc => console.log(doc));
4141
// end stream cursor example
4242
}
4343

44-
async function eventAPI(collection) {
44+
async function eventAPI(myColl) {
4545
// start event cursor example
46-
const cursor = collection.find({});
46+
const cursor = myColl.find({});
4747
// the "data" event is fired once per document
4848
cursor.on("data", data => console.log(data));
4949
// end event cursor example
5050
}
5151

52-
async function fetchAll(collection) {
52+
async function fetchAll(myColl) {
5353
// start fetchAll cursor example
54-
const cursor = collection.find({});
54+
const cursor = myColl.find({});
5555
const allValues = await cursor.toArray();
5656
// end fetchAll cursor example
5757
console.log(allValues.length);
5858
}
5959

60-
async function rewind(collection) {
60+
async function rewind(myColl) {
6161
// start rewind cursor example
62-
const cursor = collection.find({});
62+
const cursor = myColl.find({});
6363
const firstResult = await cursor.toArray();
6464
console.log("First count: " + firstResult.length);
6565
await cursor.rewind();
@@ -68,9 +68,9 @@ async function rewind(collection) {
6868
// end rewind cursor example
6969
}
7070

71-
async function count(collection) {
71+
async function count(myColl) {
7272
// start count cursor example
73-
const cursor = collection.find({});
73+
const cursor = myColl.find({});
7474
const count = await cursor.count();
7575
// end count cursor example
7676
console.log(count);

source/code-snippets/typescript/dot-notation.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ interface TestType {
33
field: { nested: number };
44
}
55
const database = client.db("<your db>");
6-
const collection = database.collection<TestType>("<your collection>");
7-
await collection.updateOne({}, { $set: { "field.nested": "A string" } });
6+
const myColl = database.collection<TestType>("<your collection>");
7+
await myColl.updateOne({}, { $set: { "field.nested": "A string" } });
88
// end-no-error
99
// start-error
1010
interface TestType {
1111
field: { nested: number };
1212
}
1313
const database = client.db("<your db>");
14-
const collection = database.collection<TestType>("<your collection>");
15-
await collection.updateOne({}, { $set: { field: { nested: "A string" } } });
14+
const myColl = database.collection<TestType>("<your collection>");
15+
await myColl.updateOne({}, { $set: { field: { nested: "A string" } } });
1616
// end-error
1717
// start-no-key
1818
interface User {
1919
email: string;
2020
}
2121

2222
const database = client.db("<your database>");
23-
const collection = db.collection<User>("<your collection>");
24-
collection.find({ age: "Accepts any type!" });
23+
const myColl = db.collection<User>("<your collection>");
24+
myColl.find({ age: "Accepts any type!" });
2525
// end-no-key

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ let changeStream;
99
async function run() {
1010
try {
1111
const database = client.db("insertDB");
12-
const collection = database.collection("haikus");
12+
const haikus = database.collection("haikus");
1313

1414
// Open a Change Stream on the "haikus" collection
15-
changeStream = collection.watch();
15+
changeStream = haikus.watch();
1616

1717
// Print change events
1818
for await (const change of changeStream) {

0 commit comments

Comments
 (0)