Skip to content

Commit 2c7d5f5

Browse files
stephmarie17lindseymoore
authored andcommitted
DOCSP-48487 Move usage examples (mongodb#1071)
1 parent 10c9684 commit 2c7d5f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+810
-1342
lines changed

config/redirects

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ raw: ${prefix}/stable -> ${base}/current/
2626

2727
[*-v6.0)]: ${prefix}/${version}/fundamentals/connection/socks/ -> ${base}/${version}/
2828

29+
# Comprehensive Coverage Redirects
30+
2931
[*-master]: ${prefix}/${version}/quick-start/download-and-install/ -> ${base}/${version}/get-started/
3032
[*-master]: ${prefix}/${version}/quick-start/create-a-deployment/ -> ${base}/${version}/get-started/
3133
[*-master]: ${prefix}/${version}/quick-start/create-a-connection-string/ -> ${base}/${version}/get-started/
@@ -49,7 +51,6 @@ raw: ${prefix}/stable -> ${base}/current/
4951
[*-master]: ${prefix}/${version}/fundamentals/gridfs/ -> ${base}/${version}/crud/gridfs/
5052
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/retrieve/ -> ${base}/${version}/crud/query/retrieve/
5153
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/project/ -> ${base}/${version}/crud/query/project/
52-
[*-master]: ${prefix}/${version}/fundamentals/usage-examples/count/ -> ${base}/${version}/crud/query/count/
5354
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/distinct/ -> ${base}/${version}/crud/query/distinct/
5455
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/cursor/ -> ${base}/${version}/crud/query/cursor/
5556
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/geo/ -> ${base}/${version}/crud/query/geo/
@@ -83,6 +84,23 @@ raw: ${prefix}/stable -> ${base}/current/
8384
[*-master]: ${prefix}/${version}/fundamentals/crud/write-operations/modify/ -> ${base}/crud/update/modify/
8485
[*-master]: ${prefix}/${version}/fundamentals/crud/write-operations/embedded-arrays/ -> ${base}/crud/update/embedded-arrays/
8586
[*-master]: ${prefix}/${version}/fundamentals/crud/write-operations/pkFactory/ -> ${base}/crud/pkFactory/
86-
87-
# Comprehensive Coverage Redirects
8887
[*-master]: ${prefix}/${version}/security/authentication/enterprise-mechanisms/ -> ${base}/security/authentication
88+
89+
# Usage Example Redirects
90+
91+
[*-master]: ${prefix}/${version}/usage-examples/findOne/ -> ${base}/crud/query/retrieve/
92+
[*-master]: ${prefix}/${version}/usage-examples/find/ -> ${base}/crud/query/retrieve/
93+
[*-master]: ${prefix}/${version}/usage-examples/insertOne/ -> ${base}/crud/insert/
94+
[*-master]: ${prefix}/${version}/usage-examples/insertMany/ -> ${base}/crud/insert/
95+
[*-master]: ${prefix}/${version}/usage-examples/updateOne/ -> ${base}/crud/update/modify/
96+
[*-master]: ${prefix}/${version}/usage-examples/updateMany/ -> ${base}/crud/update/modify/
97+
[*-master]: ${prefix}/${version}/usage-examples/replaceOne/ -> ${base}/crud/update/replace/
98+
[*-master]: ${prefix}/${version}/usage-examples/deleteOne/ -> ${base}/crud/delete/
99+
[*-master]: ${prefix}/${version}/usage-examples/deleteMany/ -> ${base}/crud/delete/
100+
[*-master]: ${prefix}/${version}/usage-examples/count/ -> ${base}/crud/query/count/
101+
[*-master]: ${prefix}/${version}/usage-examples/distinct/ -> ${base}/crud/query/distinct/
102+
[*-master]: ${prefix}/${version}/usage-examples/command/ -> ${base}/run-command/
103+
[*-master]: ${prefix}/${version}/usage-examples/bulkWrite/ -> ${base}/bulk-write/
104+
[*-master]: ${prefix}/${version}/usage-examples/transactions/ -> ${base}/crud/transactions/
105+
[*-master]: ${prefix}/${version}/usage-examples/transaction-conv/ -> ${base}/crud/transactions/transaction-conv/
106+
[*-master]: ${prefix}/${version}/usage-examples/transaction-core/ -> ${base}/crud/transactions/transaction-core/

snooty.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ toc_landing_pages = [
1515
"/security/authentication",
1616
"/aggregation-tutorials",
1717
"/data-formats",
18-
"connect/connection-options",
19-
"crud",
18+
"/connect/connection-options",
19+
"/crud",
2020
"/crud/query",
2121
"crud/update",
22-
"monitoring-and-logging"
22+
"/crud/transactions",
23+
"/monitoring-and-logging"
2324
]
2425
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
2526

source/archive-reference-files/collations.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.. _node-fundamentals-collations:
2-
.. _node-collations:
32

