@@ -49,6 +49,8 @@ The ``EJSON.stringify()`` method has these fields:
4949 - Modifies output. If the value exists but it is not an array or a
5050 function, ``EJSON.stringify()`` returns all document fields.
5151
52+ ``replacer`` can be an array or a function.
53+
5254 .. list-table::
5355 :header-rows: 1
5456 :widths: 30 70
@@ -57,13 +59,19 @@ The ``EJSON.stringify()`` method has these fields:
5759 - Effect
5860
5961 * - array
60- - An array of document fields to include in the output
62+ - An array of document fields to include in the output. The
63+ array elements must specify the field names to include in
64+ the returned JSON string.
6165
6266 * - function
6367 - A function that takes two parameters, ``key`` and
6468 ``value``. ``key`` provides the function's ``this``
65- context. EJSON returns the transformed ``value``. See
66- below for an :ref:`example <ex-ejson-function>`
69+ context. EJSON returns the transformed ``value``.
70+
71+ The function is run for each object. The object
72+ values are replaced with the function's return value.
73+
74+ For an example, see :ref:`ex-ejson-function`.
6775
6876 * - ``spacer``
6977 - integer or string
@@ -209,7 +217,7 @@ Use a Function to Transform Output Fields
209217~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
210218
211219To transform field values, use a JavaScript function to set the
212- ``replace `` option.
220+ ``replacer `` option. For example:
213221
214222.. code-block:: javascript
215223
@@ -229,7 +237,10 @@ To transform field values, use a JavaScript function to set the
229237
230238The function runs recursively against the input object.
231239
240+ Example output:
241+
232242.. code-block:: javascript
243+ :copyable: false
233244
234245 [
235246 {
@@ -273,6 +284,156 @@ The function also modifies the ``quantity`` field in the output string.
273284All of the ``quantity`` values are multiplied by two in the output
274285string. ``EJSON.stringify()`` does not update the collection.
275286
287+ Use a Function to Transform Output Fields in Nested Objects
288+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
289+
290+ Create the ``salesWithAddress`` collection with nested addresses:
291+
292+ .. code-block:: javascript
293+
294+ db.salesWithAddress.insertMany( [
295+ { custId: 345, purchaseDate: ISODate("2023-07-04"),
296+ quantity: 4, cost: Decimal128("100.60"),
297+ address: { number: 100, street: "Main Street", ZIP: 12345 } },
298+ { custId: 346, purchaseDate: ISODate("2023-07-12"),
299+ quantity: 3, cost: Decimal128("175.45"),
300+ address: { number: 200, street: "East Street", ZIP: 12345 } }
301+ ] )
302+
303+ The following example uses a ``replacer`` function to change the ZIP
304+ codes for the addresses to ``55555``:
305+
306+ .. code-block:: javascript
307+
308+ // Retrieve the salesWithAddress contents as an array and save
309+ // in queryResults
310+ let queryResults = db.salesWithAddress.find().toArray()
311+
312+ // Define a replacer function to change the ZIP codes
313+ let replacer = function( key, value ) {
314+ if (key === 'address') {
315+ value.ZIP = 55555;
316+ }
317+ return value;
318+ }
319+
320+ // Run EJSON.stringify() to change the ZIP codes in queryResults
321+ EJSON.stringify( queryResults, replacer, 3 )
322+
323+ Example output:
324+
325+ .. code-block:: javascript
326+ :copyable: false
327+
328+ [
329+ {
330+ "_id": {
331+ "$oid": "65498c6562f443aa1490070f"
332+ },
333+ "custId": 345,
334+ "purchaseDate": {
335+ "$date": "2023-07-04T00:00:00Z"
336+ },
337+ "quantity": 4,
338+ "cost": {
339+ "$numberDecimal": "100.60"
340+ },
341+ "address": {
342+ "number": 100,
343+ "street": "Main Street",
344+ "ZIP": 55555
345+ }
346+ },
347+ {
348+ "_id": {
349+ "$oid": "65498c6562f443aa14900710"
350+ },
351+ "custId": 346,
352+ "purchaseDate": {
353+ "$date": "2023-07-12T00:00:00Z"
354+ },
355+ "quantity": 3,
356+ "cost": {
357+ "$numberDecimal": "175.45"
358+ },
359+ "address": {
360+ "number": 200,
361+ "street": "East Street",
362+ "ZIP": 55555
363+ }
364+ }
365+ ]
366+
367+ Use a Function to Replace BSON Strings
368+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
369+
370+ For a list of BSON data types and the corresponding numeric codes, see
371+ :manual:`BSON Types </reference/bson-types>`.
372+
373+ The following example uses a ``replacer`` function to replace the BSON
374+ strings with the string ``"This is a string"``:
375+
376+ .. code-block:: javascript
377+
378+ // Retrieve the salesWithAddress contents as an array and save
379+ // in queryResults
380+ let queryResults = db.salesWithAddress.find().toArray()
381+
382+ // Define a replacer function to replace the strings
383+ let replacer = function( key, value ) {
384+ if (typeof value === "string") {
385+ return "This is a string";
386+ }
387+ return value;
388+ }
389+
390+ // Run EJSON.stringify() to replace the strings in queryResults
391+ EJSON.stringify( queryResults, replacer, 3 )
392+
393+ Example output:
394+
395+ .. code-block:: javascript
396+ :copyable: false
397+
398+ [
399+ {
400+ "_id": {
401+ "$oid": "This is a string"
402+ },
403+ "custId": 345,
404+ "purchaseDate": {
405+ "$date": "This is a string"
406+ },
407+ "quantity": 4,
408+ "cost": {
409+ "$numberDecimal": "This is a string"
410+ },
411+ "address": {
412+ "number": 100,
413+ "street": "This is a string",
414+ "ZIP": 12345
415+ }
416+ },
417+ {
418+ "_id": {
419+ "$oid": "This is a string"
420+ },
421+ "custId": 346,
422+ "purchaseDate": {
423+ "$date": "This is a string"
424+ },
425+ "quantity": 3,
426+ "cost": {
427+ "$numberDecimal": "This is a string"
428+ },
429+ "address": {
430+ "number": 200,
431+ "street": "This is a string",
432+ "ZIP": 12345
433+ }
434+ }
435+ ]
436+
276437Write to a File from Inside mongosh
277438~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278439
0 commit comments