Skip to content

Commit e747538

Browse files
author
Caitlin Davey
committed
Merge branch 'v3.6' of github.com:mongodb/docs-node into v3.6
2 parents 8299295 + a4e7efc commit e747538

File tree

21 files changed

+795
-600
lines changed

21 files changed

+795
-600
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ toc_landing_pages = [
88
"/fundamentals",
99
"/fundamentals/connection",
1010
"/fundamentals/crud",
11+
"/fundamentals/bson",
1112
"/usage-examples",
1213
]
1314
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"

source/code-snippets/crud/arrayFilters.js

Lines changed: 35 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { MongoClient } = require("mongodb");
2-
const stream = require("stream");
32

43
// Replace the following string with your MongoDB deployment's connection string.
54
const uri =
@@ -9,212 +8,88 @@ const client = new MongoClient(uri, {
98
useUnifiedTopology: true,
109
});
1110

12-
13-
async function loadData() {
11+
async function printData() {
1412
try {
1513
await client.connect();
14+
const myDB = client.db("test");
15+
const myColl = myDB.collection("testColl");
1616

17-
const database = client.db("test");
18-
const pizza = database.collection("pizza");
19-
20-
await pizza.drop();
21-
22-
await pizza.insertMany([
23-
{
24-
name: "Steve Lobsters",
25-
address: "731 Yexington Avenue",
26-
items: [
27-
{
28-
type: "pizza",
29-
size: "large",
30-
toppings: ["pepperoni"],
31-
},
32-
{
33-
type: "pizza",
34-
size: "medium",
35-
toppings: ["mushrooms", "sausage", "green peppers"],
36-
comment: "Extra green peppers please!",
37-
},
38-
{
39-
type: "pizza",
40-
size: "large",
41-
toppings: ["pineapple, ham"],
42-
comment: "red pepper flakes on top",
43-
},
44-
{
45-
type: "calzone",
46-
fillings: ["canadian bacon", "sausage", "onion"],
47-
},
48-
{
49-
type: "beverage",
50-
name: "Diet Pepsi",
51-
size: "16oz",
52-
},
53-
],
54-
},
55-
{
56-
name: "Popeye",
57-
address:"1 Sweetwater",
58-
items: [
59-
{
60-
type: "pizza",
61-
size: "large",
62-
toppings: ["garlic", "spinach"]
63-
},
64-
{
65-
type: "calzone",
66-
toppings: ["ham"],
67-
},
68-
]
69-
}
70-
]);
71-
72-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
17+
console.log(JSON.stringify(await myColl.find().toArray()));
7318
} finally {
7419
await client.close();
7520
}
7621
}
7722

78-
79-
async function runAllArrayElements() {
80-
81-
try {
82-
await client.connect();
83-
84-
const database = client.db("test");
85-
const pizza = database.collection("pizza");
86-
87-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
88-
89-
// start allArrayElement example
90-
const query = { "name": "Popeye" };
91-
const updateDocument = {
92-
$push: { "items.$[].toppings": "fresh mozzarella" }
93-
};
94-
const result = await pizza.updateOne(query, updateDocument);
95-
// end allArrayElement example
96-
console.log(result.modifiedCount);
97-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
98-
} finally {
99-
await client.close();
100-
}
101-
}
10223
async function runFirstArrayElement() {
103-
10424
try {
10525
await client.connect();
26+
const myDB = client.db("test");
27+
const myColl = myDB.collection("testColl");
10628

107-
const database = client.db("test");
108-
const pizza = database.collection("pizza");
109-
110-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
29+
console.log(JSON.stringify(await myColl.find().toArray()));
11130

11231
// start firstArrayElement example
113-
const query = { name: "Steve Lobsters", "items.type": "pizza" };
32+
const query = { "entries.x": { $type : "string" } };
11433
const updateDocument = {
115-
$set: { "items.$.size": "extra large" }
34+
$inc: { "entries.$.y": 33 }
11635
};
117-
const result = await pizza.updateOne(query, updateDocument);
36+
const result = await myColl.updateOne(query, updateDocument);
11837
// end firstArrayElement example
11938
console.log(result.modifiedCount);
120-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
39+
console.log(JSON.stringify(await myColl.find().toArray()));
12140
} finally {
12241
await client.close();
12342
}
12443
}
12544

126-
127-
async function arrayFiltersOne() {
45+
async function runAllArrayElements() {
12846
try {
12947
await client.connect();
48+
const myDB = client.db("test");
49+
const myColl = myDB.collection("testColl");
13050

131-
const database = client.db("test");
132-
const pizza = database.collection("pizza");
51+
console.log(JSON.stringify(await myColl.find().toArray()));
13352

134-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
135-
136-
// start arrayFiltersOne example
137-
const query = { name: "Steve Lobsters" };
53+
// start allArrayElement example
54+
const query = { date: "5/15/2023" };
13855
const updateDocument = {
139-
$push: { "items.$[orderItem].toppings": "garlic" }
56+
$unset: { "calls.$[].duration": "" }
14057
};
141-
const options = {
142-
arrayFilters: [{
143-
"orderItem.type": "pizza",
144-
"orderItem.size": "large",
145-
}]
146-
};
147-
148-
const result = await pizza.updateMany(query, updateDocument, options);
149-
// end arrayFiltersOne example
150-
58+
const result = await myColl.updateOne(query, updateDocument);
59+
// end allArrayElement example
15160
console.log(result.modifiedCount);
152-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
61+
console.log(JSON.stringify(await myColl.find().toArray()));
15362
} finally {
15463
await client.close();
15564
}
15665
}
15766

158-
async function arrayFiltersTwo() {
67+
async function arrayFiltersIdentifier() {
15968
try {
16069
await client.connect();
70+
const myDB = client.db("test");
71+
const myColl = myDB.collection("testColl");
16172

162-
const database = client.db("test");
163-
const pizza = database.collection("pizza");
164-
165-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
73+
console.log(JSON.stringify(await myColl.find().toArray()));
16674

167-
// start arrayFiltersTwo example
168-
const query = { name: "Steve Lobsters" };
75+
// start arrayFiltersIdentifier example
76+
const query = { date: "11/12/2023" };
16977
const updateDocument = {
170-
$push: { "items.$[item].toppings": "salami" },
78+
$mul: { "items.$[i].quantity": 2 }
17179
};
17280
const options = {
17381
arrayFilters: [
17482
{
175-
"item.type": "pizza",
176-
"item.toppings": "pepperoni",
177-
},
178-
],
83+
"i.recipe": "Fried rice",
84+
"i.item": { $not: { $regex: "oil" } },
85+
}
86+
]
17987
};
180-
const result = await pizza.updateOne(query, updateDocument, options);
181-
// end arrayFiltersTwo example
88+
const result = await myColl.updateOne(query, updateDocument, options);
89+
// end arrayFiltersIdentifier example
18290
console.log(result.modifiedCount);
18391

184-
pizza.insertOne({
185-
name: "Steve Lobsters",
186-
address: "731 Yexington Avenue",
187-
items: [
188-
{
189-
type: "pizza",
190-
size: "large",
191-
toppings: ["pepperoni"],
192-
},
193-
{
194-
type: "pizza",
195-
size: "medium",
196-
toppings: ["mushrooms", "sausage", "green peppers"],
197-
comment: "Extra green peppers please!",
198-
},
199-
{
200-
type: "pizza",
201-
size: "large",
202-
toppings: ["pineapple, ham"],
203-
comment: "red pepper flakes on top",
204-
},
205-
{
206-
type: "calzone",
207-
fillings: ["canadian bacon", "sausage", "onion"],
208-
},
209-
{
210-
type: "beverage",
211-
name: "Diet Pepsi",
212-
size: "16oz",
213-
},
214-
],
215-
});
216-
217-
console.log(JSON.stringify(await (await pizza.find()).toArray()));
92+
console.log(JSON.stringify(await myColl.find().toArray()));
21893
} finally {
21994
await client.close();
22095
}

source/code-snippets/indexes/text.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,26 @@ async function run() {
1515
await client.connect();
1616

1717
// begin-idx
18-
const database = client.db("sample_mflix");
19-
const movies = database.collection("movies");
18+
const myDB = client.db("testDB");
19+
const myColl = myDB.collection("blogPosts");
2020

21-
// Create a text index on the "fullplot" field in the
22-
// "movies" collection.
23-
const result = await movies.createIndex({ fullplot: "text" }, { default_language: "english" });
24-
console.log(`Index created: ${result}`);
21+
// Create a text index on the "title" and "body" fields
22+
const result = await myColl.createIndex(
23+
{ title: "text", body: "text" },
24+
{ default_language: "english" },
25+
{ weights: { body: 10, title: 3 } }
26+
);
2527
// end-idx
28+
console.log(`Index created: ${result}`);
2629

2730
// begin-query
28-
const query = { $text: { $search: "java coffee shop" } };
29-
const projection = { _id: 0, fullplot: 1 };
30-
const cursor = movies
31-
.find(query)
32-
.project(projection);
31+
const query = { $text: { $search: "life ahead" } };
32+
const projection = { _id: 0, title: 1 };
33+
const cursor = myColl.find(query).project(projection);
3334
// end-query
34-
35+
for await (const doc of cursor) {
36+
console.log(doc);
37+
}
3538
} finally {
3639
await client.close();
3740
}

source/faq.txt

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -301,35 +301,25 @@ This sets the maximum number of ``file descriptors`` for the process to
301301
How Can I Prevent a Slow Operation From Delaying Other Operations?
302302
------------------------------------------------------------------
303303

304-
A slow operation may delay your other operations that occur after it, if
305-
the ``maxPoolSize`` has not been set in the
306-
:ref:`connection options <node-connection-options>`.
307-
MongoDB is synchronous and uses a single execution thread per socket,
308-
meaning that MongoDB will execute one single operation per socket at any
309-
point in time. Any other operation sent to that socket will have to wait
310-
until the current operation is finished. If you have a slow-running
311-
operation that holds up other operations, the best solution is to create
312-
a separate connection pool for the slow operation, isolating it from
313-
other, faster operations.
314-
315-
.. note::
316-
If the number of operations is greater than the value of the
317-
``maxPoolSize`` option and a slow operation occurs, subsequent
318-
operations will be delayed.
319-
320-
To create a separate connection pool, instantiate another ``MongoClient``
321-
call the ``connect()`` method on it. See the following example for the
322-
syntax you can use to create two clients, each with its own connection
323-
pool:
324-
325-
.. code-block:: javascript
326-
327-
const clientA = new MongoClient(uri, options);
328-
clientA.connect(); // any method calls on clientA use clientA's connection pool
329-
330-
const clientB = new MongoClient(uri, options);
331-
clientB.connect(); // any method calls on clientB use clientB's connection pool
304+
To control the maximum size of a connection pool, you can set the
305+
``maxPoolSize`` option in the :ref:`connection options
306+
<node-connection-options>`. The default value of ``maxPoolSize`` is
307+
``100``. If the number of in-use connections to a server reaches
308+
``maxPoolSize``, the next request to that server will wait
309+
until a connection becomes available. To prevent long-running operations
310+
from slowing down your application, you can increase ``maxPoolSize``.
311+
312+
The driver does not limit the number of requests that can wait for
313+
sockets to become available. Requests wait for the amount of time
314+
specified in the ``waitQueueTimeoutMS`` option, which
315+
defaults to ``0`` (no limit). You should set this option if it is
316+
more important to stop long-running operations than it is to complete
317+
every operation.
332318

319+
.. tip::
320+
321+
To learn more about connection pooling, see :ref:`How Does Connection
322+
Pooling Work in the Node Driver? <node-faq-connection-pool>`.
333323

334324
How Can I Ensure My Connection String Is Valid for a Replica Set?
335325
-----------------------------------------------------------------

source/fundamentals.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fundamentals
1717
/fundamentals/logging
1818
/fundamentals/monitoring
1919
/fundamentals/gridfs
20-
/fundamentals/csfle
20+
/fundamentals/encrypt-fields
21+
/fundamentals/bson
2122

2223
.. include:: /includes/fundamentals-sections.rst

source/fundamentals/bson.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.. _node-bson-control:
2+
3+
=============
4+
BSON Settings
5+
=============
6+
7+
.. toctree::
8+
:caption: BSON settings
9+
10+
/fundamentals/bson/undefined-values
11+
12+
.. contents:: On this page
13+
:local:
14+
:backlinks: none
15+
:depth: 1
16+
:class: singlecol
17+
18+
Overview
19+
--------
20+
21+
Learn how to configure your application's BSON serialization settings.
22+
The guides in this section describe the following topics:
23+
24+
- :ref:`Undefined Values <node-undefined-values>`: Control how the
25+
driver serializes undefined values

0 commit comments

Comments
 (0)