43
==========
54
Collations

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ async function run() {
99
try {
1010

1111
// Get the database and collection on which to run the operation
12-
const database = client.db("insertDB");
13-
const foods = database.collection("foods");
12+
const database = client.db("sample_mflix");
13+
const movies = database.collection("movies");
1414

1515
// Create an array of documents to insert
16-
const docs = [
17-
{ name: "cake", healthy: false },
18-
{ name: "lettuce", healthy: true },
19-
{ name: "donut", healthy: false }
16+
const moviesToInsert = [
17+
{ title: "Arsenic and Old Lace", genres: ["Comedy", "Romance"], year: 1944, cast: ["Cary Grant", "Priscilla Lane", "Raymond Massey"] },
18+
{ title: "Ball of Fire", genres: ["Comedy", "Romance"], year: 1941, cast: ["Gary Cooper", "Barbara Stanwyck", "Oskar Homolka"] },
19+
{ title: "I Married a Witch", genres: ["Comedy", "Fantasy", "Romance"], year: 1942, cast: ["Veronica Lake", "Fredric March", "Susan Hayward"] },
2020
];
2121

2222
// Prevent additional documents from being inserted if one fails
2323
const options = { ordered: true };
2424

2525
// Execute insert operation
26-
const result = await foods.insertMany(docs, options);
26+
const result = await movies.insertMany(moviesToInsert, options);
2727

2828
// Print result
2929
console.log(`${result.insertedCount} documents were inserted`);

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ const uri = "<connection string uri>";
55

66
const client = new MongoClient(uri);
77

8-
interface Food {
9-
name: string;
10-
healthy: boolean;
8+
interface Movie {
9+
title: string;
10+
genres: string[];
11+
year: number;
12+
cast: string[];
1113
}
1214

1315
async function run() {
1416
try {
15-
const database = client.db("insertDB");
17+
const database = client.db("sample_mflix");
1618
// Specifying a schema is optional, but it enables type hints on
1719
// finds and inserts
18-
const foods = database.collection<Food>("foods");
20+
const movies = database.collection<Movie>("movies");
1921

20-
const result = await foods.insertMany(
21-
[
22-
{ name: "cake", healthy: false },
23-
{ name: "lettuce", healthy: true },
24-
{ name: "donut", healthy: false },
25-
],
22+
const result = await movies.insertMany(
23+
{ title: "Arsenic and Old Lace", genres: ["Comedy", "Romance"], year: 1944, cast: ["Cary Grant", "Priscilla Lane", "Raymond Massey"] },
24+
{ title: "Ball of Fire", genres: ["Comedy", "Romance"], year: 1941, cast: ["Gary Cooper", "Barbara Stanwyck", "Oskar Homolka"] },
25+
{ title: "I Married a Witch", genres: ["Comedy", "Fantasy", "Romance"], year: 1942, cast: ["Veronica Lake", "Fredric March", "Susan Hayward"] },
2626
{ ordered: true }
2727
);
2828
console.log(`${result.insertedCount} documents were inserted`);

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

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

99
async function run() {
1010
try {
11-
// Connect to the "insertDB" database and access its "haiku" collection
12-
const database = client.db("insertDB");
13-
const haiku = database.collection("haiku");
11+
// Connect to the "sample_mflix" database and access its "movies" collection
12+
const database = client.db("sample_mflix");
13+
const movies = database.collection("movies");
1414

1515
// Create a document to insert
1616
const doc = {
17-
title: "Record of a Shriveled Datum",
18-
content: "No bytes, no problem. Just insert a document, in MongoDB",
17+
title: "Charade",
18+
genres: ["Comedy", "Romance", "Thriller"],
19+
year: 1963,
20+
cast: ["Cary Grant", "Audrey Hepburn", "Walter Matthau"],
1921
}
20-
// Insert the defined document into the "haiku" collection
21-
const result = await haiku.insertOne(doc);
22+
// Insert the defined document into the "movies" collection
23+
const result = await movies.insertOne(doc);
2224

2325
// Print the ID of the inserted document
2426
console.log(`A document was inserted with the _id: ${result.insertedId}`);

source/code-snippets/usage-examples/insertOne.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ const uri = "<connection string uri>";
55

66
const client = new MongoClient(uri);
77

8-
interface Haiku {
8+
interface Movie {
99
title: string;
10-
content: string;
10+
content: string[];
11+
year: number;
12+
cast: string[];
1113
}
1214

1315
async function run() {
1416
try {
15-
const database = client.db("insertDB");
17+
const database = client.db("sample_mflix");
1618
// Specifying a Schema is optional, but it enables type hints on
1719
// finds and inserts
18-
const haiku = database.collection<Haiku>("haiku");
19-
const result = await haiku.insertOne({
20-
title: "Record of a Shriveled Datum",
21-
content: "No bytes, no problem. Just insert a document, in MongoDB",
20+
const movies = database.collection<Movie>("movies");
21+
const result = await movies.insertOne({
22+
title: "Charade",
23+
genres: ["Comedy", "Romance", "Thriller"],
24+
year: 1963,
25+
cast: ["Cary Grant", "Audrey Hepburn", "Walter Matthau"],
2226
});
2327
console.log(`A document was inserted with the _id: ${result.insertedId}`);
2428
} finally {

source/crud/bulk-write.txt

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ Bulk Operations
1010
:depth: 2
1111
:class: singlecol
1212

13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:description: Learn how to perform bulk operations by using the {+driver-long+}.
19+
:keywords: insert, update, replace, code example, efficiency
20+
1321
Overview
1422
--------
1523

@@ -220,7 +228,7 @@ The following table describes the fields that you can set in a
220228

221229
* - ``collation``
222230
- | (Optional) The collation to use when sorting results. To learn more
223-
about collations, see the :ref:`node-fundamentals-collations` guide.
231+
about collations, see the :ref:`node-collations` section of the :ref:`node-configure` guide.
224232
| Type: ``String`` or ``Object``
225233

226234
* - ``hint``
@@ -299,7 +307,7 @@ The following table describes the fields that you can set in a
299307

300308
* - ``collation``
301309
- | (Optional) The collation to use when sorting results. To learn more
302-
about collations, see the :ref:`node-fundamentals-collations` guide.
310+
about collations, see the :ref:`node-collations` section of the :ref:`node-configure` guide.
303311
| Type: ``String`` or ``Object``
304312

305313
* - ``hint``
@@ -387,7 +395,7 @@ The following table describes the fields you can set in an ``UpdateOneModel`` or
387395

388396
* - ``collation``
389397
- | (Optional) The collation to use when sorting results. To learn more about
390-
collations, see the :ref:`node-fundamentals-collations` guide.
398+
collations, see the :ref:`node-collations` section of the :ref:`node-configure` guide.
391399
| Type: ``Object``
392400

393401
* - ``hint``
@@ -474,7 +482,7 @@ to specify an update operation:
474482

475483
* - ``collation``
476484
- | (Optional) The collation to use when sorting results. To learn more about
477-
collations, see the :ref:`node-fundamentals-collations` guide.
485+
collations, see the :ref:`node-collations` section of the :ref:`node-configure` guide.
478486
| Type: ``Document``
479487

480488
* - ``hint``
@@ -558,7 +566,7 @@ The following table describes the fields you can set in a ``DeleteOneModel`` or
558566

559567
* - ``collation``
560568
- | (Optional) The collation to use when sorting results. To learn more about
561-
collations, see the :ref:`node-fundamentals-collations` guide.
569+
collations, see the :ref:`node-collations` section of the :ref:`node-configure` guide.
562570
| Type: ``Object``
563571

564572
* - ``hint``
@@ -635,7 +643,7 @@ to specify a delete operation:
635643

636644
* - ``collation``
637645
- | (Optional) The collation to use when sorting results. To learn more about
638-
collations, see the :ref:`node-fundamentals-collations` guide.
646+
collations, see the :ref:`node-collations` section of the :ref:`node-configure` guide.
639647
| Type: ``Document``
640648

641649
Example
@@ -823,6 +831,52 @@ A ``MongoClientBulkWriteError`` object contains the following properties:
823831
progress before the error.
824832
| Type: ``ClientBulkWriteResult``
825833

834+
.. _node-usage-bulk:
835+
836+
bulkWrite() Example: Full File
837+
------------------------------
838+
839+
.. include:: /includes/crud/example-intro.rst
840+
841+
The following code is a complete, standalone file that performs a bulk write
842+
operation on the ``theaters`` collection in the ``sample_mflix`` database. The
843+
``operations`` parameter includes examples of ``insertOne``,
844+
``updateMany``, and ``deleteOne`` write operations:
845+
846+
.. tabs::
847+
848+
.. tab:: JavaScript
849+
:tabid: javascript
850+
851+
.. literalinclude:: /code-snippets/usage-examples/bulkWrite.js
852+
:language: javascript
853+
:linenos:
854+
855+
.. tab:: TypeScript
856+
:tabid: typescript
857+
858+
.. literalinclude:: /code-snippets/usage-examples/bulkWrite.ts
859+
:language: typescript
860+
:linenos:
861+
862+
Running the preceding example results in the following output:
863+
864+
.. code-block:: javascript
865+
:copyable: false
866+
867+
BulkWriteResult {
868+
insertedCount: 2,
869+
matchedCount: 1,
870+
modifiedCount: 1,
871+
deletedCount: 0,
872+
upsertedCount: 0,
873+
upsertedIds: {},
874+
insertedIds: {
875+
'0': new ObjectId("..."),
876+
'1': new ObjectId("...")
877+
}
878+
}
879+
826880
.. _node-bulk-addtl-info:
827881

828882
Additional Information

0 commit comments

Comments
 (0)