Skip to content

DOCSP-48487 Move usage examples #1071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions config/redirects
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ raw: ${prefix}/stable -> ${base}/current/

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

# Comprehensive Coverage Redirects

[*-master]: ${prefix}/${version}/quick-start/download-and-install/ -> ${base}/${version}/get-started/
[*-master]: ${prefix}/${version}/quick-start/create-a-deployment/ -> ${base}/${version}/get-started/
[*-master]: ${prefix}/${version}/quick-start/create-a-connection-string/ -> ${base}/${version}/get-started/
Expand All @@ -49,7 +51,6 @@ raw: ${prefix}/stable -> ${base}/current/
[*-master]: ${prefix}/${version}/fundamentals/gridfs/ -> ${base}/${version}/crud/gridfs/
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/retrieve/ -> ${base}/${version}/crud/query/retrieve/
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/project/ -> ${base}/${version}/crud/query/project/
[*-master]: ${prefix}/${version}/fundamentals/usage-examples/count/ -> ${base}/${version}/crud/query/count/
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/distinct/ -> ${base}/${version}/crud/query/distinct/
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/cursor/ -> ${base}/${version}/crud/query/cursor/
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/geo/ -> ${base}/${version}/crud/query/geo/
Expand Down Expand Up @@ -83,6 +84,23 @@ raw: ${prefix}/stable -> ${base}/current/
[*-master]: ${prefix}/${version}/fundamentals/crud/write-operations/modify/ -> ${base}/crud/update/modify/
[*-master]: ${prefix}/${version}/fundamentals/crud/write-operations/embedded-arrays/ -> ${base}/crud/update/embedded-arrays/
[*-master]: ${prefix}/${version}/fundamentals/crud/write-operations/pkFactory/ -> ${base}/crud/pkFactory/

# Comprehensive Coverage Redirects
[*-master]: ${prefix}/${version}/security/authentication/enterprise-mechanisms/ -> ${base}/security/authentication

# Usage Example Redirects

[*-master]: ${prefix}/${version}/usage-examples/findOne/ -> ${base}/crud/query/retrieve/
[*-master]: ${prefix}/${version}/usage-examples/find/ -> ${base}/crud/query/retrieve/
[*-master]: ${prefix}/${version}/usage-examples/insertOne/ -> ${base}/crud/insert/
[*-master]: ${prefix}/${version}/usage-examples/insertMany/ -> ${base}/crud/insert/
[*-master]: ${prefix}/${version}/usage-examples/updateOne/ -> ${base}/crud/update/modify/
[*-master]: ${prefix}/${version}/usage-examples/updateMany/ -> ${base}/crud/update/modify/
[*-master]: ${prefix}/${version}/usage-examples/replaceOne/ -> ${base}/crud/update/replace/
[*-master]: ${prefix}/${version}/usage-examples/deleteOne/ -> ${base}/crud/delete/
[*-master]: ${prefix}/${version}/usage-examples/deleteMany/ -> ${base}/crud/delete/
[*-master]: ${prefix}/${version}/usage-examples/count/ -> ${base}/crud/query/count/
[*-master]: ${prefix}/${version}/usage-examples/distinct/ -> ${base}/crud/query/distinct/
[*-master]: ${prefix}/${version}/usage-examples/command/ -> ${base}/run-command/
[*-master]: ${prefix}/${version}/usage-examples/bulkWrite/ -> ${base}/bulk-write/
[*-master]: ${prefix}/${version}/usage-examples/transactions/ -> ${base}/crud/transactions/
[*-master]: ${prefix}/${version}/usage-examples/transaction-conv/ -> ${base}/crud/transactions/transaction-conv/
[*-master]: ${prefix}/${version}/usage-examples/transaction-core/ -> ${base}/crud/transactions/transaction-core/
7 changes: 4 additions & 3 deletions snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ toc_landing_pages = [
"/security/authentication",
"/aggregation-tutorials",
"/data-formats",
"connect/connection-options",
"crud",
"/connect/connection-options",
"/crud",
"/crud/query",
"crud/update",
"monitoring-and-logging"
"/crud/transactions",
"/monitoring-and-logging"
]
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"

Expand Down
1 change: 0 additions & 1 deletion source/archive-reference-files/collations.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.. _node-fundamentals-collations:
.. _node-collations:

