Skip to content

Commit 8f11c41

Browse files
author
Dave
authored
DOCSP-15571 productize new async writer (#199)
* DOCSP-15571 productize new async writer * Review feedback * Review comments * Staging fixes * Merge fix
1 parent 90e3b0c commit 8f11c41

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
.. Top level title in calling page, won't render in right TOC
2+
3+
The results of database queries cannot be passed inside the following
4+
contexts:
5+
6+
- Class constructor functions
7+
- Non-async generator functions
8+
- Callbacks to ``.sort()`` on an array
9+
10+
To access to the results of database calls, use `async functions
11+
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function>`__,
12+
`async generator functions
13+
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of>`__,
14+
or ``.map()``.
15+
16+
Constructors
17+
~~~~~~~~~~~~
18+
19+
The following constructors do not work:
20+
21+
.. code-block:: javascript
22+
:copyable: false
23+
24+
// This code will fail
25+
class FindResults {
26+
constructor() {
27+
this.value = db.students.find();
28+
}
29+
}
30+
31+
// This code will fail
32+
function listEntries() { return db.students.find(); }
33+
class FindResults {
34+
constructor() {
35+
this.value = listEntries();
36+
}
37+
}
38+
39+
Use an ``async`` function instead:
40+
41+
.. code-block:: javascript
42+
43+
class FindResults {
44+
constructor() {
45+
this.value = ( async() => {
46+
return db.students.find();
47+
} )();
48+
}
49+
}
50+
51+
.. note::
52+
53+
You can also create a method that performs a database operation
54+
inside a class as an alternative to working with asynchronous
55+
JavaScript.
56+
57+
.. code-block::
58+
59+
class FindResults {
60+
constructor() { }
61+
62+
init() { this.value = db.students.find(); }
63+
}
64+
65+
To use this class, first construct a class instance then call the
66+
``.init()`` method.
67+
68+
Generator Functions
69+
~~~~~~~~~~~~~~~~~~~
70+
71+
The following generator functions do not work:
72+
73+
.. code-block:: javascript
74+
:copyable: false
75+
76+
// This code will fail
77+
function* FindResults() {
78+
yield db.students.findMany();
79+
}
80+
81+
// This code will fail
82+
function listEntries() { return db.students.findMany(); }
83+
function* findResults() {
84+
yield listEntries();
85+
}
86+
87+
Use an ``async generator function`` instead:
88+
89+
.. code-block:: javascript
90+
91+
function listEntries() { return db.students.findMany(); }
92+
async function* findResults() {
93+
yield listEntries();
94+
}
95+
96+
Array Sort
97+
~~~~~~~~~~
98+
99+
The following array sort does not work:
100+
101+
.. code-block:: javascript
102+
:copyable: false
103+
104+
// This code will fail
105+
db.getCollectionNames().sort( ( collectionOne, collectionTwo ) => {
106+
return db[ collectionOne ].estimatedDocumentCount() - db[ collectionOne ].estimatedDocumentCount() )
107+
} );
108+
109+
Use ``.map()`` instead.
110+
111+
.. code-block:: javascript
112+
113+
db.getCollectionNames().map( collectionName => {
114+
return { collectionName, size: db[ collectionName ].estimatedDocumentCount() };
115+
} ).sort( ( collectionOne, collectionTwo ) => {
116+
return collectionOne.size - collectionTwo.size;
117+
} ).map( collection => collection.collectionName);
118+
119+
This approach to array sort is often more performant than the
120+
equivalent unsupported code.
121+

source/reference/compatibility.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,8 @@ output suitable for automated parsing, use ``EJSON.stringify()``.
170170

171171
.. include:: /includes/examples/ex-eval-output.rst
172172

173+
Limitations on Database Calls
174+
-----------------------------
175+
176+
.. include:: /includes/fact-call-limitations.rst
177+

source/write-scripts.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ Execute JavaScript and MongoDB Code
477477

478478
.. include:: /includes/examples/ex-display-dbname-and-hostname.rst
479479

480+
Limitations on Database Calls
481+
-----------------------------
482+
483+
.. include:: /includes/fact-call-limitations.rst
484+
480485
.. toctree::
481486
:titlesonly:
482487

0 commit comments

Comments
 (0)