Skip to content

Commit 267b2e4

Browse files
biniona-mongodbcbushdariakp
authored
(DOCSP 17936) Replace One TS (#239)
Co-authored-by: Chris Bush <chris.bush@10gen.com> Co-authored-by: Daria Pardue <81593090+dariakp@users.noreply.github.com>
1 parent 32a4c4c commit 267b2e4

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

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

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,14 @@ async function run() {
1414
const movies = database.collection("movies");
1515

1616
// create a query for a movie to update
17-
const query = { title: "Blacksmith Scene" };
18-
const options = {
19-
// create a document if no documents match the query
20-
upsert: true,
21-
};
17+
const query = { title: { $regex: "The Cat from" } };
2218
// create a new document that will be used to replace the existing document
2319
const replacement = {
24-
title: "Sandcastles in the Sand",
25-
plot:
26-
"Robin Sparkles mourns for a relationship with a mall rat at an idyllic beach.",
20+
title: `The Cat from Sector ${Math.floor(Math.random() * 1000) + 1}`,
2721
};
2822

29-
const result = await movies.replaceOne(query, replacement, options);
30-
31-
if (result.modifiedCount === 0 && result.upsertedCount === 0) {
32-
console.log("No changes made to the collection.");
33-
} else {
34-
if (result.matchedCount === 1) {
35-
console.log("Matched " + result.matchedCount + " documents.");
36-
}
37-
if (result.modifiedCount === 1) {
38-
console.log("Updated one document.");
39-
}
40-
if (result.upsertedCount === 1) {
41-
console.log(
42-
"Inserted one new document with an _id of " + result.upsertedId._id
43-
);
44-
}
45-
}
23+
const result = await movies.replaceOne(query, replacement);
24+
console.log(`Modified ${result.modifiedCount} document(s)`);
4625
} finally {
4726
await client.close();
4827
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { MongoClient } from "mongodb";
2+
3+
// Replace the uri string with your MongoDB deployment's connection string.
4+
const uri =
5+
"mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority";
6+
7+
const client = new MongoClient(uri);
8+
9+
interface Movie {
10+
title: string;
11+
}
12+
13+
async function run() {
14+
try {
15+
await client.connect();
16+
17+
const database = client.db("sample_mflix");
18+
const movies = database.collection<Movie>("movies");
19+
20+
const result = await movies.replaceOne(
21+
{ title: { $regex: "The Cat from" } },
22+
{
23+
title: `The Cat from Sector ${Math.floor(Math.random() * 1000) + 1}`,
24+
}
25+
);
26+
console.log(`Modified ${result.modifiedCount} document(s)`);
27+
} finally {
28+
await client.close();
29+
}
30+
}
31+
run().catch(console.dir);

source/usage-examples/replaceOne.txt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,25 @@ Example
4343

4444
.. include:: /includes/connect-guide-note.rst
4545

46-
.. literalinclude:: /code-snippets/usage-examples/replaceOne.js
47-
:language: javascript
48-
:linenos:
46+
.. tabs::
47+
48+
.. tab:: JavaScript
49+
:tabid: javascript
50+
51+
.. literalinclude:: /code-snippets/usage-examples/replaceOne.js
52+
:language: javascript
53+
:linenos:
54+
55+
.. tab:: TypeScript
56+
:tabid: typescript
57+
58+
.. literalinclude:: /code-snippets/usage-examples/replaceOne.ts
59+
:language: typescript
60+
:linenos:
61+
62+
If you run the preceding example, you should see output that resembles the
63+
following:
64+
65+
.. code-block:: none
66+
67+
Modified 1 document(s)

0 commit comments

Comments
 (0)