==========
Collations
Expand Down
14 changes: 7 additions & 7 deletions source/code-snippets/usage-examples/insertMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ async function run() {
try {
Copy link
Collaborator Author

@stephmarie17 stephmarie17 Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[for technical reviewer] The /code-snippets/usage-examples/ folder starting here contains the only code changes for review, everything else in the PR is moving existing content.


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

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

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

// Execute insert operation
const result = await foods.insertMany(docs, options);
const result = await movies.insertMany(moviesToInsert, options);

// Print result
console.log(`${result.insertedCount} documents were inserted`);
Expand Down
22 changes: 11 additions & 11 deletions source/code-snippets/usage-examples/insertMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ const uri = "<connection string uri>";

const client = new MongoClient(uri);

interface Food {
name: string;
healthy: boolean;
interface Movie {
title: string;
genres: string[];
year: number;
cast: string[];
}

async function run() {
try {
const database = client.db("insertDB");
const database = client.db("sample_mflix");
// Specifying a schema is optional, but it enables type hints on
// finds and inserts
const foods = database.collection<Food>("foods");
const movies = database.collection<Movie>("movies");

const result = await foods.insertMany(
[
{ name: "cake", healthy: false },
{ name: "lettuce", healthy: true },
{ name: "donut", healthy: false },
],
const result = await movies.insertMany(
{ title: "Arsenic and Old Lace", genres: ["Comedy", "Romance"], year: 1944, cast: ["Cary Grant", "Priscilla Lane", "Raymond Massey"] },
{ title: "Ball of Fire", genres: ["Comedy", "Romance"], year: 1941, cast: ["Gary Cooper", "Barbara Stanwyck", "Oskar Homolka"] },
{ title: "I Married a Witch", genres: ["Comedy", "Fantasy", "Romance"], year: 1942, cast: ["Veronica Lake", "Fredric March", "Susan Hayward"] },
{ ordered: true }
);
console.log(`${result.insertedCount} documents were inserted`);
Expand Down
16 changes: 9 additions & 7 deletions source/code-snippets/usage-examples/insertOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ const client = new MongoClient(uri);

async function run() {
try {
// Connect to the "insertDB" database and access its "haiku" collection
const database = client.db("insertDB");
const haiku = database.collection("haiku");
// Connect to the "sample_mflix" database and access its "movies" collection
const database = client.db("sample_mflix");
const movies = database.collection("movies");

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

// Print the ID of the inserted document
console.log(`A document was inserted with the _id: ${result.insertedId}`);
Expand Down
18 changes: 11 additions & 7 deletions source/code-snippets/usage-examples/insertOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ const uri = "<connection string uri>";

const client = new MongoClient(uri);

interface Haiku {
interface Movie {
title: string;
content: string;
content: string[];
year: number;
cast: string[];
}

async function run() {
try {
const database = client.db("insertDB");
const database = client.db("sample_mflix");
// Specifying a Schema is optional, but it enables type hints on
// finds and inserts
const haiku = database.collection<Haiku>("haiku");
const result = await haiku.insertOne({
title: "Record of a Shriveled Datum",
content: "No bytes, no problem. Just insert a document, in MongoDB",
const movies = database.collection<Movie>("movies");
const result = await movies.insertOne({
title: "Charade",
genres: ["Comedy", "Romance", "Thriller"],
year: 1963,
cast: ["Cary Grant", "Audrey Hepburn", "Walter Matthau"],
});
console.log(`A document was inserted with the _id: ${result.insertedId}`);
} finally {
Expand Down
66 changes: 60 additions & 6 deletions source/crud/bulk-write.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ Bulk Operations
:depth: 2
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:description: Learn how to perform bulk operations by using the {+driver-long+}.
:keywords: insert, update, replace, code example, efficiency

Overview
--------

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

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

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

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

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

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

* - ``hint``
Expand Down Expand Up @@ -474,7 +482,7 @@ to specify an update operation:

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

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

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

* - ``hint``
Expand Down Expand Up @@ -635,7 +643,7 @@ to specify a delete operation:

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

Example
Expand Down Expand Up @@ -823,6 +831,52 @@ A ``MongoClientBulkWriteError`` object contains the following properties:
progress before the error.
| Type: ``ClientBulkWriteResult``

.. _node-usage-bulk:

bulkWrite() Example: Full File
------------------------------

.. include:: /includes/crud/example-intro.rst

The following code is a complete, standalone file that performs a bulk write
operation on the ``theaters`` collection in the ``sample_mflix`` database. The
``operations`` parameter includes examples of ``insertOne``,
``updateMany``, and ``deleteOne`` write operations:

.. tabs::

.. tab:: JavaScript
:tabid: javascript

.. literalinclude:: /code-snippets/usage-examples/bulkWrite.js
:language: javascript
:linenos:

.. tab:: TypeScript
:tabid: typescript

.. literalinclude:: /code-snippets/usage-examples/bulkWrite.ts
:language: typescript
:linenos:

Running the preceding example results in the following output:

.. code-block:: javascript
:copyable: false

BulkWriteResult {
insertedCount: 2,
matchedCount: 1,
modifiedCount: 1,
deletedCount: 0,
upsertedCount: 0,
upsertedIds: {},
insertedIds: {
'0': new ObjectId("..."),
'1': new ObjectId("...")
}
}

.. _node-bulk-addtl-info:

Additional Information
Expand Down
Loading
Loading